Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't hide Bottom Sheet, Android

I'm having problems with my bottom-sheet because when I open the activity it is on, blocking the view enter image description here

This happens, I think, because of the XML attribute declaring the bottom-sheet with 350dp of height:

<android.support.v4.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:background="?android:attr/windowBackground"
    android:clipToPadding="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

The thing is, I can't change that value to 0dp because the next time when I try to open the bottom-sheet, there is no bottom-sheet, because the height is 0dp, so it won't show anything. My question is, is there a way to declare the bottom-sheet off? (I've tried to setState to STATE_COLLAPSED but didn't work). Bellow is the java code that interacts with the Bottom Sheet. JAVA:

View bottomSheet = findViewById( R.id.bottom_sheet );
        mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
                    //mBottomSheetBehavior.setPeekHeight(0);
                    //mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                    //mBottomSheetBehavior.isHideable();
                }
            }

            @Override
            public void onSlide(View bottomSheet, float slideOffset) {

            }
        });
like image 872
alb Avatar asked Mar 22 '17 18:03

alb


People also ask

How do you close the bottom sheet?

Calling bottomsheet. close() or pressing on the backdrop will close the bottom sheet completely.

How do I stop my android from dragging the bottom sheet?

Disable drag of BottomSheetDialogFragment Even if we have made the BottomSheetDialogFragment as expanded, user can hold the top part of the BottomSheet and drag to show peek view. It can also be disabled by overriding onStateChanged() . This will set the expanded state even if user drags the view.


3 Answers

In my case i was not able to hide the bottomsheet and it was placed on top of my view. I found out that animateLayoutChanges = "true" in my layout file was causing this issue.

like image 196
Marius Kohmann Avatar answered Oct 01 '22 14:10

Marius Kohmann


first you have to add the attribute

app:behavior_hideable="true"

in your

<android.support.v4.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:background="?android:attr/windowBackground"
    android:clipToPadding="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

And then you can hide the bottom sheet using

mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN)

and not

mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)

the state COLLAPSED is between HIDDEN and EXPANDED and his heigth must be specified by the attribute:

app:behavior_peekHeight="200dp"
like image 21
Pietro Scarampella Avatar answered Oct 19 '22 00:10

Pietro Scarampella


Write this:

    mBottomSheetBehavior.setPeekHeight(0);
like image 12
ALBPT Avatar answered Oct 18 '22 22:10

ALBPT