I have a button that have two state and must have different background for each of them.
For using advantage of android default implement of Button (for example ripple effect in +Lollipop) i didn't define custom background and using colorButtonNormal attribute as below :
<style name="PrimaryButton.Success">
<item name="colorButtonNormal">@color/colorSuccess</item>
</style>
<style name="PrimaryButton.Fail">
<item name="colorButtonNormal">@color/colorFail</item>
</style>
I know how to set theme for my button when using XML(setting app:theme attribute of my AppCompatButton) but as i mention above,I need change it on run-time programmatically. how can i do it ?
If you only need one color at a time (in spite of the state), you can use
Button button = ...;
int color = ...;
ViewCompat.setBackgroundTintList(button, ColorStateList.valueOf(color));
This will preserve ripple effect on Lollipop and newer devices (API 21+)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With