Home > Enterprise >  BottomSheetBehaviour is dissmised when user clicks on back
BottomSheetBehaviour is dissmised when user clicks on back

Time:09-25

I have a BottomSheetBehaviour in my layout, and it works absolutely fine. The problem is, that whenever user clicks on back button, it becomes invisible(or gone). How can I prevent it to be non-dissmissable?

CodePudding user response:

create a layout for your bottom sheet as:

<androidx.core.widget.NestedScrollView 
   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@ id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="16dp"
    app:behavior_peekHeight="260dp"//change according to your need
          app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
        
         <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        //your design here       
       </LinearLayout>
    </androidx.core.widget.NestedScrollView>

include this layout in your main layout file

<include layout="@layout/layout_name" />

now you can use your bottom sheet in your activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name);
    
    View bottomSheet = findViewById(R.id.bottomSheet);
    //add behaviour as
     BottomSheetBehavior<View> bottomSheetBehavior =BottomSheetBehavior.from(bottomSheet);
    //access your views inside bottomSheet as
    
    TextView textView = bottomSheet.findViewById(R.id.textViewId);
    
}

CodePudding user response:

Just override onCancel() or onDismiss().

@Override
public void onCancel(DialogInterface dialog) {
//   super.onCancel(dialog); // comment this out
   Toast.makeText(MainApp.get(), "CANCEL REQUESTED", Toast.LENGTH_SHORT).show();
}

Or override onBackPressed in onCreateDialog function when you're crating the dialog

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new Dialog(getActivity(), getTheme()){
        @Override
        public void onBackPressed() {
           Toast.makeText(MainApp.get(), "CANCEL REQUESTED", Toast.LENGTH_SHORT).show();
        }
    };
}
  • Related