qwertz qwertz - 1 year ago 247
Android Question

Android ripple background color

I am using a ripple effect on my navigation drawer. I have set it like this and applied it to my ListView:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/black_200" />

<item android:state_pressed="true" android:color="@color/black_200">
<ripple android:color="@color/black_400" />

<item android:drawable="@color/white" />

I want the ripple background to remain the same like the normal activated state. Even when I define the color to be the same as the activated state, it gets darker and the ripple bubble gets even more dark. How can I color the background to be the same like the activated state and the ripple bubble to be the color I told it to be?

Answer Source

You can control the colour of the RippleDrawable by doing the following:

RippleDrawable rippleDrawable = (RippleDrawable)view.getBackground(); // assumes bg is a RippleDrawable

int[][] states = new int[][] { new int[] { android.R.attr.state_enabled} };
int[] colors = new int[] { Color.BLUE }; // sets the ripple color to blue

ColorStateList colorStateList = new ColorStateList(states, colors);

Or, via XML:

<?xml version="1.0" encoding="utf-8"?>
    android:color="#FFFF0000"> <!-- The ripple will be red -->

    <!-- the normal bg color will be light grey -->
        <color android:color="#FFDDDDDD" />

    <!-- make sure the ripple doesn't exceed the bounds -->
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="?android:colorAccent" />


After seeing @i.shadrin's answer below, I must admit it's a much simpler approach (using styles). If this is an option for you, I would recommend it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download