I have a straight forward CollapsingToolbarLayout. It works fine so far but if I collapse the toolbar the title's position isn't centered vertically.
Here is my layout:
<android.support.design.widget.CoordinatorLayout 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="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/appbar_header_height_expanded"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/placekitten_1"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text" />
</android.support.v4.widget.NestedScrollView>
Have a look on my screenshots, so you can see how it behaves at the two states.
Here is the collapsed Toolbar's problem with the title.
I have this in my BaseFragment to set my toolbar at the actual Fragment:
protected void setToolbar(View view, int resource, String title, String subtitle) {
Toolbar toolbar = view.findViewById(resource);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);
((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle(subtitle);
}
So I found out,.. If I show a Snackbar then the title jumps to the right position and the expand/collaps works fine! So any ideas why it works after showing a Snackbar?
View view = findViewById(R.id.content_frame);
Snackbar mySnackbar = Snackbar.make(view, text, Snackbar.LENGTH_LONG);
mySnackbar.getView().setBackgroundColor(getResources().getColor(color));
mySnackbar.show();
And content_frame.xml is the root layout for the main activity:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" />
any ideas?
The Problem is the android:fitsSystemWindows="true"
in the root CoordinatorLayout
. Remove that attribute from the root layout and the title behaves well.
I tried solution at the end of this link: http://www.solutionscan.org/43500-android and it worked for me...
override fun onActivityCreated(savedInstanceState: Bundle?) {
// ...
collapsing_toolbar.post { collapsing_toolbar.requestLayout() }
}
The only difference is that I am using fragments and new navigation component...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With