Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ripple effect does not work in RelativeLayout

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?

like image 543
Varun Kumar Avatar asked Aug 11 '15 11:08

Varun Kumar


People also ask

What is a RelativeLayout?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

How do I set gravity center in RelativeLayout?

To center something in a RelativeLayout, you use android:layout_centerInParent="true" on the child. If you try to center several childs, they'll end up under/over each other.

What is RelativeLayout and LinearLayout?

Android Layout TypesLinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.

Is RelativeLayout deprecated?

@filthy_wizard: RelativeLayout is not deprecated.


2 Answers

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

like image 69
Raghunandan Avatar answered Oct 09 '22 11:10

Raghunandan


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

like image 41
Ali Kazi Avatar answered Oct 09 '22 10:10

Ali Kazi