0

This is a very confusing question since there are so much information available and we have so many versions of appcompat support library that seem to break and get fixed repeatedly.

Even on appcompat support library 23.2.1, AppCompatButton attribute app:backgroundTint does not correctly work on Android 5.0. It works perfectly on Android 4.4 and lower and Android 5.1 and higher. Only Android 5.0 has problems with it.

So how to have a tinted button using AppCompat support library that runs correctly on Android 5.0?

(I will answer my own question.)

Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228

2 Answers2

1

This is a known issue of 23.2.1 and marked as 'FutureRelease' i.e., fixed for the next version.

Edit: Still not fixed in 23.3.0.

Edit: Finally fixed in 23.4.0.

Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Hey Ian, finally I can get a real Google engineer to talk about this issue. Thanks for pointing me to the bug report! This has been confusing me for months. – Randy Sugianto 'Yuku' Mar 18 '16 at 04:06
  • @yuku - please do file bugs at [b.android.com](http://b.android.com) when you see issues - we do look there frequently (particularly after immediately after a release)! – ianhanniballake Mar 18 '16 at 04:11
  • Unfortunately with the new release 23.3.0, it is still not working. Screenshots and project files attached at https://code.google.com/p/android/issues/detail?id=203872#c10 @ianhanniballake – Randy Sugianto 'Yuku' Apr 14 '16 at 08:27
0

This answer assumes using appcompat-v7 support library 23.2.1.

With reference to Lollipop's backgroundTint has no effect on a Button, it is indeed not possible to tint a button natively in Android 5.0. (In Android 5.1 they fixed it so that it is possible.)

Even when not using the appcompat library, <Button android:backgroundTint="..."/> does not work. The button stays grey.

Examining the source code of appcompat version 23.2.1, it seems that the compatibility tinting is only applied on Android 4.4 and below. On Android 5.0 and above, it uses the native tinting. Unfortunately that is the wrong way to go. On Android 5.0, the compatibility tinting should be applied as well.

You can force compatibility tinting by calling setSupportBackgroundTintList on the button from your code and the tinting will happen correctly on Android 5.0.

One of the possible solutions is to subclass AppCompatButton as follows:

public class AppCompatButton2 extends AppCompatButton {
    public AppCompatButton2(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            final ColorStateList csl = getBackgroundTintList();
            setSupportBackgroundTintList(csl);
        }
    }
}
Community
  • 1
  • 1
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228