How to make relative layout with box shadow

There are two ways to create shadow:

  1. If sdk version is lower than 5.0...

    A. You can also use android drawable image

    android:background="@android:drawable/dialog_holo_light_frame"
    

    B. Use below shadow_round.xml to make a drawable background for shadow of view. android:background="@drawable/shadow_round

     <!-- Drop Shadow Stack -->
     <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
            <corners android:radius="10dp" />
            <solid android:color="#00CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
            <corners android:radius="10dp" />
            <solid android:color="#10CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
            <corners android:radius="10dp" />
            <solid android:color="#20CCCCCC" />
        </shape>
    </item>
     <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
            <corners android:radius="10dp" />
            <solid android:color="#30CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="2dp" android:left="1dp" />
            <corners android:radius="10dp" />
            <solid android:color="#50CCCCCC" />
        </shape>
    </item> 
    
    <!-- Background -->
    <item>
    <shape>
        <solid android:color="#ffffff" />
        <corners android:radius="10dp" />
    </shape>
    </item>
    

  2. If you are using SDK 5.0 or greater then use the view's elevation property.

    android:elevation="2dp"
    

simple add elevation property in your relative layout. or create xml in drowable folder and call as a background

   <?xml version="1.0" encoding="utf-8"?>

       <shape  
             xmlns:android="http://schemas.android.com/apk/res/android"
             android:shape="rectangle">
       <solid android:color="#ddd" />
       <stroke
           android:width="5dp"
           android:color="#b8b9b9" />
       <corners
           android:radius="3dp" />
       </shape>

Tags:

Java

Android