Felipe A.
Felipe A.

Reputation: 949

Change android button drawable icon color programmatically

I want to change my icon color from my button programmatically...

On my xml, i have:

            android:drawableTint="@color/colorPrimary"
            android:drawableTop="@drawable/ic_car_black_24dp"

To set the icon and set the icon color... But i want to change the icon color from my java side...

Can someone help me?

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/bt_search_vehicle_car"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/eight_density_pixel"
            android:layout_weight="1"
            android:background="@drawable/background_rounded_blue_border"
            android:drawableTint="@color/colorPrimary"
            android:drawableTop="@drawable/ic_car_black_24dp"
            android:padding="@dimen/eight_density_pixel"
            android:text="Carros"
            android:textAllCaps="false"
            android:textColor="@color/colorPrimary" />

Upvotes: 18

Views: 26380

Answers (6)

Deepika kapila
Deepika kapila

Reputation: 59

public void setTextViewDrawableTintColor(TextView textView, int color) {
    for (Drawable drawable : textView.getCompoundDrawables()) {
        if (drawable != null) {
            drawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(textView.getContext(), color), PorterDuff.Mode.SRC_IN));
        }
    }
}

Upvotes: 3

Salman Nazir
Salman Nazir

Reputation: 2837

I use vector drawable as drawableLeft for the Button and changed the button drawable colour programmatically using Kotlin like this.

button_id.compoundDrawableTintList = ColorStateList.valueOf(ContextCompat.getColor(context, R.color.blue))

Upvotes: 5

mochadwi
mochadwi

Reputation: 1268

You might wanna check my approach to change button colour dynamically through data binding adapter with kotlin based on your question. Check it out here

I've also mentioned and reference the original answer also the article for an alternative solution or get a better understanding of the workaround

Upvotes: 0

Usman Arshad
Usman Arshad

Reputation: 153

Use setCompoundDrawableTintList property to change color i use it as following

btn_recent.setCompoundDrawableTintList(ColorStateList.valueOf(Color.parseColor("#ff9708"))); 

Upvotes: 12

Gergely Kőr&#246;ssy
Gergely Kőr&#246;ssy

Reputation: 6393

First of all, do not use AppCompatButton directly unless you write a custom view and you want to extend it. The normal Button will be "resolved" by the system as AppCompatButton so you don't need the latter.

As for your original question, there are multiple ways to tint a drawable. You can use DrawableCompact to do it in a "tinting" fashion while you can use a normal ColorFilter to do this in a "filtering" fashion.

Tinting with DrawableCompat

Use DrawableCompat to wrap the drawable so it can be tinted on older platforms.

Button yourButton = findViewById(R.id.bt_search_vehicle_car);

Drawable drawable = getResources().getDrawable(R.drawable.ic_car_black_24dp);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, getResources().getColor(R.color.colorPrimary));

yourButton.setCompoundDrawables(null, drawable, null, null);

Using ColorFilter

Use the Drawable.setColorFilter(...) method to set an overlaying color filter for your drawable.

Button yourButton = findViewById(R.id.bt_search_vehicle_car);

Drawable drawable = getResources().getDrawable(R.drawable.ic_car_black_24dp).mutate();
drawable.setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP);

yourButton.setCompoundDrawables(null, drawable, null, null);

Upvotes: 20

magicleon94
magicleon94

Reputation: 5172

I've assumed that you need to change the android:drawableTint property.

According to this, you need to create a new drawable with a different tint, then change the drawable resource for your button.

Create a Drawable from your icon:

Drawable mDrawable=getContext().getResources().getDrawable(R.drawable.ic_car_black_24dp); 

Then change its tint:

mDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));

One you've done this, set your new Drawable:

yourButton.setImageDrawable(mDrawable);

I suggest you to skim through the comments of the linked question and here in the docs to discover different PorterDuff modes.

Upvotes: 1

Related Questions