alb
alb

Reputation: 357

Can't hide Bottom Sheet, Android

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) {

            }
        });

Upvotes: 9

Views: 20949

Answers (8)

Dany T Mpofu
Dany T Mpofu

Reputation: 1

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 to hide it.

firstly check if its showing otherwise hide it.

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

Upvotes: -1

Nikhil
Nikhil

Reputation: 1222

You need to just simply add the below code and it works perfectly.

To hide the bottomsheet:-

bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN

To show the bottomsheet:-

 bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED

Upvotes: 0

Vicente Domingos
Vicente Domingos

Reputation: 99

When Collapsed set app:behavior_hideable="false"

Upvotes: 0

Kishore Reddy
Kishore Reddy

Reputation: 2454

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

Upvotes: 1

Marius Kohmann
Marius Kohmann

Reputation: 721

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.

Upvotes: 3

RoCkDevstack
RoCkDevstack

Reputation: 3817

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.

Upvotes: 3

Pietro Scarampella
Pietro Scarampella

Reputation: 472

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"

Upvotes: 21

ALBPT
ALBPT

Reputation: 144

Write this:

    mBottomSheetBehavior.setPeekHeight(0);

Upvotes: 12

Related Questions