Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar with a wrong height and shadow

I am trying to add shadow to a toolbar using elevation and the Design Library. The layout code is something like:

<android.support.design.widget.CoordinatorLayout ... >
  <android.support.design.widget.AppBarLayout ... >
    <android.support.design.widget.CollapsingToolbarLayout ... >
       <android.support.v7.widget.Toolbar
           android:id="@+id/app_bar"
           android:layout_width="match_parent"
           android:layout_height="?actionBarSize"
           app:contentInsetStart="16dp"
           android:background="@color/colorPrimary"
           android:elevation="16dp"
        />
    </android.support.design.widget.CollapsingToolbarLayout>
  </android.support.design.widget.AppBarLayout>

The complete application source code it is available on github.

The problem is that the toolbar height or the shadow are not behaving as I expect. If you watch the screen capture below, you can notice the problem.

What I need to do is to display the shadow below of the blue area.

Current Toolbar

Any hint is very appreciated.

like image 597
RobertoAllende Avatar asked Jan 23 '16 23:01

RobertoAllende


People also ask

How do I get rid of app Bar elevation?

You can remove the elevation from Material UI's AppBar by setting the elevation prop to 0.

How do I turn off Shadow Bar?

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 .

How do I get rid of the shadow bar on my Android?

By default, android provides shadow for action bar. This example demonstrates How to remove shadow below the action bar. Step 1 - Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 - Add the following code to res/layout/activity_main.

What is AppBarLayout?

AppBarLayout is a vertical LinearLayout which implements many of the features of material designs app bar concept, namely scrolling gestures. Children should provide their desired scrolling behavior through AppBarLayout.


2 Answers

As mentioned there, it's by implementation of CollapsingToolbarLayout - elevation is removed if CollapsingToolbarLayout shows non-pinned elements:

if (Math.abs(verticalOffset) == scrollRange) {
  // If we have some pinned children, and we're offset to only show those views,
  // we want to be elevate
  ViewCompat.setElevation(layout, layout.getTargetElevation());
} else {
  // Otherwise, we're inline with the content
  ViewCompat.setElevation(layout, 0f);
}

So, all I can suggest is to make your own CollapsingToolbarLayout by copying original CollapsingToolbarLayout from Google, and make changes in this condition.

like image 130
romtsn Avatar answered Oct 10 '22 00:10

romtsn


Move the elevation to the AppBarLayout. CollapsingToolbarLayout changes in size, so setting it on the AppBarLayout creates the shadow at the right position.

<android.support.design.widget.CoordinatorLayout ... >
<android.support.design.widget.AppBarLayout
      android:elevation="16dp">
  <android.support.design.widget.CollapsingToolbarLayout ... >
     <android.support.v7.widget.Toolbar ... />
  </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
like image 44
patloew Avatar answered Oct 10 '22 00:10

patloew