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

Using a Compound Drawable

Author: Julien Quiévreux
Published? false
FormatLanguage: WikiFormat

Problem:

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

Solution:

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

Discussion:

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:

<TextView
     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]

No records found.