Home » Android » android – How to add button tint programmatically

android – How to add button tint programmatically

Posted by: admin March 11, 2020 Leave a comment


In the new AppCompat library, we can tint the button this way:


How can I set the tint of the button programmatically in my code?
I’m basically trying to implement a conditional coloring of the button based on some user input.

How to&Answers:

According to the documentation the related method to android:backgroundTint is setBackgroundTintList(ColorStateList list)


Follow this link to know how create a Color State List Resource.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
        android:color="#your_color_here" />

then load it using


where contextInstance is an instance of a Context

using AppCompart

btnTag.setSupportButtonTintList(ContextCompat.getColorStateList(Activity.this, R.color.colorPrimary));


You could use


But I would recommend you to use a support library drawable tinting which just got released yesterday:

Drawable drawable = ...;

// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
drawable = DrawableCompat.wrap(drawable);

// We can now set a tint
DrawableCompat.setTint(drawable, Color.RED);
// ...or a tint list
DrawableCompat.setTintList(drawable, myColorStateList);
// ...and a different tint mode
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);

You can find more in this blog post (see section “Drawable tinting”)


Seems like views have own mechanics for tint management, so better will be put tint list:



In properly extending dimsuz’s answer by providing a real code situation, see the following code snippet:

    Drawable buttonDrawable = button.getBackground();
    buttonDrawable = DrawableCompat.wrap(buttonDrawable);
    //the color is a direct color int and not a color resource
    DrawableCompat.setTint(buttonDrawable, Color.RED);

This solution is for the scenario where a drawable is used as the button’s background. It works on pre-Lollipop devices as well.


Have you tried something like this?


note that getResources() will only work in an activity. But it can be called on every context too.


You can use DrawableCompat e.g.

public static Drawable setTint(Drawable drawable, int color) {
    final Drawable newDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(newDrawable, color);
    return newDrawable;


I had a similar problem. I wished to colour a complex drawable background for a view based on a color (int) value.
I succeeded by using the code:

ColorStateList csl = new ColorStateList(new int[][]{{}}, new int[]{color});

Where color is an int value representing the colour required.
This represents the simple xml ColorStateList:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:color="color here"/>

Hope this helps.


The way I managed to get mine to work was by using CompoundButtonCompat.setButtonTintList(button, colour).

To my understanding this works regardless of android version.


For ImageButton you can use:

favoriteImageButton.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint


this is easily handled in the new Material Button from material design library, first, add the dependency:

implementation 'com.google.android.material:material:1.1.0-alpha07'

then in your XML, use this for your button:

    app:backgroundTint="@color/grayBackground_500" />

and when you want to change the color, here’s the code in Kotlin, It’s not deprecated and it can be used prior to Android 21:

accept.backgroundTintList = ColorStateList.valueOf(ResourcesCompat.getColor(resources, 
R.color.colorPrimary, theme))


here’s how to do it in kotlin:

view.background.setTint(ContextCompat.getColor(context, textColor))


The suggested answer here doesn’t work properly on android 5.0 if your XML based color state list references themed attributes..
For instance, I have an xml color state list like so:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?colorPrimary" android:state_enabled="true"/>
    <item android:alpha="0.12" android:color="?attr/colorOnSurface"/>

Using this as my backgroundTint from xml works just fine on android 5.0 and everything else. However if I try to set this in code like this:

(Don’t do this)

myButton.setSupportButtonTintList(ContextCompat.getColorStateList(myButton.getContext(), R.color.btn_tint_primary));

It actually doesn’t matter if I pass the Activity or the button’s context to ContextCompat.getColorStateList() method, neither will give me the proper color state list with respect to the theme the button is within. This is because using theme attributes in color state lists wasn’t supported until api 23 and ContextCompat does not do anything special to resolve these. Instead you must use AppCompatResources.getColorStateList() which does its own resource parsing/theme attribute resolution on devices < API 23.

Instead, you must use this:

myButton.setSupportBackgroundTintList(AppCompatResources.getColorStateList(myButton.getContext(), R.color.btn_tint_primary));

TLDR: use AppCompatResources and not -ContextCompat- if you’ll need resolved themed resources across all API versions of android.

For more information on the topic, see this article.


If you are using Kotlin and Material Design, you can change color of your MaterialButton like this:

myButton.background.setTintList(ContextCompat.getColorStateList(context, R.color.myColor))

You can improve it even better by creating an extension function for your MaterialButton in order to make you code more readable and your coding little more convenient:

fun MaterialButton.changeColor(color: Int) {
    this.background.setTintList(ContextCompat.getColorStateList(context, color))

Then, you can use your function everywhere like this: