Stack Overflow Asked by Eng. Samer T on February 15, 2021
I want to build layout like the one bellow:
The default layout is something like:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:toolbarId="@+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed|snap"
app:contentScrim="?attr/colorPrimary">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="500dp"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
app:srcCompat="@drawable/lionking" />
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:menu="@menu/my_menu"
app:layout_collapseMode="pin"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<include layout="@layout/item_nested_scrollview"/>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
now to change the toolbar location to be bellow the imageView I created new file with linearlayout (vertical orientation) and put Imageview and toolbar inside it, so the above code becomes like:
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:toolbarId="@+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed|snap"
app:contentScrim="?attr/colorPrimary">
<include layout="@layout/toolbar_in_middle"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<include layout="@layout/item_nested_scrollview"/>
</androidx.core.widget.NestedScrollView>
The LinearLayout file “toolbar_in_middle.xml” which is included in the above file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed|snap"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="500dp"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
app:srcCompat="@drawable/lionking" />
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:menu="@menu/my_menu"
app:layout_collapseMode="pin" />
</LinearLayout>
the layout display toolbar in the middle of layout as wanted, but the behavior does not work correctly, the ImageView doesn’t work with parralax mode, also the toolbar doesn’t acts as supposed to act. although there is app:layout_scrollFlags attributes for both.
the final goal is to build layout, its behavior smillar to layout in Samsung weather app.
As CollapsingToolbarLayout
is basically a FrameLayout
, child views will be placed by default at the top but you can simply set layout_gravity
to change this behavior.
Other things look good in your layout. But some other changes are listed below that I thought you may need:
colorPrimary
is transparent by default, so create a color resource to have the transparency desired in the screenshot.exitUntilCollapsed
flag is included as scroll flags of the collapsing toolbar.app:titleEnabled="false"
.So the layout would look like:
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:toolbarId="@+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed|exitUntilCollapsed|snap"
app:contentScrim="?attr/colorPrimary"
app:titleEnabled="false">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="500dp"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
app:srcCompat="@drawable/ic_launcher_foreground" />
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
android:background="@color/color_primary_half_transparent"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:layout_gravity="bottom"
app:title="Test"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
And color/color_primary_half_transparent.xml
would look like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.5" android:color="?colorPrimary" />
</selector>
Correct answer by momt99 on February 15, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP