9

I have a TextView and a ImageView in a horizontal linear layout. Is it possible to scale the ImageView so that it is relative to 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

The layout file would be

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <ImageView />
    <TextView />
</LinearLayout>
mfc
  • 3,018
  • 5
  • 31
  • 43

3 Answers3

9

Yeah, you can scale down the ImageView at the height relative to the TextView. To match the height of TextView, set match_parent to android:layout_height for ImageView which will help the ImageView to take the height of TextView. Try as follows...

<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 way...

<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" />
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • Wouldn't setting a left compound drawable be more efficient? – Ben Pearson Mar 04 '14 at 13:49
  • 1
    Its possible...but he is asking that **Is it possible to scale the ImageView so that it is relative to the size of the TextView in the layout file?** Please, read the question properly. – Hamid Shatu Mar 04 '14 at 13:51
  • you basicaly used what I wrote in comment :p – Darko Rodic Mar 04 '14 at 13:51
  • @DarkoRodic...I didn't even notice your comment ;) – Hamid Shatu Mar 04 '14 at 13:53
  • @HamidShatu I read the question properly and interpreted what they wanted to achieve. It's bad for performance to have a heavy view hierarchy. Why use a LinearLayout instead of a compound drawable when a compound drawable is more efficient? – Ben Pearson Mar 04 '14 at 13:57
  • @BenPearson...Yeah, I agree with you. Personally, I will use compound drawable. – Hamid Shatu Mar 04 '14 at 14:00
  • @BenPearson...I have updated my answer in accordance to your argue. Thanks for your concern. :) – Hamid Shatu Mar 04 '14 at 14:04
  • @BenPearson How is it possible to achieve a left compound drawable that will scale to the size of the text? The way compound drawables are implemented, the text can be scaled to the size of the image, but not the other way around. – ashishduh Jun 18 '14 at 21:47
  • any thoughts on how to produce the same result with a RelativeLayout? This solution works fine for LinearLayout but if i change it to a RelativeLayout the scaling doesn't work anymore – Arlzheim Nov 29 '17 at 11:04
-1

Try this

 <TextView
        android:id="@+id/Text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:drawableLeft="@drawable/image"
        />
Brajesh Kumar
  • 929
  • 6
  • 16
-1

By using this you can simply add images inside your textview

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