223

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

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/follow"
    android:id="@+id/button_follow"
    android:backgroundTint="@color/blue_100"
    />

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.

2
  • Are you sure android:backgroundTint is working on Pre-Lollipop? I test with both Button and ApCompatButton but backgroundTint only seems to be working on Lollipop.
    – Sharjeel
    Commented Apr 23, 2015 at 10:44
  • 1
    Please check this answer. Commented Mar 15, 2016 at 8:53

23 Answers 23

262

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

Update

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" >
    <item
        android:color="#your_color_here" />
</selector>

then load it using

setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.your_xml_name));

where contextInstance is an instance of a Context


using AppCompart

btnTag.setSupportButtonTintList(ContextCompat.getColorStateList(Activity.this, R.color.colorPrimary));
9
  • 2
    which is not a color, but a ColorStateList. How to leverage that? Commented Apr 22, 2015 at 14:45
  • 9
    I get how to do it now thanks, but why would Android not allow you to just use a color manually? For every color of every button I have, I'll have to create an xml for a ColorStateList? That seems like a waste to me Commented Apr 22, 2015 at 15:01
  • 4
    setBackgroundTintList needs API 21 even if you call it on AppCompatButton.
    – Sharjeel
    Commented Apr 23, 2015 at 11:20
  • 35
    The AppCompat support library offers a static helper: ViewCompat.setBackgroundTintList(View, ColorStateList) that can be used all the way back to API 4. But it only works for views that implement TintableBackgroundView, for example AppCompatButton (instead of the usual Button).
    – Jon Adams
    Commented Mar 29, 2016 at 20:42
  • 2
    Now, using ViewCompat.setBackgroundTintList(View, ColorStateList), as @Jon Adams suggested, makes even more sense since View.setSupportButtonTintList is restricted with RestrictTo annotation. Details here: developer.android.com/reference/android/support/annotation/…
    – AlexKost
    Commented Nov 1, 2017 at 7:10
124

You could use

button.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.id.blue_100)));

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

2
  • 3
    could u provide complete code to set tint using ur method? Commented May 10, 2016 at 9:20
  • the question is about a button, this answer is about a drawable. Why is this even displaying as an answer. Very misleading and unhelpful.
    – Kaan
    Commented Nov 5, 2022 at 7:55
88

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

ViewCompat.setBackgroundTintList(
    editText, 
    ColorStateList.valueOf(errorColor));
1
  • Much better using it this way, so you get backwards compatibility from API 4!
    – xarlymg89
    Commented Jan 9, 2020 at 11:24
40

here's how to do it in kotlin:

view.background.setTint(ContextCompat.getColor(context, textColor))
2
  • 1
    Care, if you do this, you will set tint of the drawable. So, any view with the same background will be tint !!
    – MakiX
    Commented Oct 21, 2021 at 13:01
  • this is for over API23
    – c-an
    Commented Dec 1, 2021 at 7:09
30

The simple way to do it

in Java

myButton.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.id.white)));

in Kotlin

myButton.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(context, R.color.white) )
3
  • 1
    resources.getColor() is depricated. Commented Nov 26, 2021 at 6:46
  • 1
    It's better to use ContextCompat.getColor(context, R.color.white) Commented Jan 29, 2022 at 11:21
  • 1
    Why not just use ContextCompat.getColorStateList? You can replace two function calls with just one Commented Oct 25, 2023 at 20:50
29

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);
    button.setBackground(buttonDrawable);

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

0
12

Have you tried something like this?

button.setBackgroundTintList(getResources().getColorStateList(R.id.blue_100));

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

4
  • 1
    You could create an xml as described here: developer.android.com/reference/android/content/res/…
    – Chris K.
    Commented Apr 22, 2015 at 15:06
  • 1
    getColorStateList appears to be deprecated. Commented Jan 16, 2016 at 0:54
  • 2
    setBackgroundTintList seem to require API level 21
    – EMalik
    Commented Jul 14, 2016 at 19:38
  • 2
    button. setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.blue)); worked for me
    – jesto paul
    Commented Dec 13, 2016 at 11:38
9

You can use something like that:

myButton.backgroundTintList = AppCompatResources.getColorStateList(context, R.color.primary_variant)
8

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:

<com.google.android.material.button.MaterialButton
    android:id="@+id/accept"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/i_accept"
    android:textSize="18sp"
    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))
2
  • 1
    Is there a similar one for the text tint itself? Commented Jul 18, 2019 at 14:15
  • 1
    you mean text as a button and you want to change the color of the background ? Commented Jul 20, 2019 at 5:11
7

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

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.

0
6

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});
textView.setBackgroundTintList(csl);

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"/>
</selector>

Hope this helps.

2
  • 2
    Minimum required API level 21
    – forsberg
    Commented Feb 13, 2017 at 16:34
  • 1
    well you could just use ColorStateList.valueOf(ColorInt)
    – user924
    Commented Mar 10, 2020 at 10:36
6

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:

myButton.changeColor(R.color.myColor)
4

For ImageButton you can use:

favoriteImageButton.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint
3
  • setColorFilter is not defined for Buttons
    – Jérémy
    Commented Aug 22, 2018 at 20:43
  • It is, for ImageButton. Commented Aug 25, 2018 at 6:49
  • Oh ok, I didn't know about it. But OP is asking for Button. Can you edit your answer with this detail so I can remove my downvote?
    – Jérémy
    Commented Aug 28, 2018 at 1:44
4

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"/>
</selector>

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.

4

In addition to Shayne3000's answer you can also use a color resource (not only an int color). Kotlin version:

var indicatorViewDrawable = itemHolder.indicatorView.background
indicatorViewDrawable = DrawableCompat.wrap(indicatorViewDrawable)
val color = ResourcesCompat.getColor(context.resources, R.color.AppGreenColor, null) // get your color from resources
DrawableCompat.setTint(indicatorViewDrawable, color)
itemHolder.indicatorView.background = indicatorViewDrawable
4

There are three options for it using setBackgroundTintList

int myColor = Color.BLACK;
  1. button.setBackgroundTintList(new ColorStateList(EMPTY, new int[] { myColor }));
  2. button.setBackgroundTintList(ColorStateList.valueOf(myColor));
  3. button.setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.my_color));
3

If you dont want to care about different versions of android, you can use this code, for basicaly any view. Worked for me.

val states = arrayOf(intArrayOf(android.R.attr.state_enabled))
                    val colors = intArrayOf(Color.GREEN) // your color here
                    val colorStateList = ColorStateList(states, colors)
                  ViewCompat.setBackgroundTintList(yourButtonHere,colorStateList)

Kotlin version, wish a nice day to everyone reading this ;)

btw. if you created some drawable background shape this should override only only the tint color.

1
checkbox.ButtonTintList = ColorStateList.ValueOf(Android.Color.White);

Use ButtonTintList instead of BackgroundTintList

1

Tint can be added to button like:

filterBtn.setBackgroundTintList(ContextCompat.getColorStateList(context,R.color.colorPrimary))
0

simple we can also use for an imageview

    imageView.setColorFilter(ContextCompat.getColor(context,
R.color.COLOR_YOUR_COLOR));
0

If anyone wants to know how to set the HTML color code in the background tint (in my case the color code is coming from backend)

String htmlColorCode = "#FF0000";
int colorInt = Color.parseColor(htmlColorCode);
View myView = findViewById(R.id.my_view);
ViewCompat.setBackgroundTintList(myView, ColorStateList.valueOf(colorInt));
-1

With Kotlin,

checkbox.buttonTintList = AppCompatResources.getColorStateList(context, color.colorPrimary)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.