setCompoundDrawablesWithIntrinsicBounds/android:drawableLeft - Add a Drawable to the Left of the text in a TextView, in Java

setCompoundDrawablesWithIntrinsicBounds - How to Add a Drawable to the Left of the text in a TextView, in Java

setCompoundDrawablesWithIntrinsicBounds was added in API Level 3
Related XML Attribute: android:drawableLeft 

setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

The drawable (image, picture, etc) will be displayed to the left of the text

1. Ensure to change the minSDKVersion to 3, or greater.

2. Add the following text to the TextView: "This is an example with a drawable set to the left in a TextView."


4. Add a drawable named myImage

5. Add the code below to the onCreate method.

myTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myImage,0,0,0);

6. Compile and run!



Related Articles: