android – How do I set the color for a button while maintaining the ripple effect?

Question:

I use AppCompatButton buttons in my application (minSdk 9). Buttons in my application come in different colors and can become inactive ( setEnabled(false) ).

I want to make them flat and with ripple animation, according to the material design. But I can't give them a color. Either the ripple effect is lost, or they stop turning gray when inactive.

How can I set the color of the button so that the ripple effect is preserved, but in the inactive state they are gray?

At the moment there is this:

<style name="MyGreenButton"  parent="Widget.AppCompat.Button.Colored">
   <item name="android:colorButtonNormal">#0f0</item>
   <item name="android:textColor">#fff</item>
</style>


<android.support.v7.widget.AppCompatButton
   android:theme="@style/MyGreenButton"/>

The buttons change color, the text is white, but there is no ripple effect and in the inactive state the color remains green.

Answer:

It turned out to be implemented like this. I checked it on OS versions from 2.3.3 to 6.0.1, it works (of course, the ripple effect is only from 5).

Markup:

<android.support.v7.widget.AppCompatButton
  style="@style/MyButton"
  ...
  />

styles.xml:

<style name="MyButton" parent="Widget.AppCompat.Button.Colored">
  <item name="backgroundTint">@color/button_background_selector</item>
  <item name="android:textColor">@color/button_text_selector</item>
</style>

button_background_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="#555555"/>
    <item android:color="#00ff00"/>
</selector>

button_text_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="#888888"/>
    <item android:color="#ffffff"/>
</selector>
Scroll to Top