Skip to content Skip to sidebar Skip to footer

Android: Fit Height Of Drawableleft In A Textview

I am trying to display a blue line next to a block of text, pretty much like this: Here's my code:

Solution 1:

You can try doing it in code by setting bounds for the image

textView1.getViewTreeObserver()
        .addOnGlobalLayoutListener(newOnGlobalLayoutListener() {
        @OverridepublicvoidonGlobalLayout() {
            Drawableimg= ActivityName.this.getContext().getResources().getDrawable(
                R.drawable.blue_line);
            img.setBounds(0, 0, img.getIntrinsicWidth() * textView1.getMeasuredHeight() / img.getIntrinsicHeight(), textView1.getMeasuredHeight());
            textView1.setCompoundDrawables(img, null, null, null);
            textView1.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });

Solution 2:

The best way to do this is to wrap your drawable in an xml drawable file and set it as a drawable in your text view as follows:

Drawable XML File:

<?xml version="1.0" encoding="utf-8"?><bitmapxmlns:android="http://schemas.android.com/apk/res/android"android:scaleType="fitXY"android:src="@drawable/total_calories"/>

TextView in XML:

<TextView 
    android:id="@+id/title_total_cal"
    style="@style/title_stats_textview"
    android:drawableLeft="@drawable/total_calories_drawable"/>

Solution 3:

Try as below...

<RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content" ><ImageViewandroid:id="@+id/imageView"android:layout_width="wrap_content"android:layout_height="match_parent"android:src="@drawable/btndr" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/imageView" /></RelativeLayout>

Solution 4:

Simply, Keep your image as 9patch drawable.

You can add android:drawableLeft="@drawable/checkmark" to your textview. You can also set drawablePadding to keep the textview organized.

android:drawableLeft="@drawable/button_icon"android:drawablePadding="2dip"

Here is the link to create 9patch drawable

<TextView android:text="@string/txtUserName"android:id="@+id/txtUserName"android:layout_width="160dip"android:layout_height="60dip"android:layout_gravity="center"android:drawableLeft="@drawable/button_icon"android:drawablePadding="2dip"
/>  

Solution 5:

Unfortunately, setBounds was not working for me so I had to do a workaround.

// Turn wanted drawable to bitmapDrawabledr= getResources().getDrawable(R.drawable.somedrawable);
Bitmapbitmap= ((BitmapDrawable) dr).getBitmap();

// I had a square image with same height and width so I needed only TextView height (getLineHeight)intsize= textView1.getLineHeight();
Drawabled=newBitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, size, size, true));
textView1.setCompoundDrawables(d, null, null, null);

// Now we can set some spacing between text and image
textView1.setCompoundDrawablePadding(10);

It is not the best solution regarding performance because new bitmap is created, but still works.

Post a Comment for "Android: Fit Height Of Drawableleft In A Textview"