Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing BackgroundTint of a MaterialButton

Tags:

android

I'm using the lasted support design : 28, alpha3.

I use using the "Theme.MaterialComponents.Light.NoActionBar" as the theme for my application and "MaterialButton" instead of a normal "Button" in my layouts.

I can set the BackgroundTind from the XML as normal but i can't change it via java.

I tried:

deliverSwitch.setBackgroundTintList(getResources().getColorStateList(R.color.colorYellow));

deliverSwitch.setSupportBackgroundTintList(getResources().getColorStateList(R.color.colorYellow));

but none of them worked... I also tried to clear the current tint by leaving the setBackgroundTintList null and it doesn't work either.

like image 273
Florin Barbu Avatar asked Oct 29 '25 13:10

Florin Barbu


1 Answers

I couldn't get it working either. As a workaround I did the following: First you get the current background Drawable, then you tint it with the desired color and set the new background with setBackgroundDrawable for your Material Button.

Drawable background = materialButton.getBackground();
background.setTint(ContextCompat.getColor(getContext(), R.color.bg_button_primary));
materialButton.setBackgroundDrawable(background);

I hope that helps.

like image 100
f4b Avatar answered Nov 01 '25 05:11

f4b



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!