Reputation: 1269
I am applying the method setBackgroundTintList() on my AppCompatButton class as the following:
applyTintColor(this.appCompatButton, R.drawable.button_orange_color_state);
The method applyTintColor:
public void applyTintColor(@NonNull View view, @DrawableRes int color) {
ColorStateList colorStateList = ContextCompat.getColorStateList(view.getContext(), color);
ViewCompat.setBackgroundTintList(view, colorStateList);
}
My button_orange_color_state.xml file.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Disable background -->
<item android:color="@color/brand_secondary"
android:state_enabled="false"/>
<!-- Default background -->
<item android:color="@color/orange"/>
</selector>
Problem: After applying the method applyTintColor, the button take the shape with the default white transparent color instead of my orange color. But I noticed, when I press the button, the button correctly take the color of the disable state, then after enabling the button again, the color goes to orange. The only, but dirty way I found so far, is then to add the following piece of code at the end of my applyTintColor:
view.setEnabled(!view.isEnabled());
view.setEnabled(!view.isEnabled());
In that way, the button directly take into account the good orange color.
So I wondering if someone has a better idea to make things better here ?
Upvotes: 2
Views: 1628
Reputation: 1780
For future reference, if the view has already been laid out, after view.setBackgroundTintList(tint)
, you can call view.refreshDrawableState()
.
Upvotes: 2