Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Using a Compound Drawable

In Chapter: Graphical User Interface
Author: Julien Quiévreux ('letroll')
Published? false
FormatLanguage: WikiFormat


How do I use a "compound drawable", a TextView that contains an ImageView in addition to its text?


Use one of the attributes such as android:drawableLeft="@drawable/up_count_big" to your TextView.


To integrate an image to your textview you must know where you want it, then use the appropriate property:

android: drawableLeft : to place your image on the left of the text. android: drawableTop android: drawableRight android: drawableBottom

Here is an example of use in a layout xml:

     android: layout_width = "wrap_content"
     android: layout_height = "wrap_content"
     android: text = "@ string/text"
     android: drawableTop = "@drawable/ic_launcher"
     android: gravity = "center" />

Here is an example of configuring the drawable in the code:

TextView tv = (TextView) findViewById (R.id.textView);
tv.setCompoundDrawablesWithIntrinsicBounds (0,
    R.drawable.ic_launcher, 0, 0);

See Also:

[1] [2] [3]


If you found this recipe useful, why not buy the book and have the whole collection always at hand?"