1

My ToolBar disappears when setting elevation for AppBarLayout. Here's the layout.

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/appbar_height"
    app:elevation="0dp"
    android:background="@color/transparent">

    <android.support.v7.widget.Toolbar
        style="@style/ToolBarStyle"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp"
        android:background="@drawable/backgorund_toolbar_tranluscent"
        android:minHeight="@dimen/abc_action_bar_default_height_material" />
</android.support.design.widget.AppBarLayout>

I have tried values like 0dp, 0.1dp and 4dp for app:elevation. What's happening here? Is it a support library bug? I'm using 24.0.0.

Binoy Babu
  • 16,699
  • 17
  • 91
  • 134

2 Answers2

4

Answer from @Zeeshan is totally right.

as an extra here is a sample code that works

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            StateListAnimator stateListAnimator = new StateListAnimator();
            stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(appBarLayout, "elevation", 0.1f));
            appBarLayout.setStateListAnimator(stateListAnimator);
}

I had to set elevation to 0.1 because setting it to 0 wasn't working, the whole Layout was disappearing.

bisonfute
  • 91
  • 1
  • 9
3

New Update: In Appcompat v24.0.0, you can not set elevation to AppBarLayout using setElevation() and app:elevation as these are deprecated.

You have to use stateListAnimator property to set elevation now.

Note: set duration to 1ms in StateListAnimator in order to avoid delay in Elevation Drawing.

AppBarLayout elevation change is delayed on appCompat v24.0.0

Zeeshan Khan
  • 553
  • 7
  • 11
  • This method is deprecated. target elevation is now deprecated. AppBarLayout's elevation is now controlled via a StateListAnimator. If a target elevation is set, either by this method or the app:elevation attibute, a new state list animator is created which uses the given elevation value. – Binoy Babu Aug 16 '16 at 07:59
  • 1
    That doesn't explain how the entire toolbar disappears. It's supposed to work even if the method and attr is depreciated. – Binoy Babu Aug 16 '16 at 08:00