I’m trying to implement ripple effect in a RelativeLayout
on API 22 but it doesn’t show up. However the same ripple works in a Button
.
The code for my ripple drawable is as follows:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#d1c4e9">
<item android:id="@android:id/mask"
android:drawable="@android:color/white" />
<item android:drawable="@drawable/rect"/>
</ripple>
Code for Relative Layout is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/ripple">
</RelativeLayout>
After this the ripple is set as a background on Button
and RelativeLayout
. The ripple on button works great but it doesn’t show up on the RelativeLayout
at all.
Can anyone tell me what am I doing wrong?
Adding this attribute android:clickable="true"
works. Tested on Nexus 5
Answer:
In addition to what Rahunandan said, if you are using appcompat-v7 support library you also need to add
android:background="?attr/selectableItemBackground"
.
Answer:
This attributes in the layout.
android:background="?attr/selectableItemBackground"
android:clickable="true"
Answer:
In my case ripple effect is working after the first click, but for first click it didn’t work for me. Have changed the background selector file with android:state_activated=”true” and in main.xml android:clickable=”true” then it’s work fine for all time.
selector.xml (under res\drawable\selector.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@drawable/card_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:state_activated="true" android:drawable="@drawable/card_bg_focused" android:state_enabled="true" android:state_focused="true"/>
<item android:state_activated="true" android:drawable="@drawable/card_bg_selected" android:state_enabled="false" android:state_selected="true"/>
</selector>
In activity_main.xml
<com.mysample.RecyclingImageView
android:id="@+id/imageview_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/selector"
android:clickable="true"/>
Tags: androidandroid, layout