Using Drawablecompat Class To Apply A Tintlist
Solution 1:
d = DrawableCompat.wrap(d);
creates a new instance if it's not already DrawableWrapper
so you tint this new instance but the original which is stored in the button remains the same.
The whole code would look something like this
Buttonb= (Button) findViewById(R.id.button);
Drawabled= b.getBackground();
d = DrawableCompat.wrap(d);
DrawableCompat.setTintList(d, getResources().getColorStateList(...));
b.setBackground(d); // or setBackgroundDrawable on older platforms
So yeah, I'd go with the second approach you described because it abstracts the hard work from you.
EDIT:
Just took a dive into appcompat code and found out that the AppCompatButton
tints iself and not the drawable unlike Lollipop native (but only if the background is on the whitelist, e.g. default appcompat button drawable). So you have to clear tint from the button itself first.
Buttonb= (Button) findViewById(R.id.button);
if (b instanceof AppCompatButton) {
((AppCompatButton)b).setSupportBackgroundTintList(null);
}
Drawabled= b.getBackground();
d = DrawableCompat.wrap(d);
DrawableCompat.setTintList(d, getResources().getColorStateList(...));
b.setBackground(d); // or setBackgroundDrawable on older platforms
EDIT 2:
The above code will throw a NullPointerException
when you try to reset the button's tint list. I'm currently filing a bug report.
In the meantime I suggest you inflate the button with a custom background (non-whitelisted for tinting by appcompat) directly or with @null
background and resolving the default button background by
TypedArrayta= context.obtainStyledAttributes(null, newint[]{android.R.attr.background}, R.attr.buttonStyle, R.style.Widget_AppCompat_Button);
Drawabled= ta.getDrawable(0);
ta.recycle();
Final solution
So as all this looks pretty fugly, the easiest (and only working and foolproof, yet hidious as well) solution for you now is this:
Buttonb= (Button) findViewById(R.id.button);
ColorStateListc= getResources().getColorStateList(...);
Drawabled= b.getBackground();
if (b instanceof AppCompatButton) {
// appcompat button replaces tint of its drawable background
((AppCompatButton)b).setSupportBackgroundTintList(c);
} elseif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Lollipop button replaces tint of its drawable background// however it is not equal to d.setTintList(c)
b.setBackgroundTintList(c);
} else {
// this should only happen if // * manually creating a Button instead of AppCompatButton// * LayoutInflater did not translate a Button to AppCompatButton
d = DrawableCompat.wrap(d);
DrawableCompat.setTintList(d, c);
b.setBackgroundDrawable(d);
}
You should put this monstrosity away in a utility class.
Post a Comment for "Using Drawablecompat Class To Apply A Tintlist"