9

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

            }
        });
alb
  • 347
  • 2
  • 7
  • 24

8 Answers8

21

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"
  • bottom sheet does not show when i setstate COLLAPSED. I have app:behavior_peekHeight="200dp" in xml code – kemdo Sep 28 '17 at 06:21
  • just wondering, are your bottomsheet a first child in a CoordinatorLayout? – Pietro Scarampella Oct 24 '17 at 14:02
  • hello there acctualy i have a diffrent isuue where if i go to a fragment from a button click which is in the bottom sheet the fragment is opening but the bottom sheet is not hiding automaticly in the other fragment i have to do it manaully by tap on the screen here is my question https://stackoverflow.com/questions/69282868/bottomsheet-is-not-hiding-when-navigating-between-different-fragments can anyone help me with this – Vasant Raval Sep 22 '21 at 14:44
12

Write this:

    mBottomSheetBehavior.setPeekHeight(0);
ALBPT
  • 144
  • 1
  • 8
  • hello there acctualy i have a diffrent isuue where if i go to a fragment from a button click which is in the bottom sheet the fragment is opening but the bottom sheet is not hiding automaticly in the other fragment i have to do it manaully by tap on the screen here is my question https://stackoverflow.com/questions/69282868/bottomsheet-is-not-hiding-when-navigating-between-different-fragments can anyone help me with this – Vasant Raval Sep 22 '21 at 14:45
3

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.

Marius Kohmann
  • 713
  • 1
  • 8
  • 11
  • I have removed animateLayoutChanges = "true" and it is working perfectly. Thanks! – Nithin Raja Jul 10 '18 at 00:14
  • hello there acctualy i have a diffrent isuue where if i go to a fragment from a button click which is in the bottom sheet the fragment is opening but the bottom sheet is not hiding automaticly in the other fragment i have to do it manaully by tap on the screen here is my question https://stackoverflow.com/questions/69282868/bottomsheet-is-not-hiding-when-navigating-between-different-fragments can anyone help me with this – Vasant Raval Sep 22 '21 at 14:45
  • You saved my time. thanks – Rasel Oct 27 '21 at 04:00
2

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.

RoCkDevstack
  • 3,517
  • 7
  • 34
  • 56
  • hello there acctualy i have a diffrent isuue where if i go to a fragment from a button click which is in the bottom sheet the fragment is opening but the bottom sheet is not hiding automaticly in the other fragment i have to do it manaully by tap on the screen here is my question https://stackoverflow.com/questions/69282868/bottomsheet-is-not-hiding-when-navigating-between-different-fragments can anyone help me with this – Vasant Raval Sep 22 '21 at 14:45
1

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
Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
Kishore Reddy
  • 2,394
  • 1
  • 19
  • 15
0

When Collapsed set app:behavior_hideable="false"

0

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
Nikhil
  • 1,212
  • 13
  • 30
-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