4

I would like to change the background color of a button without removing the ripple effect/shadow/hover effect.

I also would like a solution that does not re-implement the ripple effect as described in this question.

It should also work on at least KitKat and above.


Here's a list of things I've tried that do not work:

  1. Button.setBackgroundColor(0x2200FF00);
    

    Changes the background color of a button, but it removes the ripple effect, the shadow, and all visual aesthetics of a standard android material button.

  2. Button.setBackgroundTintList(Context.getResources().getColorStateList(R.color.Blue));
    
    android:backgroundTint="#0000FF"
    

    Works, but not on KitKat (API >= 21).

  3. <style name="ButtonBlue">
        <item name="colorButtonNormal">@color/colorBlue</item>
    </style>
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Button.setTextAppearance(R.style.ButtonBlue);
    } else {
        Button.setTextAppearance(Context, R.style.ButtonBlue);
    }
    

    Only works when defined in XML, setTextAppearance does not dynamically change the theme.

    android:theme="@style/ButtonBlue"
    

Is it possible to dynamically set the button's background color like this?

Dave Chen
  • 10,887
  • 8
  • 39
  • 67

0 Answers0