How to achieve shadow over the card view?

Viewed 198

I am trying to achieve following layout :

enter image description here

  1. I want to expand each view on click.
  2. How do I obtain the shadow over each view?

I have tried the following drawable :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

  <item>
    <shape android:shape="rectangle">

      <gradient
          android:startColor="#cacaca"
          android:centerColor="#b6b6b6"
          android:endColor="#e9e9e9"
          />
    </shape>

    <gradient
        android:startColor="#6586F0"
        android:centerColor="#D6D6D6"
        android:endColor="#4B6CD6"
        android:angle="90"/>
  </item>

  <item
      android:left="0dp"
      android:right="0dp"
      android:top="5dp"
      android:bottom="2dp">
    <shape android:shape="rectangle">
      <solid android:color="#ffffff"/>
      <corners android:radius="20dp"/>
    </shape>
  </item>
</layer-list>

But it is showing following view :

enter image description here

Please help me to achieve the proper view.

2 Answers

you can achieve this by nesting multiple cardViews inside the FrameLayout. Change the android:layout_marginTop and app:cardElevation properties to give this stack effect.

Try below code :

For Drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Drop Shadow Stack -->
    <item>
        <shape android:shape="rectangle">
            <padding
                android:left="1dp"
                android:right="1dp"
                android:top="1dp" />
            <corners android:topLeftRadius="20dp"
                android:topRightRadius="20dp"/>
            <solid android:color="#14000029" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <padding
                android:left="1dp"
                android:right="1dp"
                android:top="1dp" />
            <corners android:topLeftRadius="20dp"
                android:topRightRadius="20dp"/>
            <solid android:color="#14000029" />
        </shape>
    </item>
    <item>
        <shape
            android:shape="rectangle">
            <padding
                android:left="1dp"
                android:right="1dp"
                android:top="1dp" />

            <corners android:topLeftRadius="20dp"
                android:topRightRadius="20dp"/>
            <solid android:color="#14000029" />
        </shape>
    </item>
    <!-- Background -->
    <item>
        <shape
            android:shape="rectangle">
            <corners android:topLeftRadius="20dp"
                android:topRightRadius="20dp"/>
            <solid android:color="@android:color/white"/>
        </shape>
    </item>
</layer-list>

Code in Layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
    tools:context=".CardViewShadowActivity"
    android:layout_marginTop="30dp">
    <LinearLayout
        android:id="@+id/linear_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="30dp"
        android:background="@drawable/custom_bg">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linear_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="30dp"
        android:background="@drawable/custom_bg"
        android:layout_below="@id/linear_1"
        android:layout_marginTop="-20dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linear_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="30dp"
        android:background="@drawable/custom_bg"
        android:layout_below="@id/linear_2"
        android:layout_marginTop="-20dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linear_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="30dp"
        android:background="@drawable/custom_bg"
        android:layout_below="@id/linear_3"
        android:layout_marginTop="-20dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"/>
    </LinearLayout>
</RelativeLayout>

Customize layout and drawable according to your need.

Output for above code is:

enter image description here

I hope this helps you

Related