07-20-2023, 04:35 PM
Generally I think it's best to handle all of the Views, but sadly here for MaterialButton it's different, so this is what I have for both:
```
fun View.setBackgroundTintColor(@ColorInt color: Int) {
val background = background
if (background is RippleDrawable) {
val newBackground = background.mutate() as RippleDrawable
if (color != 0)
newBackground.setColor(ColorStateList.valueOf(color))
this.background = newBackground
} else {
if (color == 0)
ViewCompat.setBackgroundTintList(this, null)
else
ViewCompat.setBackgroundTintList(this, ColorStateList.valueOf(color))
}
}
fun MaterialButton.setBackgroundTintColor(@ColorInt color: Int) {
setBackgroundTintList(ColorStateList.valueOf(color));
}
```
```
fun View.setBackgroundTintColor(@ColorInt color: Int) {
val background = background
if (background is RippleDrawable) {
val newBackground = background.mutate() as RippleDrawable
if (color != 0)
newBackground.setColor(ColorStateList.valueOf(color))
this.background = newBackground
} else {
if (color == 0)
ViewCompat.setBackgroundTintList(this, null)
else
ViewCompat.setBackgroundTintList(this, ColorStateList.valueOf(color))
}
}
fun MaterialButton.setBackgroundTintColor(@ColorInt color: Int) {
setBackgroundTintList(ColorStateList.valueOf(color));
}
```