Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" />
 ......
like image 872
Neanderthal Avatar asked Oct 29 '15 11:10

Neanderthal


People also ask

How do I set elevation in android?

To set the default (resting) elevation of a view, use the android:elevation attribute in the XML layout. To set the elevation of a view in the code of an activity, use the View. setElevation() method. To set the translation of a view, use the View.

How do you make a RecyclerView more performant?

If the size of ImageView in RecyclerView items is not fixed then RecyclerView will take some time to load and adjust the RecyclerView item size according to the size of Image. So to improve the performance of our RecyclerView we should keep the size of our ImageView to make it RecyclerView load faster.

Which RecyclerView method is called when getting the size of the data set?

getItemCount() : RecyclerView calls this method to get the size of the data set. For example, in an address book app, this might be the total number of addresses. RecyclerView uses this to determine when there are no more items that can be displayed.


3 Answers

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.

like image 26
Sachin Mandhare Avatar answered Oct 26 '22 10:10

Sachin Mandhare


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.

like image 126
Neanderthal Avatar answered Oct 26 '22 09:10

Neanderthal


Just set below three property in your recyclerview

android:outlineProvider="bounds"
android:background="@null"
android:elevation="2dp"
like image 29
Berkay92 Avatar answered Oct 26 '22 10:10

Berkay92