BoD BoD - 4 months ago 58
Android Question

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.

BoD BoD
Answer

It seems that tinting a ripple drawable is meaningless (and the default background of a button is a ripple drawable).

In fact, after looking at the platform's default button drawable, I found the "correct" way to do this:. You have to define this in your theme:

    <item name="android:colorButtonNormal">@color/accent</item>

(Of course this is only for level 21+.)

Warning: since this is defined in a theme, this will use the given color for all the buttons (at least all of the buttons in activities using that theme.)

As a bonus, you can also change the ripple color by defining this:

    <item name="android:colorControlHighlight">@color/accent_ripple</item>
Comments