Home » Android » android – Hidden state of the Bottom Sheet

android – Hidden state of the Bottom Sheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

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);
How to&Answers:

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

Answer:

mBottomSheetBehaviour.setPeekHeight(0);

use this and it will hide.

Answer:

try the following :

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

BottomSheetBehavior bottomSheetBehavior =  
BottomSheetBehavior.from(bottomSheetViewgroup);

then use

bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

Answer:

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.

Answer:

Try BottomSheetBehaviour.STATE_COLLAPSED

bottomBar = BottomSheetBehavior.from(findViewById(R.id.bottom_bar));
bottomBar.setState(BottomSheetBehavior.STATE_COLLAPSED);

Answer:

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();

Answer:

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;
        }
    });

Answer:

 bottomsheetbehavior.setPeekHeight(0, true);

to hide with a small animation 🙂

or

            bottomSheet.animate()
                    .translationYBy(bottomSheetBehavior.getPeekHeight());