androidandroid-layoutandroid-viewandroid-dialogbottom-sheet

How to disable BottomSheetDialogFragment dragging


How to disable BottomSheetDialogFragment dragging by finger?

I saw similar questions, but they're all about BottomSheet not BottomSheetDialogFragment.


Solution

  • Having created MyActivity as follows:

    public class MyActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
    
            new MyBottomSheetFragment().show(getSupportFragmentManager(), "tag");
        }
    
        public static class MyBottomSheetFragment extends BottomSheetDialogFragment {
    
            @Override
            public void setupDialog(Dialog dialog, int style) {
                BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialog;
                bottomSheetDialog.setContentView(R.layout.sample);
    
                try {
                    Field behaviorField = bottomSheetDialog.getClass().getDeclaredField("behavior");
                    behaviorField.setAccessible(true);
                    final BottomSheetBehavior behavior = (BottomSheetBehavior) behaviorField.get(bottomSheetDialog);
                    behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    
                        @Override
                        public void onStateChanged(@NonNull View bottomSheet, int newState) {
                            if (newState == BottomSheetBehavior.STATE_DRAGGING{ 
                                behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                            }
                        }
    
                        @Override
                        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                        }
                    });
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    Where R.layout.sample is a simple layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <View
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#e479da" />
    
        <View
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#798de4" />
    
        <View
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#e4db79" />
    
    </LinearLayout>
    

    You'll get following output:

    A part of solution is borrowed from this answer.