I am working on grid layout using recyclerview in android. The grid occupies a portion of the screen and has a shadow. To get the desired shadow effect I am using an elevati
Just set below three property in your recyclerview
android:outlineProvider="bounds"
android:background="@null"
android:elevation="2dp"
The android:elevation does only apply shadows on devices which are running Lollipop or later. If you want to suppot older devices, you have to create a shadow yourself.
I found the answer after a little bit of searching from here. The problem was the transparent background. Elevation works with only non-transparent backgrounds on views. To fix it we should set android:outlineProvider="bounds"
on the view and android:clipToPadding="false"
on the view's parent.
Hope it helps someone.
For Lollipop and you can use the android:elevation property but below lollipop versions you have to give custom shadow so refer the below code for shadow
card_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#CABBBBBB"/>
<corners android:radius="2dp" />
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
Give this file as a background to your recyclerview inflater file it will work fine.