Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlaying content above AppBarLayout using new Material Design

I want to achieve something like that. (not the FAB or the Snackbar). How can i create a layout, overlaying the AppBarLayout? Like this! (For Example)

AppBarLayout with overlaying content

Like Play Store:

Just like on the Play Store

My AppBarLayout with CoordinatorLayout and NestedScrollView with RelativeLayout as content looks like this:

<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:id="@+id/rootLayout" android:layout_width="match_parent" android:layout_height="match_parent">  <android.support.design.widget.AppBarLayout     android:layout_width="match_parent"     android:layout_height="@dimen/_118sdp"     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">      <android.support.design.widget.CollapsingToolbarLayout         android:id="@+id/collapsingToolbarLayout"         android:layout_width="match_parent"         android:layout_height="match_parent"         app:contentScrim="@color/mpc_pink"         app:expandedTitleMarginStart="@dimen/_40sdp"         app:layout_scrollFlags="scroll|exitUntilCollapsed">          <de.mypostcardstore.widgets.ItemImageView             android:id="@+id/header"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:scaleType="centerCrop"             android:src="@color/mpc_pink"             app:layout_collapseMode="parallax"             app:layout_collapseParallaxMultiplier="0.7" />          <android.support.v7.widget.Toolbar             android:id="@+id/article_toolbar"             android:layout_width="match_parent"             android:layout_height="?attr/actionBarSize"             android:minHeight="?attr/actionBarSize"             app:contentScrim="@color/mpc_pink"             app:layout_collapseMode="pin"             app:layout_scrollFlags="scroll|enterAlways"             app:popupTheme="@style/ThemeOverlay.AppCompat.Light"             app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />       </android.support.design.widget.CollapsingToolbarLayout>  </android.support.design.widget.AppBarLayout>  <android.support.v4.widget.NestedScrollView     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="?android:colorBackground"     android:fillViewport="true"     app:layout_behavior="@string/appbar_scrolling_view_behavior">      <RelativeLayout         android:layout_width="match_parent".....> 

It would be awesome if someone could help me out. I can not find anything on the internet...

Thanks in advance!

like image 429
X7S Avatar asked Jun 24 '15 16:06

X7S


2 Answers

Just add something like

app:behavior_overlapTop="64dp" 

to your NestedScrollView and it will be placed above the expanded toolbar.

In addition, you should add something like

app:expandedTitleMarginBottom="70dp" 

to your CollapsingToolbarLayout so the title does not appear under your overlaid scroll content.

like image 96
Ander Webbs Avatar answered Sep 28 '22 09:09

Ander Webbs


It's quite simple, really. You could achieve that by using a combination of ToolBar, FrameLayout, and your content view (could be a ListView like your first example, or anything).

The idea is to make your FrameLayout possess the same color as your ToolBar, giving the illusion of ToolBar being much larger than it is. Then all that is left to do is to make your content view be the last (or in API 21 and above: possess the highest elevation attribute) so that it would appear as if it floats above the aforementioned FrameLayout.

See my illustration below:

enter image description here

Now that you got the big idea, below is some real live XML snippet for doing such thing. (I actually use this layout in one of my apps) :

<!-- Somewhere in your layout.xml -->  ....      <android.support.v7.widget.Toolbar         android:id="@+id/tb_toolbar"         android:layout_width="match_parent"         android:layout_height="@dimen/abc_action_bar_default_height_material"         android:background="?attr/colorPrimary"         android:minHeight="?attr/actionBarSize"         app:contentInsetStart="72dp"         app:popupTheme="@style/ThemeOverlay.AppCompat.Light"         app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>      <!-- This is the 'faux' ToolBar I've been telling you about. This is the part that will be overlaid by the content view below. -->     <FrameLayout         android:id="@+id/v_toolbar_extension"         android:layout_width="match_parent"         android:layout_height="64dp"         android:layout_below="@+id/tb_toolbar"         android:background="?attr/colorPrimary"         android:elevation="2dp"/>      <!-- Normally, I use this FrameLayout as a base for inflating my fragments. You could just use put your content view here. -->     <FrameLayout         android:id="@+id/ly_content"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_below="@+id/tb_toolbar"         android:elevation="3dp"/>  .... 

Note that my ly_content has higher elevation value than that of v_toolbar_extension. This is what will give you that desired 'overlaid toolbar' effect.

Last but not least, you would want to add this line somewhere in your activity's onCreate() :

/* Assuming mToolbar exists as a reference to your ToolBar in XML. */ setSupportActionBar(mTbToolbar); getSupportActionBar().setElevation(0); 

What that codes woud do is to set your ToolBar elevation to zero; removing preset shadows that were given as a default to ToolBars. If you don't do this, said shadow will create a "seam" between your ToolBar and your FrameLayout, thus breaking the illusion of those two being the same.

p.s., It is also important to give your content view a padding on each side. Doing so, your content view will not cover the entire width of the screen (which would render this effect useless).


Note: I see some good answers here that mentioned the absence of FrameLayout and instead making the ToolBar taller. While in theory it might work as well as my proposed solution, you might have problems when trying to manipulate scrolling; by doing that, you won't be able to separate ToolBar and its extension. You'll be forced to either make the Toolbar static or scroll all of the ToolBar altogether (makes scrolling a bit weird).

Add to that, the fact that you can't easily assign a custom drawable into a Toolbar. Hence makes it hard to follow the Google Play example you've given above. While if you're using my solution, all you'd need to do is just make your Toolbar transparent and assign the drawable to the FrameLayout instead.

like image 36
Hadi Satrio Avatar answered Sep 28 '22 09:09

Hadi Satrio