androidkotlinmaterial-designshadowandroid-elevation

Android Elevation not working in LinearLayout Android


I have Linear Layout, inside that i have recycler view. I want elevation and corner radius but it not working properly. Can someone suggest me what i am doing wrong.

MainLayout

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:clipToPadding="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     <LinearLayout
        android:id="@+id/container"
        style="@style/WhiteBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/header">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

 </androidx.constraintlayout.widget.ConstraintLayout>

RecyclerView Item Layout

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:paddingStart="12dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    tools:ignore="RtlSymmetry">

  .....more elements..

</androidx.constraintlayout.widget.ConstraintLayout>

styles

<style name="WhiteBox" >
    <item name="android:background">@drawable/white_box_background</item>
    <item name="android:elevation">8dp</item>
</style> 

white_box_background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="7dp"/>
    <solid android:color="@android:color/white"/>
</shape>

As below image show what i am getting, corner and shadow also not showing properly enter image description here

What I want i.e. Expected Result

Expected Output


Solution

  • I would suggest you use card view instead.

    Add the dependency to your app module's build.gradle

    dependencies {
      implementation "androidx.cardview:cardview:1.0.0"
    }
    

    Replace your current LinearLayout snippet with

    <androidx.cardview.widget.CardView
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:orientation="vertical"
        app:cardCornerRadius="5dp"
        android:elevation="2dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/header">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </androidx.cardview.widget.CardView>
    

    for more info on CardView see https://developer.android.com/guide/topics/ui/layout/cardview#kts