1

So I've added Navigation Drawer and because of it I have to use Toolbar in my layout xml file (instead of using theme with action bar Theme.AppCompat.Light.DarkActionBar, and now it's Theme.AppCompat.Light.NoActionBar)

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>

</com.google.android.material.appbar.AppBarLayout>

I found this answer to add shadow below toolbar (AppBarLayout) https://stackoverflow.com/a/31026359/9766649

But it only works for Android 21+

With Theme.AppCompat.Light.DarkActionBar shadow is working for older Android

So the only solution is to use custom shadow like https://stackoverflow.com/a/26904102/9766649 ?

user155
  • 775
  • 1
  • 7
  • 25

1 Answers1

0

I decided to use different implementation for older and newer Android

For Android 21+ (layout-v21/activity_main.xml):

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <include layout="@layout/toolbar_view"/>

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content"/>

</LinearLayout>

For older Android (layout/activity_main.xml):

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/toolbar_view"/>

    <FrameLayout android:layout_width="match_parent"
                 android:layout_height="match_parent">

        <include layout="@layout/content"/>

        <View android:layout_width="match_parent"
              android:layout_height="5dp"
              android:background="@drawable/drop_shadow"/>

    </FrameLayout>

</LinearLayout>

toolbar_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppTheme.ActionBar"
    xmlns:android="http://schemas.android.com/apk/res/android"/>

drawable/drop_shadow.xml (to add elevation below toolbar for older Android):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <gradient android:startColor="@android:color/transparent"
              android:endColor="#88666666"
              android:angle="90"/>

</shape>
user155
  • 775
  • 1
  • 7
  • 25