Lollipop's Backgroundtint Has No Effect on a Button

Lollipop's backgroundTint has no effect on a Button

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 -

Sample Image

Background Tint Text view not working for pre Lollipop Devices

After researching on my own, I found a third party library on github that uses a chat bubble and allows background color changing which is similar to the background tint effect. You can check it out on
https://github.com/himanshu-soni/ChatMessageView

Android setBackgroundTintList on pre-lollipop devices

You can use also setSupportBackgroundTintList

Applies a tint to the background drawable. Does not modify the current tint mode, which is SRC_IN by default.

Subsequent calls to View.setBackground(Drawable) will automatically mutate the drawable and apply the specified tint and tint mode.

Also take a look on ViewCompat.setBackgroundTintList()

Applies a tint to the background drawable.

This will always take effect when running on API v21 or newer. When running on platforms previous to API v21, it will only take effect if view implement the TintableBackgroundView interface.

I found a solution here on SO that I've used before and is this:

public static void setButtonTint(Button button, ColorStateList tint) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && button instanceof AppCompatButton) {
((AppCompatButton) button).setSupportBackgroundTintList(tint);
} else {
ViewCompat.setBackgroundTintList(button, tint);
}
}

It works for me I hope it works for you too.

AppCompatButton 23.2.1 app:backgroundTint doesn't color the button on Android 5.0 (API 21) but works perfectly on ≤ API 19 and ≥ API 22

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.

AndroidX Setting Background Color on Button has NO Effect

As svkaka's answer mentioned, i used app:backgroundTint="#af2222" in xml to solve the issue.

Programatically, i used this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) statusButton.backgroundTintList = ColorStateList.valueOf(MY_COLOR)

else statusButton.setBackgroundColor(MY_COLOR)

Also to note, this is required on Both material and support design button since the android:background="#fff" on both now seems to have no effect

BackgroundTint produces a weird color in Android

As you suspect, the original alpha is your problem. You need it to be ignored in order to get your "clear" tint color. However, ignoring the original alpha also means that you lose the shape/style that the button gets from its 9-patch (as you've already noticed).

Naturally, this makes for an impossible situation -- you cannot both ignore and use the original alpha.

If this is really important, I think your best bet may be to just copy the original 9-patch into your project (don't forget all the dpi variants and various states), make the main button area an opaque white, and make your tinted buttons use that background instead.

The easier alternative is to switch to the Material themes (perhaps the support library version in order to get backwards compatibility). The support library also enables backgroundTint on older Android versions.

AppCompatButton backgroundTint API 21

Just use app:backgroundTint instead of android:backgroundTint, the tint will take effect below Lollipop. The reason is AppCompatActivity AppCompatDelegateImplV7 use AppCompatViewInflater to auto change Button or TextView to AppCompatButton or AppCompatTextView, then app:backgroundTint take effect.

Android Sample Image 20



Related Topics



Leave a reply



Submit