Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 847
Mehdiway Avatar asked Jan 22 '13 16:01

Mehdiway


People also ask

What are android shadows?

Shadows are drawn by the parent of the elevated view, and thus subject to standard view clipping, clipped by the parent by default. Elevation is also useful to create animations where widgets temporarily rise above the view plane when performing some action.


2 Answers

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 ;)

like image 90
Mehdiway Avatar answered Oct 08 '22 01:10

Mehdiway


You can use the android.support.v4.view.ViewCompat class which sets the elevation of a view using the static method setElevation. The class is a helper for accessing features in view introduced after API Level 4 in a backwards compatible fashion.

The base elevation is in pixels eg

View mFab = (View) findViewById(R.id.floating_button);
ViewCompat.setElevation(mFab, 12);
like image 22
Ovokerie Ogbeta Avatar answered Oct 08 '22 02:10

Ovokerie Ogbeta