Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set buttonColorNormal programatically for Android Button with AppCompat's design effect

I have been using Google design support library for Android. In order to set colour of a button different from the app theme I declare the Button in layout XML file as follows:

<Button
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:theme="@style/MyButton" />

And then define the MyButton in styles.xml as

<style name="MyButton" parent="ThemeOverlay.AppCompat">
    <item name="colorButtonNormal">@color/my_color</item>
</style>

This gives me a button as per the design support library, with the background color as the one defined with @color/my_color in my colors.xml file.

Hence basically it is to use android:theme to change the colorButtonNormal attribute in order to get the desired color.

How could I achieve the same result programatically? Basically if I could do something like

myButton.setTheme(R.style.MyButton) 

...then I could set the colorButtonNormal to get the view.

I cannot set the it like

myButton.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.my_color));

or not even like

ColorStateList colorStateList = ContextCompat.getColorStateList(getActivity(), R.color.my_color);
ViewCompat.setBackgroundTintList(myButton, colorStateList);

This will remove the design support library effect of touch.

like image 870
Tejas Sherdiwala Avatar asked Nov 08 '22 14:11

Tejas Sherdiwala


1 Answers

for Button I wrote this helper method:

public static ColorStateList getButtonColorStateList(Context context, int accentColor) {
    // get darker variant of accentColor for button pressed state
    float[] colorHSV = new float[3];
    Color.colorToHSV(accentColor, colorHSV);
    colorHSV[2] *= 0.9f;
    int darkerAccent = Color.HSVToColor(colorHSV);

    return new ColorStateList(
            new int[][] {{android.R.attr.state_pressed}, {android.R.attr.state_enabled}, {-android.R.attr.state_enabled}},
            new int[] { darkerAccent, accentColor, getColor(context, R.color.buttonColorDisabled) });
}

accentColor is color value for normal enabled state. For pressed state is used darker variant of accentColor and for disable state I have defined color in values:

<color name="buttonColorDisabled">#dddddd</color>

Use this method:

mButton.setSupportBackgroundTintList(Utils.getButtonColorStateList(this, accentColor));

where mButton is AppCompatButton and accentColor is value of color.

This works for me on Lollipop and above with effect of touch and on pre-Lollipop as standard color change.

like image 100
Petr Daňa Avatar answered Nov 14 '22 21:11

Petr Daňa