deadpoint
deadpoint

Reputation: 453

CollapsingToolbarLayout collapsed title has bad position

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.

expanded toolbar

Here is the collapsed Toolbar's problem with the title.

enter image description here

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?

Upvotes: 12

Views: 2861

Answers (4)

Nikhil Savaliya
Nikhil Savaliya

Reputation: 46

Add line android:fitsSystemWindows="true" to toolbar aswell.

<android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Upvotes: 0

deadpoint
deadpoint

Reputation: 453

The Problem is the android:fitsSystemWindows="true" in the root CoordinatorLayout. Remove that attribute from the root layout and the title behaves well.

Upvotes: 11

clzola
clzola

Reputation: 2025

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...

Upvotes: 5

smora
smora

Reputation: 719

This is something about

((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

It always get the one corresponding to the first fragment so for the others it didn't work. (I be able to reproduce the bug only when I changed to toolbar id to a wrong one, and that's explain why you dont have problem on one of your fragment)

Try to set a specific id for each Toolbar component in your different fragment layouts, or if the layout.xml is the same for all fragments, find a way to be sure to setSupportActionBar with the exact Toolbar view of your current fragment.

Upvotes: 1

Related Questions