Ripple Effect (Touch animation) in Android for Button or Layout


How to achieve ripple effect or ripple animation using support library in Android?

Ripple effect is the part of material design and added in API level 21. It gives the beautiful animation when a button or a layout is touched.

Android Ripple Effect Close View

Here we'll implement ripple effect for:
  • Button 
  • LinearLayout and 
  • FrameLayout

First of all we need to create a file colors.xml (if not present) in app -> res -> values to define all the necessary colors
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="white">#FFFFFF</color>
    <color name="colorRipple">#A9A9F5</color>
</resources>

Now we need to create a drawable resource file and name it ripple_effect as shown below

Android Drawable resource file



Add the below code to your ripple_effect.xml (v21) file (for API level 21)
<?xml version="1.0" encoding="utf-8"?>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorRipple">     <!-- ripple color -->

    <!-- for Button -->
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="3dp" />
            <solid android:color="@color/colorPrimary"/>
        </shape>
    </item>

</ripple>

Add the below code to your ripple_effect.xml file (for API level below 21)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="3dp" />
            <solid android:color="@color/colorRipple" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <corners android:radius="3dp" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="3dp" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </item>

</selector>


Now add a Button, LinearLayout and FrameLayout to your layout file and set background as drawable resource file ripple_effect.xml

Adding Button
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/refresh"
        android:textColor="@color/white"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/ripple_effect" />

Adding LinearLayout
    <LinearLayout
        android:id="@+id/llInviteOthers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="8dp"
        android:clickable="true"
        android:background="@drawable/ripple_effect">

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@mipmap/ic_refresh"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/refresh"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:textColor="@color/white"
            android:layout_gravity="center_vertical"/>

    </LinearLayout>

Adding FrameLayout
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="8dp"
        android:clickable="true"
        android:background="@drawable/ripple_effect">

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@mipmap/ic_refresh"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/refresh"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:textColor="@color/white"
            android:layout_gravity="center_vertical"/>

    </FrameLayout>


Preview Screens
Android Ripple Effect for FrameLayoutAndroid Ripple Effect for ButtonAndroid Ripple Effect for LinearLayout


SHARE
    Blogger Comment
    Facebook Comment

11 comments:

  1. Very nice tutorial. keep sharing <a href="http://blog.nkdroidsolutions.com/android-ripple-effect-example-code/" target="_blank" rel="dofollow”></a>

    ReplyDelete
  2. It is evident from your code that different button states can have different colors, but in ripple drawable you have not mentioned any states to provide different colors. How is this handled in a ripple tag?

    ReplyDelete
  3. if you have a constrant layout, this seem not to work am i missing something?

    ReplyDelete
  4. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.is article.

    angularjs online training

    apache spark online training

    informatica mdm online training

    devops online training

    aws online training

    ReplyDelete
  5. A universal message I suppose, not giving up is the formula for success I think. Some things take longer than others to accomplish, so people must understand that they should have their eyes on the goal, and that should keep them motivated to see it out til the end.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  6. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.

    Best PHP Training Institute in Chennai|PHP Course in chennai
    Best .Net Training Institute in Chennai
    Dotnet Training in Chennai
    Dotnet Training in Chennai

    ReplyDelete
  7. Thanks for your information's...IT sector has an Very god opinion about Java...looking towards to explore more about JAVA visit here and Grasp it...
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  8. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    Java Training in Chennai

    Java Training in Velachery

    Java Training in Tambaram

    Java Training in Porur

    Java Training in OMR

    Java Training in Annanagar


    ReplyDelete