Scaling an ImageView to the size of a TextView in XML

I have a TextView and ImageView in a horizontal linear layout. Is it possible to scale the ImageView to fit the size of the TextView in the layout file?

Basically, I want something like: -

enter image description here

Instead of something like this

enter image description here

Layout file will be

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView /> <TextView /> </LinearLayout> 
+6
source share
3 answers

Yes, you can reduce the ImageView in height relative to the TextView . To match the height of the TextView , set match_parent to android:layout_height for the ImageView , which will help ImageView take the height of the TextView . Try the following ...

 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@drawable/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textSize="30sp" /> </LinearLayout> 

You can also achieve the same result as below.

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher" android:text="TextView" android:gravity="center_vertical" android:textSize="30sp" /> 
+8
source

try it

  <TextView android:id="@+id/Text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/image" /> 
-1
source

Using this, you can simply add images inside your text box

 <TextView> android:drawableLeft="@drawable/picture.png" </TextView> 
-1
source

All Articles