How To Make Android Toolbar Elevation Working On Pre-Lollipop Devices

Step 1.



Step 2.

Create a new drawable desource file  "shadow.xml".

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
  3.     <gradient
  4.         android:startColor="@android:color/transparent"
  5.         android:endColor="#40000000"
  6.         android:angle="90"/>
  7. </shape>

Step 3.

Open "activity_main.xml" and add these lines between "include" and "RelativeLayout" Tags.

  1. <FrameLayout
  2.         android:layout_width="fill_parent"
  3.         android:layout_height="4dp"
  4.         android:background="@drawable/shadow"
  5.         android:layout_below="@+id/toolbar"
  6.         android:visibility="invisible" />
Now your "activity_main.xml" should look like this.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     tools:context=".MainActivity">
  6.     <include
  7.         android:id="@+id/toolbar"
  8.         layout="@layout/toolbar"/>
  9.     <FrameLayout
  10.         android:layout_width="fill_parent"
  11.         android:layout_height="4dp"
  12.         android:background="@drawable/shadow"
  13.         android:layout_below="@+id/toolbar"
  14.         android:visibility="invisible" />
  15.         <RelativeLayout
  16.             android:layout_width="fill_parent"
  17.             android:layout_height="fill_parent"
  18.             android:layout_alignParentLeft="true"
  19.             android:layout_alignParentStart="true"
  20.             android:layout_below="@+id/toolbar"
  21.             android:layout_alignParentRight="true"
  22.             android:layout_alignParentEnd="true"
  23.             android:layout_alignParentBottom="true"
  24.             android:paddingLeft="@dimen/activity_horizontal_margin"
  25.             android:paddingTop="@dimen/activity_vertical_margin"
  26.             android:paddingRight="@dimen/activity_horizontal_margin"
  27.             android:paddingBottom="@dimen/activity_vertical_margin">
  28.             <TextView
  29.                 android:layout_width="wrap_content"
  30.                 android:layout_height="wrap_content"
  31.                 android:text="@string/hello_world"
  32.                 android:id="@+id/textView16" />
  33.         </RelativeLayout>
  34. </RelativeLayout>

Finally.

Run your application.


Comments

Popular posts from this blog

How To Make Material Design Actionbar/Toolbar In 7 Simple Steps.