I am using Android v21 support library.
I have created a button with custom background color. The Material design effects like ripple, reveal are gone (except the elevation on click) when I use the back ground color.
<Button
style="?android:attr/buttonStyleSmall"
android:background="?attr/colorPrimary"
android:textColor="@color/white"
android:textAllCaps="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1"
/>
The following is a normal button and the effects are working just fine.
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:text="Button1"
/>
The @ianhanniballake's answer is absolutely correct and simple. But it took me few days to understand. For someone who don't understand his answer, here is more detail implementation
===Or===
There are two approaches explained in the great tutorial be Alex Lockwood: http://www.androiddesignpatterns.com/2016/08/coloring-buttons-with-themeoverlays-background-tints.html:
Approach #1: Modifying the button’s background color w/ a ThemeOverlay
Approach #2: Setting the AppCompatButton’s background tint
When you use
android:background
, you are replacing much of the styling and look and feel of a button with a blank color.Update: As of the version 23.0.0 release of AppCompat, there is a new
Widget.AppCompat.Button.Colored
style which uses your theme'scolorButtonNormal
for the disabled color andcolorAccent
for the enabled color.This allows you apply it to your button directly via
If you need a custom
colorButtonNormal
orcolorAccent
, you can use aThemeOverlay
as explained in this pro-tip andandroid:theme
on the button.Previous Answer
You can use a drawable in your v21 directory for your background such as:
This will ensure your background color is
?attr/colorPrimary
and has the default ripple animation using the default?attr/colorControlHighlight
(which you can also set in your theme if you'd like).Note: you'll have to create a custom selector for less than v21:
Assuming you have some colors you'd like for the default, pressed, and focused state. Personally, I took a screenshot of a ripple midway through being selected and pulled the primary/focused state out of that.
If you're ok with using a third party library, check out traex/RippleEffect. It allows you to add a Ripple effect to ANY view with just a few lines of code. You just need to wrap, in your xml layout file, the element you want to have a ripple effect with a
com.andexert.library.RippleView
container.As an added bonus it requires Min SDK 9 so you can have design consistency across OS versions.
Here's an example taken from the libraries' GitHub repo:
You can change the ripple colour by adding this attribute the the RippleView element:
app:rv_color="@color/my_fancy_ripple_color
Use
backgroundTint
instead ofbackground
I tried this:
instead of changing background property. This does not remove the material effect.