CollapsingToolbarLayout / Toolbar margin bug

2.8k views Asked by At

I want to use in my app similar layout to @chrisbanses cheesesquare (https://github.com/chrisbanes/cheesesquare).

Everything works great on Nexus 5,6. But when you test it on other devices like Samsung S6, Sony Z3 Compact the buttons from the toolbar are not visible.

enter image description here

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    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/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@drawable/default_vehicle"
            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>

This is my layout. I can't find a way to fix this. You can try to run Cheesesquare on phone like Samsung S with 5.0.2 Android. I've also tested Sony devices. The same. Nexus phone's are OK.

Do you have any idea how to fix this?

I'm using com.android.support:design:22.2.0 (the latest version)

2

There are 2 answers

1
adek On BEST ANSWER

OK. I've got confirmation. This is bug in Design Support Library 22.2.0. More information: https://code.google.com/p/android/issues/detail?id=176647

If you know any workaround before Google will publish a fix I'll be greatful.

There is a workaround for the Toolbar and missing buttons problem (wrong position)

if (Build.VERSION.SDK_INT != 21) return; 

final int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
final int result = (resourceId > 0) ? getResources().getDimensionPixelSize(resourceId) * 2 : 0;
final CollapsingToolbarLayout.LayoutParams params =
        (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
params.topMargin -= result;
toolbar.setLayoutParams(params);

EDIT: Google updated Support Library. This bug was fixed in 22.2.1 version. Just update Support Library and use the latest version and everything will be fine.

1
user2922073 On
if ("samsung".equals(Build.MANUFACTURER) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Toolbar toolbar = getToolbar();
        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams();
        lp.topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -47, getResources().getDisplayMetrics());
        toolbar.setLayoutParams(lp);
    }