Custom Transitions in Android M using RippleDrawables

By | March 20, 2016

Ripple Drawables.

Touch feedback in material design provides an instantaneous visual confirmation at the point of contact when users interact with UI elements. The default touch feedback animations for buttons use the new RippleDrawable class, which transitions between different states with a ripple effect.

We can either apply RippleDrawable in XML or using Drawable XML or using java.

Using XML

create a file named “ripple.xml” in the drawable folder

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android=""
    android:color="?android:colorControlHighlight" />


Ripple animation extends beyond the bounds of its view, i.e the animation goes beyond the view to which it is applied, We will try to set the animation inside the View using the following methods.

Modify the ripple.xml like this.

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android=""
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="?android:colorAccent" />

and Apply it as background of your View like this.

	android:text="Button with Ripple Effect BorderLess" />

Another way to set the Ripple Effect within the View is to make a shape drawable and remove the “@android:id/mask”.

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android=""
    <shape android:shape="rectangle">
        android:width="1dp" />

Java methods to attain Ripple Effect in Views.

1. Method 1

	Button btn = (Button) findViewById(;
	RippleDrawable ripple = (RippleDrawable) btn.getBackground();
	GradientDrawable rippleBackground = (GradientDrawable) ripple.getDrawable(0);

2. Method 2

	Button btn2 = (Button) findViewById(;
	Drawable myIcon = getResources().getDrawable(R.drawable.btn2);
	RippleDrawable rippleDrawable = new RippleDrawable(new ColorStateList(new int[][]{{}},
			new int[]{Color.GREEN}), myIcon, null);

3. Method 3

	Button btn3 = (Button) findViewById(;
	rippleDrawable = new RippleDrawable(ColorStateList.valueOf(Color.RED),
			myIcon, null);

4. Method 4

	Button btn4 = (Button) findViewById(;
	RippleDrawable mRipple = new RippleDrawable(new ColorStateList(
							 new int[][]{ {} },
							 new int[]{
							new ColorDrawable(Color.GREEN), null);

You can download the complete Android Studio Source Code from here.

Website Pin Facebook Twitter Myspace Friendfeed Technorati Digg Google StumbleUpon Premium Responsive

Leave a Reply

Your email address will not be published. Required fields are marked *