A
A
Artem2016-05-01 21:42:09
Android
Artem, 2016-05-01 21:42:09

How to set a color for a button while maintaining the ripple effect?

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 material design. But I can't give them a color. Either the ripple effect is lost, or they stop turning gray when inactive.
How to set the color of the button so that the ripple effect is preserved, and 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 the color remains green when inactive.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem, 2016-05-02
@bartwell

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).

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

<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>

<?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>

<?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>

D
Denis Zagaevsky, 2016-05-01
@zagayevskiy

You need to use a special background: developer.android.com/intl/ru/reference/android/gr...
And read about selectors: developer.android.com/intl/ru/guide/topics/resource...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question