Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kevin Gonzales
4,638 PointsCan you use PNG's? I attempted too use a png and got a failed to convert into a drawable. Help please.
I cannot use a PNG file as a drawable. Can you tell me how too?
1 Answer
Ben Junya
12,365 PointsYou can totally use a PNG in Android. In fact, it's recommended over jpeg or gif.
Android's picky about the filename, so you need to make sure there's no uppercase letters or spaces in the name.
You can put the PNG in the drawable folder - any of them will work, but you need to be conscious of the pixel density of the screen - xhdpi, hdpi, mdpi, ldpi (xtra high, high, medium, and low respectively).
You can access the file with the R.drawable object in Android - for example:
Filename: mypicture.png
Access with: R.drawable.mypicture
If you're setting it dynamically with code, you can use the imageView.setImageResource method:
ImageView myImageView = (ImageView) findViewById(R.id.your_imageview_id);
myImageView.setImageResource(R.drawable.mypicture);
Or if you're doing it from the XML Layout file, You can use the GUI to do it, but I prefer the text editor. It will look something like this:
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/imageView4"
android:background="@drawable/mypicture"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
/>
<!-- Note: I'm using RelativeLayout -->
Hope this helps!
Kevin Gonzales
4,638 PointsKevin Gonzales
4,638 PointsYes it did! Thank You!