简体   繁体   中英

How to put ripple effect on relative layout in android?

I have created my custom button for my card-view in android using Relative Layout and calling that button via <include layout functionality. Everything is working fine I just want to know put ripple effect on that click how to do that?

Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="125dp"
        android:layout_height="30dp"
        android:background="@color/colorPrimary"
        android:layout_gravity="center_horizontal"
        android:focusableInTouchMode="true">


        <TextView
            android:layout_width="125dp"
            android:layout_height="30dp"
            android:fontFamily="@font/questa"
            android:text="Know More"
            android:gravity="center"
            android:textAllCaps="false"
            android:textColor="#ffffff">
        </TextView>

    </FrameLayout>

</RelativeLayout>

Include Layout Code:

<include
                android:id="@+id/btnViewDetails"
                style="@style/CardView.Button"
                layout="@layout/button"
                android:layout_width="125dp"
                android:layout_height="30dp"
                android:layout_below="@+id/viewDivider"
                />

You have to add ripple to your background parent.

suppose that you have a ripple file like this, file name is ripple in drawable :

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#9D9D9D">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#9D9D9D" />
            <corners android:radius="20dp" />
        </shape>
    </item>

    <item android:drawable="@drawable/drawable_all_rows" />
</ripple>

*** The first item add effect to your component or button and the second item is state that you didnt click on button .

drawable_all_rows :

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

    <solid android:color="#ffffff" />
    <corners android:radius="20dp" />

</shape>

Then you add this to your relative layout background like this:

you just add 3 line code in RelativeLayout

android:clickable="true"
android:focusable="true"
android:foreground="?selectableItemBackground"
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorPrimaryDark">
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimaryDark" />
            <corners android:radius="@dimen/_4sdp" />
        </shape>
    </item>

<item android:id="@android:id/background">
    <shape android:shape="rectangle">
        <gradient
            android:angle="90"
            android:endColor="@color/blue"
            android:startColor="@color/colorPrimary"
            android:type="linear" />
        <corners android:radius="@dimen/_4sdp" />
    </shape>
</item>

I just use 1 line

android:background="?selectableItemBackground"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM