2

i want ripple effect and border around my cardview, and i have two xml file one for border and other is for riple effect, so how can i set on my cardview as background

here is my code

shape.xml(v17)

<?xml version="1.0" encoding="UTF-8"?> 
         <shape xmlns:android="http://schemas.android.com/apk/res/android">
                 <stroke android:width="1dp" android:color="#1B5E20" />
                 <corners android:radius="2dp"/>
                 <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
         </shape>

ripple.xml(v21)

<?xml version="1.0" encoding="utf-8"?>
 <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:attr/colorControlHighlight">
     <item
            android:id="@android:id/mask"
            android:drawable="@android:color/white">
     </item>
 </ripple>

and here is my cardview that i want to set both thing together..

cardview.xml

<?xml version="1.0" encoding="utf-8"?>
     <android.support.v7.widget.CardView
            android:layout_marginTop="10dp"
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android">

         <RelativeLayout
                android:background="@drawable/shape"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp">

             <TextView
                    android:textColor="#000"
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Title"
                    android:textSize="20sp" />

         </RelativeLayout>
     </android.support.v7.widget.CardView>

how can i set both file together bcz two background in on layout is not supported. help me, plz.

Sagar Chavada
  • 5,169
  • 7
  • 40
  • 67

3 Answers3

3

Do it using layer-list. cardview_style.xml

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <stroke android:width="1dp" android:color="#1B5E20" />
        <corners android:radius="2dp"/>
        <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
    </shape>
</item>
<item>
    <ripple
        xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
        <item
        android:id="@android:id/mask"
        android:drawable="@android:color/white"></item>
    </ripple>
</item>

Then add following file to background attribute.

Bhagirathsinh Gohil
  • 673
  • 1
  • 9
  • 25
1

Create a new XML with a layer-list and add your shape and ripple in it

Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
1

Just like Iulian Popescu said you can use layer-list, what seems to be best option. But if you insist to keep that xmls them separatly, than you can always merge them in your code using canvas. Use this topics if you want to use this techniques:

How to merge bitmaps

Converting drawables to bitmaps

Community
  • 1
  • 1