I want to add icon at the left side of the textView.How can I do that?
Asked
Active
Viewed 1.1e+01k times
68
-
5have you searched for `android:drawableLeft="@drawable/ic_launcher"` – SilentKiller Aug 13 '14 at 06:47
3 Answers
142
You can use:
android:drawableLeft="@drawable/ic_launcher"
and you can also put padding between drawable
and textview
by
android:drawablePadding="2dp"
If you always want an icon to appear before the text, it is recommended to use drawableStart
instead of drawableLeft
since many languages are not read left to right.

ExcellentSP
- 1,529
- 4
- 15
- 39

krunal patel
- 2,369
- 1
- 11
- 11
-
2
-
3For API 17 and above Replace android:drawableLeft with android:drawableStart to support RTL – Pixbyte Sep 22 '19 at 11:19
-
how about if gravity text is center , the icon didnt change into center following the text ? – Yogi Arif Widodo Mar 31 '22 at 02:19
81
You can do this using this code.
TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

Ravindra Kushwaha
- 7,846
- 14
- 53
- 103

Robin Royal
- 1,788
- 1
- 18
- 29
-
-
5@AlexeyShevelyov To center the text, you can use `android:gravity="center_vertical"` in the layout file or `textView.setGravity(Gravity.CENTER_VERTICAL);` in the Java file. – weeix Nov 07 '17 at 03:07
-
4and to add nice padding: `textView.setCompoundDrawablePadding( context.getResources().getDimensionPixelOffset(R.dimen.small_padding)` where R.dimen.small_padding - 16dp – Kirill Karmazin Dec 02 '18 at 15:40
9
You can use this in your XML file:
android:drawableLeft
For your TextView
and specify a drawable there your want to present on the left side of it.

Emil Adz
- 40,709
- 36
- 140
- 187