Resize drawable image in TextView android
Asked Answered
O

3

8

Wonder how to resize an image inside a TextView xml in a LinearLayout. I read some solution using setCompoundDrawablesWithIntrinsicBounds method, but cannot get it right.

Here is the image:

enter image description here

I want to reduce the size of the logo.

Here is my TextView code:

<TextView  
    android:id="@+id/heading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/android_logo"
    android:drawableStart="@drawable/android_logo"
    android:gravity="top"
    android:padding="16dp"
    android:singleLine="true"
    android:text="@string/heading"
    android:textSize="20dp"
    android:drawablePadding="5dp"/>
Oaten answered 21/3, 2018 at 8:30 Comment(0)
C
27

Try :

 android:scaleX="0.7"
 android:scaleY="0.7"

OR

Wrap your resource in a drawable that defines your desired size similar to:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

  <item
      android:drawable="@drawable/icon"
      android:width="@dimen/icon_size"
      android:height="@dimen/icon_size"
      />

</layer-list >

then use it in drawable left.

Cockneyism answered 21/3, 2018 at 8:32 Comment(3)
The 2nd suggestion works with TextView with the image. Thanks for the solution!Oaten
@PattyPutty Happy to help :) Happy coding :)Cockneyism
Note "width" and "height" item attributes don't work in Android versions older than 6.0.Rasure
I
3

there is only one solution take image view for the image and resize image but text display image right side . you used below code and i hope your issue solved..

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/icon"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:src="@drawable/ic_person" />

<TextView
    android:id="@+id/heading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:drawablePadding="5dp"
    android:gravity="top"
    android:padding="5dp"
    android:singleLine="true"
    android:text="heading"
    android:textSize="20dp"
    app:layout_constraintLeft_toRightOf="@+id/icon" />

Impaction answered 21/3, 2018 at 9:50 Comment(0)
S
0

Programmatic solution:

You can use drawable.setBounds but it requires pixels rather than dp as parameters.

how to convert: Converting pixels to dp

code:

        Drawable drawable = getDrawable(R.drawable.ic_your_drawable);
        int size = dp2px(this, dpSize);
        if (drawable != null) {
            drawable.setBounds(0, 0, size, size);
        }

        textView.setCompoundDrawables(drawable, drawable, drawable, drawable); // should be (drawable, null, null, null) for you 
Sickening answered 16/5, 2020 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.