Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android persistent bottom sheet initial visibility

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?

like image 776
Valentin Avatar asked Jul 27 '17 09:07

Valentin


People also ask

What is BottomSheetbehavior in Android?

Android Bottom Sheet is a component that slides up from the bottom of the screen having multiple options. Here are the examples of the Bottom sheet from apps. There are two types of bottom sheets, Persistent Bottom Sheet and Modal Bottom Sheet.

What is BottomSheetbehavior?

STATE_SETTLING -> bottomsheet is settling to specific height after a drag/swipe gesture. This will be the peek height, expanded height, or 0, in case the user action caused the bottom sheet to hide.

When to use bottom sheet in Android?

Bottom sheets are displayed as a result of user triggered action, and also it can reveal additional content by swiping up. Bottom sheet can be either modal – that slides up from bottom of the screen to reveal more content or persistent – when they're integrated with the app to display supporting content.


1 Answers

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.

like image 158
Valentin Avatar answered Sep 27 '22 18:09

Valentin