Neanderthal
Neanderthal

Reputation: 947

How to properly set elevation value to recyclerview?

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" />
 ......

Upvotes: 18

Views: 26877

Answers (3)

Berkay92
Berkay92

Reputation: 588

Just set below three property in your recyclerview

android:outlineProvider="bounds"
android:background="@null"
android:elevation="2dp"

Upvotes: 4

Neanderthal
Neanderthal

Reputation: 947

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.

Upvotes: 28

Daniel Zolnai
Daniel Zolnai

Reputation: 16910

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.

Upvotes: 0

Related Questions