Search code examples
javaandroidxmltoolbarelevation

How to remove elevation under the toolbar on all activites


I'm trying to get rid of the elevation under the toolbar by using app:elevation="0dp" but it only works in the main activity xml. When I go to other activities I can still see the elevation, any ideas how to completely get rid of the elevation under my toolbar?

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout    
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.khalid.myapplication3.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:elevation="0dp">

    <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>


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

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

this is what I got after including toolbar in other activities layouts

enter image description here


Solution

  • I ran into this problem before, and it looks like you're using a theme that doesn't extend @style/Theme.AppCompat.NoActionBar. When you're using a Toolbar in your XML layout, you have to hide the default ActionBar so they don't conflict.

    In your AndroidManifest.xml, make sure you set the theme for the activity you're having a problem with:

    <application ...>
        <activity ...
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
    </application>