1

I was wondering if it's possible to create shadow effect for linear layout in android just like shown below. Any help would be appreciated...

Samuel Robert
  • 10,106
  • 7
  • 39
  • 60

2 Answers2

6

Then Use 9 Patch image for that,

9 patch image

for more detail here is question for shadow LinearLayout

Community
  • 1
  • 1
  • I've already tried the above code. Apparently, the above code adds just a border to my Linear Layout. What I really wanted was the blending shadow effect just like I've shown in the image. – Samuel Robert Jan 25 '16 at 05:43
  • @SamuelRobert see my update answer. –  Jan 25 '16 at 06:03
  • I appreciate your help. Nonetheless, elevation property would work only if the sdk level is 21 and above. I was trying to accomplish this effect from api level 11 onwards. – Samuel Robert Jan 25 '16 at 06:20
  • @SamuelRobert See the edit. –  Jan 25 '16 at 06:27
  • Buddy, The updated answer just adds a border below the linear layout. What I really need is the fading shadow effect not a border at the bottom. – Samuel Robert Jan 25 '16 at 06:32
  • go to the 9 patch image link. –  Jan 25 '16 at 06:34
  • Ya. you got the answer to my question. one more thing though, can we do this just with an xml drawable file?. or 9-patch is the best choice? – Samuel Robert Jan 25 '16 at 06:40
  • you are using api level 11 so it is difficult to make drawable xml of your shape shadow better use 9 patch image for that. –  Jan 25 '16 at 06:43
5

There are lots of ways and here they are Use that which one suits you

Create your own drawable

border.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="@android:color/darker_gray" />
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item android:right="1dp" android:left="1dp" android:bottom="2dp">
        <shape
            android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
</layer-list>

and your_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal"
   android:padding="10dp"
   android:background="@drawable/border"
  >
</LinearLayout>

You can also use use a drawable from android

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

or:

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

or:

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

Use a 9-patch image with a shadow and set it as the background to your Linear layout

Use this website to create 9 patch with shadow

http://inloop.github.io/shadow4android/

Harsh Sharma
  • 898
  • 1
  • 14
  • 30