Mehdiway Mehdiway - 2 months ago 42
Android Question

Simple Layout Shadow in Android

So I've been searching the web for a way to display a simple shadow for a layout, but there is no proper way to do that.

All I found was a workaround where you create a layout behind the one you want a shadow to be applied to, and then tweak it to be transparent and some other stuff.

Is there any other way to have a simple layout shadow without adding a whole new layout ?

Answer

I've been able to come up with a solution to this problem, and that by adding a View below our famous layout, displaying a gradient from one color to another.
Usually, the First color would be some sort of dark grayish, and the Second one would be the color of the background (in my case, i'll be having a light gray background so it's not completely white).

Layout + View

The xml would go like this :

...
<LinearLayout
    android:id="@+id/headerLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/headerImage"
        android:orientation="vertical" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="5dip"
        android:background="@drawable/drop_shadow" >
    </View>
</LinearLayout>
...

drop_shadow.xml :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">    
    <gradient
        android:startColor="#404040"
        android:endColor="#F1F1F1"
        android:angle="270"
        >
    </gradient>
</shape>

I hope it'll help ;)