Adding ripple effect for View in onClick

create ripple background

view_background.xml

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

view_noraml.xml //this is how you view appears in normal

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:radius="@dimen/button_corner"/>
    <solid
        android:color="@android:color/transparent"/>
    <stroke
        android:width="0.5dp"
        android:color="@color/white"/>

</shape>

now set the view_background to your view

example

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:foreground="@drawable/view_background"
    android:clickable="true"
    android:focusable="true"
    >
    <ImageView
        android:id="@+id/grid_item_imageView"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_gravity="center"
        android:scaleType="centerInside"
        />
</FrameLayout>

I know this is a pretty old thread but just in case the above answers didn't work, I'd recommend you to try the below code as it worked for me:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

The key thing to notice are the clickable and focusable.


Clickable Views

In general, ripple effect for regular buttons will work by default in API 21 and for other touchable views, it can be achieved by specifying

android:background="?android:attr/selectableItemBackground"

In code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    View myView = findViewById(R.id.myView);
    int[] attrs = new int[]{R.attr.selectableItemBackground};
    TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
    int backgroundResource = typedArray.getResourceId(0, 0);
    myView.setBackgroundResource(backgroundResource);
}

As stated in Lubomir Babev's answer, adding android:background="?android:attr/selectableItemBackground" does the trick.

However, if your view already has a background, you can use the same on the android:foreground attribute instead:

android:background="@color/anyColor"
android:foreground="?android:attr/selectableItemBackground"

android:foreground is only supported by API 23+ though.