16

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 elevation value of 12 dp. But it does not seem to work as I cannot see any elevation (shadow) of the grid. Why is this happening? Does recyclerview not support elevation?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/activity_grid_layout"
android:background="@drawable/gradient"
android:layout_height="match_parent"
tools:context="com.mindhive.mindhive.activities.GridActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/grid_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="110dp"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="80dp"
    android:background="@color/transparent"
    android:elevation="12dp"
    android:scrollIndicators="none"
    android:scrollbars="none"
    android:padding="0dp" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/grid_recycler_view"
    android:layout_alignStart="@+id/grid_recycler_view"
    android:layout_marginBottom="-18dp"
    android:layout_marginStart="67dp"
    android:src="@drawable/main_filter"
    android:elevation="1dp" />
 ......
Neanderthal
  • 857
  • 1
  • 7
  • 25

4 Answers4

24

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.

Community
  • 1
  • 1
Neanderthal
  • 857
  • 1
  • 7
  • 25
  • 1
    These two attributes worked for me, but I needed to add it for the View itself, and not it's parent to make it work – Louis CAD Jan 04 '16 at 11:50
2

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.

Sachin Mandhare
  • 658
  • 2
  • 7
  • 19
1

Just set below three property in your recyclerview

android:outlineProvider="bounds"
android:background="@null"
android:elevation="2dp"
Berkay92
  • 422
  • 4
  • 20
0

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.

Daniel Zolnai
  • 14,536
  • 7
  • 52
  • 64
  • My device runs on android 5.1.1, so shadows should have worked perfectly. – Neanderthal Oct 29 '15 at 12:30
  • 1
    What happens if you use `android:background="@android:color/white"` instead of `android:background="@color/transparent"`? By the way, there's a built-in color for transparent: `"@android:color/transparent"` – Daniel Zolnai Oct 29 '15 at 13:05
  • Yeah that would fix it. Thanks for pointing to the android transparent resource. I found the problem and fixed with the help of [link](http://stackoverflow.com/questions/27477371/android-elevation-not-showing-a-shadow) . I was setting the background to be transparent which does not cast any shadow. – Neanderthal Oct 29 '15 at 13:27
  • Feel free to post an answer with the solution you found to your answer, and mark it as accepted, for future visitors. – Daniel Zolnai Oct 29 '15 at 13:35
  • I have posted a related question [here] (http://stackoverflow.com/questions/33500592/multiple-layers-of-shadows-in-android-recyclerview-elevation). Please comment if you have any insights. Thanks. – Neanderthal Nov 03 '15 at 13:43