How to add drawable icon along with TextView in Android ?

We wanted to show icons along with titles in our applications, this is possible in one way to add Imageview along with textview and combine both in a LinearLayout while we write layouts.xml.

But all of this has been very simplified by adding just a drawable within textview and mention whether we want the icon to be before or after the textview text.

In this post we just show the related code required to be written in TextView.

$ vim app/src/main/res/layout/profile.xml
<TextView

   android:drawablePadding="5dp"
   android:drawableStart="@drawable/svg_heart_border"
   
/>

Above code adds svg drawable we added before to the textview at the beginning of text line as mentioned by “android:drawableStart”and adds padding of 5 dp between drawable icon and text.

You can adjust the icon and text using different parameters as mentioned in https://developer.android.com/reference/android/widget/TextView#attr_android:drawableBottom

Leave a Comment