setCompoundDrawable to add Drawables to TextView
Finally trying to get a couple new posts up, have a bunch of material just not a lot of free time to get ‘em posted. Here’s a quick and easy one.
I was using the setCompoundDrawable method for a TextView to set an image to the left of some text, I was hoping it was going to be as easy as loading the drawable and calling
txtVw.setCompoundDrawables( img, null, null, null );
Unfortunately nothing showed up, and I had no internet connection at the time, so I was left to fumble around trying a bunch of different things…nothing worked. Finally, after getting to work and looking at some existing code from a co-worker, I found out that using setBounds will define a bounding rectangle around the image. So the final lines of code look like…
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley ); img.setBounds( 0, 0, 60, 60 ); txtVw.setCompoundDrawables( img, null, null, null );
UPDATE: See comments to make this even easier, thanks Romain Guy and Nerdrow

June 5th, 2010 at 12:19 am
It’s much easier than this, just call setCompoundDrawableWithIntrinsicBounds() instead. It will use the size of the image as the size of the Drawable.
June 5th, 2010 at 12:23 am
Or you could just use “txtVw.setCompoundDrawableWithIntrinsicBounds(img, null, null, null);”
June 24th, 2011 at 8:43 am
Is it possible to make the image clickable?