11

I am trying to set hidden state for BottomSheet, but it doesn't work. What's the problem?

 bottomBar = BottomSheetBehavior.from(findViewById(R.id.bottom_bar));
 bottomBar.setState(BottomSheetBehavior.STATE_HIDDEN);
Charuක
  • 12,953
  • 5
  • 50
  • 88
John
  • 355
  • 2
  • 4
  • 15

8 Answers8

39

Remember to add this while hiding the bottom sheet at start of activity/fragment

bottomSheetBehavior =BottomSheetBehavior.from(bottom_sheet_view_here);
bottomSheetBehavior.setHideable(true);//Important to add
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
zohaib khaliq
  • 697
  • 10
  • 17
12
mBottomSheetBehaviour.setPeekHeight(0);

use this and it will hide.

Akshay Shah
  • 748
  • 1
  • 8
  • 23
5

try the following :

LinearLayout bottomSheetViewgroup  
= (LinearLayout) findViewById(R.id.bottom_sheet);

BottomSheetBehavior bottomSheetBehavior =  
BottomSheetBehavior.from(bottomSheetViewgroup);

then use

bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
Thorvald
  • 3,424
  • 6
  • 40
  • 66
3

Make sure you're not doing this too early in your Activity's lifecycle. If you need to do it in onCreate or something similar, try putting it in a Runnable that you post to a view, like so:

getWindow().getDecorView().post(new Runnable() {
    @Override
    public void run() {
        bottomBar = BottomSheetBehavior.from(findViewById(R.id.bottom_bar));
        bottomBar.setState(BottomSheetBehavior.STATE_HIDDEN);
    }
 });

It's not the cleanest solution, but it's sometimes unavoidable.

Charuක
  • 12,953
  • 5
  • 50
  • 88
Dmitry Brant
  • 7,612
  • 2
  • 29
  • 47
1
 bottomsheetbehavior.setPeekHeight(0, true);

to hide with a small animation :)

or

            bottomSheet.animate()
                    .translationYBy(bottomSheetBehavior.getPeekHeight());
Arnaud
  • 408
  • 4
  • 11
0

Try BottomSheetBehaviour.STATE_COLLAPSED

bottomBar = BottomSheetBehavior.from(findViewById(R.id.bottom_bar));
bottomBar.setState(BottomSheetBehavior.STATE_COLLAPSED);
Raymond
  • 2,276
  • 2
  • 20
  • 34
0

If you use something like a Tabbed Activity, you can hide your bottomsheetlayout in your Fragment.

I thing this possible, because the fragment view is created after the activity view.

class "activity"
   public void hideBottomSheet(){ 
      sheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
   }

class "fragment"
   onCreateView()
      ((YourActivity.class)getActivity()).hideBottomSheet();
Gokul G
  • 698
  • 4
  • 11
0

Another way - this way you don't need Fragments:

boolean init = true;


layoutBottomSheet.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
            if(init)hideBottomSheet();
            init=false;
        }
    });
Samvel Aleqsanyan
  • 2,812
  • 4
  • 20
  • 28