49

I have a rounded rectangle with elevation that casts a shadow, just like in the example here: http://developer.android.com/preview/material/views-shadows.html#shadows

Here is my shape:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/white" />
    <corners android:radius="6dp" />
</shape>

I want to get the nice "ripple" touch effect that everything else has, but when that's set as the view's background, no touch feedback is given. The shape stays white.

So, I made it into a layer-list:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <corners android:radius="6dp" />
        </shape>
    </item>
    <item android:drawable="?android:selectableItemBackground" />
</layer-list>

Now I get nice touch feedback when I tap it, but there's a problem.

The outline of the rounded rectangle shape was lost. It still draws the white rounded rectangle fine, but the shadow is cast as a non-rounded rectangle (square edges), and the ripple goes all the way out past the corners:

Bad corners

It doesn't look too horrible on here, but on the device it's pretty ugly and off-putting.

Is there a way to fix this? The Outline section of the first link seems to be what I want, but I can't figure out how to implement it.

4 Answers 4

80

Try that one:

ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="@color/COLOR_WHILE_PRESSING" xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:drawable="@drawable/background"></item>
</ripple>

background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <solid android:color="@color/BACKGROUND_COLOR" />
 <corners android:radius="6dp" />
</shape>

Or maybe this post will help you: Android L FAB Button shadow

I explained there, how to implement the new FAB button, I also used the outline for that.

3
  • 1
    The post you linked helped get the Outline working. Now it's perfect, thanks! Unfortunately putting the <shape> as an item in the ripple still makes it think it's a non-rounded rectangle. But using the outline method, it works fine. Jul 2, 2014 at 18:01
  • In case anyone else is trying this, I tried the same technique layer-list (referencing a layer-list and wrapping it in a ripple element) and it works nicely.
    – u2tall
    Oct 29, 2014 at 21:51
  • But what should be the Target and Min SDK ? Jul 15, 2015 at 15:05
10

This is with ripple and layer-list.

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorRipple">
<item>
    <layer-list>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#349F37" />
                <corners android:radius="6dp" />
                <stroke
                    android:width="2dp"
                    android:color="#50E751" />
            </shape>
        </item>
    </layer-list>
</item>

</ripple>
4
<RelativeLayout
 android:foreground="?android:attr/selectableItemBackground">
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
</RelativeLayout>

enter image description here

1
  • please provide description of your answer Aug 21, 2017 at 12:29
2

click here for complete source code

Create button.xml in drawable folder

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

Create button.xml in drawable-v21 folder

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:drawable="@color/colorPrimary" />
</ripple>

you can set button.xml as background of your view.

<TextView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:background="@drawable/button"
    android:clickable="true"
    android:gravity="center"
    android:padding="16dp"
    android:text="Android Ripple Effect Custom"
    android:textColor="#fff"
    android:textSize="18sp" />

if you want custom color for ripple effect instead of default gray, then you can archive it by adding colorControlHighlight in your style.

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorControlHighlight">@color/colorPrimaryDark</item>
    </style>

</resources>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.