Android – Display Images inside Textview

Hello Readers,

While designing we may get a situation where we may have to display a image near text. For example

dollar – Here is your Reward.

In Android, to display like this, we may use a Imageview followed by a Textview. The xml code may be

 <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <ImageView
            android:id="@+id/dollar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/dollar"
            android:contentDescription="@string/img" />
       <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rewardtext"/>
   </LinearLayout>

But the simple way to do this is, use the following code.

<TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/rewardtext"
        android:drawableLeft="@drawable/dollar"/>

Just adding a line android:drawableLeft=”@drawable/dollar” places the image left of the Textview. We can also use drawableRight,drawableTop,drawableBottom and place the image easily inside a Textview.

Nagarajan
Follow me

Nagarajan

Web Developer at Energy Alternatives India
I'm interested in developing Web applications andkeen on learning new technologies.

View Nagarajan's Profile
Nagarajan
Follow me

Latest posts by Nagarajan (see all)

Comments
  1. Parveen

Leave a Reply

Your email address will not be published. Required fields are marked *