Show and Hide Bottom Sheet Programmatically

AndroidMaterial DesignBottom Sheet

Android Problem Overview


I have implemented Bottom Sheet functionality within my activity in onCreate() using this solution and this library

   sheet = new BottomSheet.Builder(this, R.style.BottomSheet_Dialog)
        .title("New")
        .grid() // <-- important part
        .sheet(R.menu.menu_bottom_sheet)
        .listener(new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO
    }
}).build();

Now, I would like to Show Bottom sheet, on click of button and in a same way want to hide bottom sheet on click of same button, if already Visible

Android Solutions


Solution 1 - Android

To close the BottomSheetDialogFragment from inside the fragment you can call:

dismiss();

To show or hide the BottomSheetDialogFragment from the activity you can simply call:

bottomSheetDialogFragment.dismiss();//to hide it
bottomSheetDialogFragment.show(getSupportFragmentManager(),tag);// to show it

Solution 2 - Android

Inside your onClick() of the button use: sheet.show().

Then when you want to dismiss it, use sheet.dismiss();

Here below a possible solution:

BottomSheet sheet = new BottomSheet.Builder(...).build();
Button button = (Button)findViewById(R.id.mybutton);
button.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        //you can use isShowing() because BottomSheet inherit from Dialog class
        if (sheet.isShowing()){
            sheet.dismiss();
        } else {
            sheet.show();    
        }
    }
});

Solution 3 - Android

For showing the Bottom Sheet use this code:

bottomSheetInfoBehavior.setHideable(false);
bottomSheetInfoBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);

For Hiding the Bottom sheet use this code:

bottomSheetInfoBehavior.setHideable(true);
bottomSheetInfoBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

Solution 4 - Android

If you want hide from within a Fragment then use

this.dismiss();

or

YOUR_FRAGMENT.this.dismiss()

Solution 5 - Android

use the following code

new BottomSheet.Builder(getActivity()).title("Your Title here").sheet(R.menu.bottom_sheet).listener(new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case R.id.cancel:
                        dialog.cancel();
                        break;
                    case R.id.view:
                        //Todo view code here
                        dialog.cancel();
                        break;
                }
            }
        }).show();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSophieView Question on Stackoverflow
Solution 1 - AndroidMalek HijaziView Answer on Stackoverflow
Solution 2 - AndroidMatPagView Answer on Stackoverflow
Solution 3 - AndroidAjay SharveshView Answer on Stackoverflow
Solution 4 - AndroidPavan PyatiView Answer on Stackoverflow
Solution 5 - AndroidMuhammad Hamza ShahidView Answer on Stackoverflow