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 ?
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.
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).
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 ;)
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With