android:drawableTop - Set a Drawable Above Text, in a TextView, in XML

How to Set a Drawable Above Text, in a TextView, in XML

android:drawableTop was added in API Level 1 
Related Method: setCompoundDrawablesWithIntrinsicBounds - Add a Drawable Above a TextView

The drawable to be drawn above the text.

1. Add a drawable named myImage.

2. Add a TextView.

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

4. In the main.xml file, add the below line

  <TextView
    .... 
      android:drawableTop = "@drawable/myImage"
  />
 
5. At this point, the code will work. But to easier see the defines perimeter of the TextView, let's change the background color of the TextView to Gray. Hex: #808080

6. Compile and run!


android:drawableTop with android:layout_width = "wrap_content"


6. You can also change the android:layout_width to match_parent, and you get the result below.

android:drawableTop with android:layout_width = "match_parent"

Related Articles
Resources:
http://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableTop
http://developer.android.com/reference/android/R.attr.html#drawableTop