Skip to content

Instantly share code, notes, and snippets.

@chandruscm
Last active August 14, 2019 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chandruscm/03756a5ca3ba520d828615453de414d4 to your computer and use it in GitHub Desktop.
Save chandruscm/03756a5ca3ba520d828615453de414d4 to your computer and use it in GitHub Desktop.
activity_menu_app_bar_1.xml produces the desired effect, but the toolbar moves up: as per the height set in the empty CollapsingToolbarLayout before adding the elevation. activity_menu_app_bar_2.xml does not work, it adds elevation by default without scrolling.
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:id="@+id/activity_menu_app_bar">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:minHeight="0.1dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"/>
<include layout="@layout/activity_menu_toolbar"/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:id="@+id/activity_menu_app_bar">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="1dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<include layout="@layout/activity_menu_toolbar"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_menu_toolbar">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_normal"
android:background="?android:selectableItemBackgroundBorderless"
android:src="@drawable/ic_search"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:id="@+id/activity_menu_toolbar_search_button"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="@dimen/font_large"
android:textStyle="bold"
android:textAllCaps="true"
android:textColor="@color/textLight"
android:layout_centerInParent="true"
android:id="@+id/activity_menu_toolbar_title_text"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_normal"
android:background="?android:selectableItemBackgroundBorderless"
android:src="@drawable/ic_settings"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:id="@+id/activity_menu_toolbar_bookmark_button"/>
<View
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_alignParentBottom="true"
android:background="@color/menuDivider"/>
</RelativeLayout>
@chandruscm
Copy link
Author

activity_menu_app_bar_1.xml produces the desired effect, but the toolbar moves up: as per the height set in the empty CollapsingToolbarLayout before adding the elevation.

activity_menu_app_bar_2.xml does not work, it adds elevation by default without scrolling.

@nickbutcher
Copy link

try

<CoordinatorLayout>
  <AppBarLayout>
    <Toolbar />
    <android.support.v4.widget.Space
            android:layout_width="0dp"
            android:layout_height="1px"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:minHeight="0px" />
  </AppBarLayout>
  ...
</CoordinatorLayout>

You'll get a 1px "collapse" but the Toolbar above won't move.

@chandruscm
Copy link
Author

For some reason, it works only when I place the Space above the Toolbar and that again moves the Toolbar. Placing the Space below the Toolbar does not set any elevation at all. I tested it on a device and the emulator, both on API 25.

I enabled layout bounds and I noticed the Space wasn't collapsing, it stays at the bottom. I also tried wrapping the Toolbar and Space in a vertical linear layout and tried using a plain View instead of Space. But no luck :(

@AndroidDeveloperLB
Copy link

Say, why does AppBarLayout have a default stateListAnimator ? When does it ever get changed? Which states does it have, by default?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment