Search code examples
androidandroid-toolbar

Set custom shadow to AppBarLayout or Toolbar


I want to set custom shadow to Toolbar. Currently is shown default shadow, but it not appropriate for me. So how can I set a custom shadow?

Activity layout:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarMain"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

</android.support.design.widget.AppBarLayout>

Solution

  • Finally I found it out. But this approach do not give the same shadow as BottomNavigationView has. This is a minor issue, but maybe you have suggestions?

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbarMain"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize" />
    
    </android.support.design.widget.AppBarLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/PageBackground">
    
        <FrameLayout
            android:id="@+id/frameContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@drawable/app_bar_shadow" />
    
    </RelativeLayout>
    

    app_bar_shadow.xml:

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:angle="270"
            android:startColor="#15000000"
            android:endColor="@android:color/transparent"
            android:type="linear" />
    </shape>