Skip to content Skip to sidebar Skip to footer

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='@drawab

Solution 1:

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.

ButtonyourButton= findViewById(R.id.bt_search_vehicle_car);

Drawabledrawable= 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.

ButtonyourButton= findViewById(R.id.bt_search_vehicle_car);

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

Solution 2:

Use setCompoundDrawableTintList property to change color i use it as following

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

Solution 3:

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

Solution 4:

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(newPorterDuffColorFilter(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.

Solution 5:

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

Post a Comment for "Change Android Button Drawable Icon Color Programmatically"