coder_For_Life22
coder_For_Life22

Reputation: 26971

Programmatically set left drawable in a TextView

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?

Upvotes: 364

Views: 248794

Answers (11)

JJD
JJD

Reputation: 51804

The essential attributes to vertically center the TextView in a RelativeLayout are:

  • android:layout_height="wrap_content"
  • android:layout_centerInParent="true"

Here applied at your example:

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

Upvotes: 0

Kunj Shingala
Kunj Shingala

Reputation: 71

In Java File for Activity :
top,bottom,left,right are resource of icon.

TextView textView = (TextView) findViewById(R.id.your_id);
textView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

// for example 
personalTextView = (TextView) findViewById(R.id.personal_info);
personalTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.personal, 0, 0, 0);

Upvotes: 2

r3dm4n
r3dm4n

Reputation: 1215

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)
}

Upvotes: 6

Abhay
Abhay

Reputation: 645

I am using like this.

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

Upvotes: 0

Mohsin Iqbal
Mohsin Iqbal

Reputation: 11

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
                )
            }
        }

Upvotes: 1

BrainCrash
BrainCrash

Reputation: 13172

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

Upvotes: 972

Gibolt
Gibolt

Reputation: 47059

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)
}

Upvotes: 21

Ayan Bhattacharjee
Ayan Bhattacharjee

Reputation: 515

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);

Upvotes: 2

Rashmi P
Rashmi P

Reputation: 17

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;
}

Upvotes: -8

Shajeel Afzal
Shajeel Afzal

Reputation: 5953

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);

Upvotes: 8

Jack
Jack

Reputation: 9242

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

Upvotes: 17

Related Questions