Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show and Hide Bottom Sheet Programmatically

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

like image 883
Sophie Avatar asked Feb 17 '17 08:02

Sophie


People also ask

How to show bottomSheet dialog in Android?

To implement a Bottom Sheet dialog, you need a material design library. Include the following library in your app. gradle file. Sync the project to download the library.

How to use bottomSheet?

The modal Bottom sheet always appears from the bottom of the screen and if the user clicks on the outside content then it is dismissed. It can be dragged vertically and can be dismissed by sliding it down.


2 Answers

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 
like image 111
Malek Hijazi Avatar answered Sep 17 '22 08:09

Malek Hijazi


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();             }     } }); 
like image 45
MatPag Avatar answered Sep 21 '22 08:09

MatPag