Home » Android » java – Can't hide Bottom Sheet, Android

java – Can't hide Bottom Sheet, Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m having problems with my 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 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 , there is no , because the height is 0dp, so it won’t show anything.
My question is, is there a way to declare the 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) {

            }
        });
How to&Answers:

Write this:

    mBottomSheetBehavior.setPeekHeight(0);

Answer:

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"

Answer:

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.

Answer:

In my case I was using BottomSheetDialog.

app:behavior_hideable – attribute is used to determine if our bottom sheet will hide when it is swiped down. In other words bottom sheet top be off screen, if the peek height isn’t set.

app:behavior_peekHeight – attribute value used to represent how much pixels the bottom sheet will be visible.

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="10dp"
android:orientation="vertical"
android:background="@color/colorPrimaryDerived"
app:layout_behavior="@string/bottom_sheet_behavior"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"> ........... </LinearLayout>

I set the peekHeight to 50dp. And peek height has nothing to do with the bottomSheet layout height itself which I set 200dp (for example only).

peek

You can view the changes in your XML viewer if the bottom sheet is expanded, if so add the app:behavior_peekHeight = 0dpfrom the xml layout and it will hide and also inform you of the current state.

Answer:

Inside onCreate add these lines, it can hide the bottombar

mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior.setHideable(true); //Important to add
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); //Important to add

Answer:

When Collapsed set app:behavior_hideable=”false”

Answer:

You can manually hide that bottom sheet by setting the visibility of the parent linear layout to gone
put this line in your code when you want

if (confirmLayoutBehaviour.getState() != BottomSheetBehavior.STATE_EXPANDED) {
//todo hide your bottom sheet if its already open
confirmLayout.setVisibility(View.GONE);
} else {
//set it to visible if its not open
confirmLayout.setVisibility(View.VISIBLE);
}

it worked for me please try it