Ripple effect does not work in RelativeLayout
Asked Answered
A

5

15

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?

Ardolino answered 11/8, 2015 at 11:17 Comment(11)
Where is this ripple object class coming from?Ardine
Is the drawable in drawable-v21?Contrecoup
Holmes: sorry, i didn't get your question?Ardolino
Raghunandan: yes, its in drawable-v21 and works perfectly for a button but not for relative layoutArdolino
@VarunKumar what happens when you click on RelativeLayout. Do you any other child views taking focus??. Post your relative layout code. the xml. lets see if we can reproduce your problemContrecoup
@Contrecoup Nothing happens when I click on the RelativeLayout.Ardolino
@VarunKumar post your codes.Contrecoup
@Contrecoup I even get the background color which I have set by help of rect drawable inside the ripple drawable given in the code above. But ripple does not take place when I click on the relative layoutArdolino
@Contrecoup code posted above.Ardolino
Let us continue this discussion in chat.Contrecoup
refer my answer belowBandicoot
C
35

Adding this attribute android:clickable="true" works. Tested on Nexus 5

Contrecoup answered 11/8, 2015 at 11:47 Comment(0)
W
9

In addition to what Rahunandan said, if you are using appcompat-v7 support library you also need to add android:background="?attr/selectableItemBackground" .

Witchy answered 7/1, 2016 at 4:23 Comment(3)
how are you supposed to add another background attribute when you already have one???Algebraic
If you already have a background attribute you can set a foreground attribute instead. Try experimenting with what works best for your UI. Try setting the existing background as foreground and the selectableItemBackground as background. If setting foreground doesnt work for you, you can add a LinearLayout outside of your view and set android:background="?attr/selectableItemBackground" on the LinearLayout. Hope that makes sense.Witchy
you're right, it does help big time! So what I did was just to set it as my foreground, I have actually done that before but the color I used was not obvious, now It does. Thanks.Algebraic
B
8

This attributes in the layout.

android:background="?attr/selectableItemBackground"
android:clickable="true"
Brainstorming answered 3/11, 2017 at 14:11 Comment(0)
B
1

For me, this works (API 23 and above)

    android:background="@drawable/your_background"
    android:clickable="true"
    android:foreground="?attr/selectableItemBackground"
Baneberry answered 8/12, 2021 at 8:57 Comment(0)
B
0

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"/>
Bandicoot answered 2/6, 2017 at 14:51 Comment(1)
what do you mean? there doesn't seem to be a ripple here.Thyrotoxicosis

© 2022 - 2024 — McMap. All rights reserved.