Search code examples
androidandroid-layouttint

Lollipop's backgroundTint has no effect on a Button


I have a Button in my Activity, and I'd like it to have my theme's accent color. Instead of making my own drawables like we had to do pre-Lollipop, naturally I'd like to use the new backgroundTint attribute.

<Button
    android:id="@+id/btnAddCode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/accent"
    android:text="@string/addressInfo_edit_addCode" />

Unfortunately it has no effect, the button stays gray.

I tried different values for backgroundTintMode, which didn't change anything.

I also tried doing it programmatically in my Activity, which didn't change anything.

addCodeView.findViewById(R.id.btnAddCode).setBackgroundTintList(
     getResources().getColorStateList(R.color.accent));

Why is my tint ignored?

EDIT: Just to clarify, I am indeed testing on a Lollipop device. Other widgets (e.g. EditText) are correctly and automatically tinted.


Solution

  • Tested on API 19 through API 27

    <?xml version="1.0" encoding="utf-8"?>
      <android.support.v7.widget.AppCompatButton 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/retry"
        android:textColor="@android:color/white"
        app:backgroundTint="@android:color/holo_red_dark" />
    

    produces output as -

    enter image description here