Page Banner

Turn off ToolBar elevation – Android Material Design

[fusion_text]

Hi everybody, this time I am going to show you a common problem I have faced while working with ToolBar in Android material design section.

I have created a Navigation Drawer activity using the new activity creation wizard available in Android Studio 1.4. When I use a fragment with TabLayout in that activity I got something like this-

The shadow below the ToolBar still remains which looks very ugly. After doing a lot of research I got the following solution.

Here is the auto generated piece of code for the ToolBar. You can find it inside app_bar_main.xml file.

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
android.support.design.widget.AppBarLayout>

Follow the following steps to remove the shadow under ToolBar-

Step 1: Add an id to AppBarLayout. e.g android:id=“@+id/mAppBarLayout”

<android.support.design.widget.AppBarLayout
     android:id="@+id/mAppBarLayout" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
android.support.design.widget.AppBarLayout>

Step 2: Inside your activity (e.g. MainActivity.java) class-

Create a new variable

private AppBarLayout mAppBarLayout;

Inside onCreate method add the following lines-

mAppBarLayout=(AppBarLayout)findViewById(R.id.mAppBarLayout);

Now, add the following line to remove elevation-

mAppBarLayout.setElevation(0);
Thats it. Cheers!
 

[/fusion_text]