Changing AppCompatButton color programmatically

Support Library 22.1 introduced a new AppCompatButton that should be knowledgeable and a new DrawableCompat class to set the hue of the drawable.

I just thought I needed to easily create colored buttons and programmatically change the button color.

I create a button in code, retrieve the background image from it and try to color it with a new DrawableCompat class, and then return it to the button.

AppCompatButton btn = new AppCompatButton(context);

Drawable tintDr = DrawableCompat.wrap(btn.getBackground());

DrawableCompat.setTint(tintDr, Color.BLUE);

btn.setBackground(tintDr);

      

As a result, on Android 5.x, the button is blue, but the ripple effect is lost and the button press looks rather strange. On Android <5.0, the button is gray.

How can I press a button programmatically on all android versions?

+3
android


source to share


No one has answered this question yet

Check out similar questions:

979
Hexagonal transparency in colors
389
Mipmap drawables for icons
73
Change toolbar color in Appcompat 21
22
Adding a color filter to Drawable changes all buttons using the same Drawable
ten
ScrollView color change Programmatically
five
set background color and also use selection as background in android
2
How to programmatically set the background of a button using RippleDrawable (API 21)
0
Change theme color programmatically
0
How to create a circular widget in android which color can be changed programmatically?



All Articles
Loading...
X
Show
Funny
Dev
Pics