6

I'm trying to implement a persistent bottom sheet in my layout - one that cannot be completely hidden, but always peeks from the bottom and can be expanded to full height. This 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.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical">

        <!-- main content -->

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutBottomSheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center|bottom"
        android:background="@drawable/custom_background"
        android:clipToPadding="false"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        app:behavior_hideable="false"
        app:behavior_peekHeight="50dp"
        app:layout_behavior="@string/bottom_sheet_behavior">

        <!-- bottom sheet content -->

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

What I expect to happen is for the bottom sheet to be visible and collapsed as soon as I land on the screen, but it's not - it's hidden. I can get it to show up by calling bottomSheetBehaviour.setState(BottomSheetBehavior.STATE_EXPANDED) in onCreate(). This, curiously, only expands it slightly - more than the peek height specified but less than the full height it should take up. After it appears in this state I can drag it up and down to where it should be and it works fine. The problem is the initial landing on the screen is messed up.

I'm sure there's a way I can get this working so the Bottom Sheet initially appears at it's peek height. Any ideas?

3

As it often happens - you find the answer after asking the question. My problem was the android:layout_gravity="center|bottom" set on the bottom sheet view. Removing that fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.