Remove android.widget.Toolbar shadow
Asked Answered
H

10

76

Using API21+ Toolbar:

// Toolbar
Toolbar toolbar = new Toolbar(this);
toolbar.showOverflowMenu();

Would like to remove its shadow completely. setElevation(0) doesn't do anything since getElevation() already returns 0.

There is Material Design reference:

https://material.io/guidelines/layout/structure.html#structure-toolbars

There is Develop Reference:

https://developer.android.com/reference/android/widget/Toolbar.html

But I don't see any info related to the shadow. Toolbar

Question: how to remove/hide Toolbar shadow completely?

Hewitt answered 6/4, 2017 at 12:39 Comment(1)
Did you try app:elevation="0dp" in your xml? please notice to app:.Caterinacatering
H
27

I have found solution myself:

getActionBar().setElevation(0);

More info:

https://developer.android.com/reference/android/app/Activity.html#getActionBar()

Hewitt answered 6/4, 2017 at 12:47 Comment(0)
C
146

Use app:elevation="0dp" instead of android:elevation on your toolbar. If it is not work, put your toolbar inside of a AppBarLayout and set app:elevation="0dp":

<android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="0dp">
            ...
</android.support.design.widget.AppBarLayout>
Caterinacatering answered 6/4, 2017 at 12:52 Comment(2)
using this toolbar completely diappearedHeadwork
aditionaly add android:translationZ="0.1dp" see https://mcmap.net/q/181777/-remove-shadow-below-appbarlayout-widget-androidTicino
Z
93

Use attribute app:elevation="0dp" to your Toolbar or AppBarLayout to remove the shadow.

#. If you are using Toolbar only, then add attribute app:elevation="0dp" to Toolbar.

    <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"
        app:elevation="0dp"/>

#. If you are using AppBarLayout as a container of Toolbar, then add attribute app:elevation="0dp" to AppBarLayout.

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

OUTPUT:

enter image description here

UPDATE:

If you want to remove it programmatically then you can use below code:

getSupportActionBar().setElevation(0);

Hope this will help~

Zamia answered 7/4, 2017 at 9:18 Comment(5)
I have a feeling its highly affected by the compiled sdk version...Still testing though whats your sdk version FAT? Coz am using 26.0.1Crist
app:elevation="0dp" works fine but android:elevation="0dp" not!Maxim
The question was for android.widget.Toolbar and not for android.support.v7.widget.Toolbar!Resonator
it completely removes the toolbar, not the shadowDarn
perfect answer!Beriosova
M
41

Instead of android:elevation try app:elevation:

<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"       
    app:elevation="0dp">
</android.support.design.widget.AppBarLayout>
Mcgean answered 6/4, 2017 at 12:55 Comment(0)
H
27

I have found solution myself:

getActionBar().setElevation(0);

More info:

https://developer.android.com/reference/android/app/Activity.html#getActionBar()

Hewitt answered 6/4, 2017 at 12:47 Comment(0)
A
16

If you are using AppBarLayout in toolbar, you can use: app:elevation="0dp" instead of android:elevation in AppBarLayout

Albemarle answered 4/3, 2019 at 11:45 Comment(0)
V
11
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  appBarLayout.outlineProvider = null
}
Vardar answered 2/12, 2018 at 3:48 Comment(0)
C
4
findViewById(R.id.appBarLayout).bringToFront();

Worked for me version issues I think Thanks Bjorn

in this question

Crist answered 4/10, 2017 at 20:12 Comment(0)
M
2

Put this code in Fragment in which you want to hide the ToolBar.

@Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity)getActivity()).getSupportActionBar().setElevation(0);
}
Marthena answered 26/4, 2019 at 10:20 Comment(0)
D
0

set "#00000000" to the backgroundColor of toolbar , remember that: if you use CoordinatorLayout and AppBarLayout ,you should remove the app:layout_behavior="@string/appbar_scrolling_view_behavior" of your contentView , I have made this elementary mistake.

Dissatisfactory answered 22/5, 2019 at 9:18 Comment(1)
if you are using flags u must use view_behaviourTrollop
M
0

When you add elevation 0dp on your appbarlayout, toolbar will be completely diappeared. So that you should also add translationZ in your xml.

android:translationZ="0.1dp"
app:elevation="0dp"

Than it will look like ->

  <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/toolbarLayout"
    android:background="@android:color/transparent"
    android:translationZ="0.1dp"
    app:elevation="0dp"
    android:theme="@style/ToolbarTheme"
    >

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />
</com.google.android.material.appbar.AppBarLayout>
Magnum answered 28/1, 2021 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.