Programmatically set left drawable in a TextView

AndroidTextviewAndroid Drawable

Android Problem Overview


I have a textView in xml here.

<TextView
		android:id="@+id/bookTitle"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_weight="1"
		android:drawableLeft="@drawable/checkmark"
		android:gravity="center_vertical"
		android:textStyle="bold"
		android:textSize="24dip"
		android:maxLines="1"
		android:ellipsize="end"/>

As you can see I set the DrawableLeft in xml.

I would like to change the drawable in code.

Is there anyway to go about doing this? Or setting the drawableLeft in code for the text view?

Android Solutions


Solution 1 - Android

You can use setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)

set 0 where you don't want images

Example for Drawable on the left:

TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

Alternatively, you can use setCompoundDrawablesRelativeWithIntrinsicBounds to respect RTL/LTR layouts.


Tip: Whenever you know any XML attribute but don't have clue about how to use it at runtime. just go to the description of that property in developer doc. There you will find Related Methods if it's supported at runtime . i.e. For DrawableLeft

Solution 2 - Android

From here I see the method setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) can be used to do this.

Solution 3 - Android

Using Kotlin:

You can create an extension function or just use setCompoundDrawablesWithIntrinsicBounds directly.

fun TextView.leftDrawable(@DrawableRes id: Int = 0) {
   this.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0)
}

If you need to resize the drawable, you can use this extension function.

textView.leftDrawable(R.drawable.my_icon, R.dimen.icon_size)

fun TextView.leftDrawable(@DrawableRes id: Int = 0, @DimenRes sizeRes: Int) {
    val drawable = ContextCompat.getDrawable(context, id)
    val size = resources.getDimensionPixelSize(sizeRes)
    drawable?.setBounds(0, 0, size, size)
    this.setCompoundDrawables(drawable, null, null, null)
}

To get really fancy, create a wrapper that allows size and/or color modification.

textView.leftDrawable(R.drawable.my_icon, colorRes = R.color.white)

fun TextView.leftDrawable(@DrawableRes id: Int = 0, @DimenRes sizeRes: Int = 0, @ColorInt color: Int = 0, @ColorRes colorRes: Int = 0) {
    val drawable = drawable(id)
    if (sizeRes != 0) {
        val size = resources.getDimensionPixelSize(sizeRes)
        drawable?.setBounds(0, 0, size, size)
    }
    if (color != 0) {
        drawable?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
    } else if (colorRes != 0) {
        val colorInt = ContextCompat.getColor(context, colorRes)
        drawable?.setColorFilter(colorInt, PorterDuff.Mode.SRC_ATOP)
    }
    this.setCompoundDrawables(drawable, null, null, null)
}

Solution 4 - Android

You can use any of the following methods for setting the Drawable on TextView:

1- setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)

2- setCompoundDrawables(Left_Drawable, Top_Drawable, Right_Drawable, Bottom_Drawable)

And to get drawable from resources you can use:

getResources().getDrawable(R.drawable.your_drawable_id);

Solution 5 - Android

A Kotlin extension + some padding around the drawable

fun TextView.addLeftDrawable(drawable: Int, padding: Int = 32) {
    val imgDrawable = ContextCompat.getDrawable(context, drawable)
    compoundDrawablePadding = padding
    setCompoundDrawablesWithIntrinsicBounds(imgDrawable, null, null, null)
}

Solution 6 - Android

there are two ways of doing it either you can use XML or Java for it. If it's static and requires no changes then you can initialize in XML.

  android:drawableLeft="@drawable/cloud_up"
    android:drawablePadding="5sp"
  

Now if you need to change the icons dynamically then you can do it by calling the icons based on the events

       textViewContext.setText("File Uploaded");
textViewContext.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uploaded, 0, 0, 0);

                     

Solution 7 - Android

Works for me to change the text view left/right drawable color

for (drawable in binding.tvBloodPressure.compoundDrawablesRelative) {
            if (drawable != null) {
                drawable.colorFilter = PorterDuffColorFilter(
                    ContextCompat.getColor(binding.tvBloodPressure.context, color),
                    PorterDuff.Mode.SRC_IN
                )
            }
        }

Solution 8 - Android

I am using like this.

  txtUserName.setCompoundDrawablesWithIntrinsicBounds(
        requireActivity().getDrawable(
            R.drawable.ic_my_account
        ), null, null, null
    )

Solution 9 - Android

static private Drawable **scaleDrawable**(Drawable drawable, int width, int height) {

    int wi = drawable.getIntrinsicWidth();
    int hi = drawable.getIntrinsicHeight();
    int dimDiff = Math.abs(wi - width) - Math.abs(hi - height);
    float scale = (dimDiff > 0) ? width / (float)wi : height /
            (float)hi;
    Rect bounds = new Rect(0, 0, (int)(scale * wi), (int)(scale * hi));
    drawable.setBounds(bounds);
    return drawable;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncoder_For_Life22View Question on Stackoverflow
Solution 1 - AndroidBrainCrashView Answer on Stackoverflow
Solution 2 - AndroidJackView Answer on Stackoverflow
Solution 3 - AndroidGiboltView Answer on Stackoverflow
Solution 4 - AndroidShajeel AfzalView Answer on Stackoverflow
Solution 5 - Androidr3dm4nView Answer on Stackoverflow
Solution 6 - AndroidAyan BhattacharjeeView Answer on Stackoverflow
Solution 7 - AndroidMohsin IqbalView Answer on Stackoverflow
Solution 8 - AndroidPeterView Answer on Stackoverflow
Solution 9 - AndroidRashmi PView Answer on Stackoverflow