In this blog,
I have shown you how to make a drop shadow in the toolbar below API 21.
Implementation
In this example, I have created a custom background XML(drop shadow) and applied it inside the FrameLayout.
And this will give a drop shadow effect on the toolbar.
toolbar.xml
1 2 3 4 5 6 7 8 9 |
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways|snap" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:titleTextColor="#FFFFFF" /> |
your_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> </android.support.design.widget.AppBarLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Place Your Layout Here --> <View android:id="@+id/shadow_view" android:layout_width="match_parent" android:layout_height="5dp" android:background="@drawable/dropshadow" /> </FrameLayout> </LinearLayout> |
dropshadow.xml
1 2 3 4 5 6 7 8 |
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="@android:color/transparent" android:endColor="#60555555" android:angle="90"/> </shape> |
Note: Don’t forget to hide your view if the device you are running is >= to Lollipop.
if not you’ll end up with two shadows.
1 2 3 |
if (android.os.Build.VERSION.SDK_INT >= 21) { findViewById(R.id.shadow_view).setVisibility(View.GONE); } |