Skip to content Skip to sidebar Skip to footer

Bottomsheetdialog Get Behavour Always Returns Null

I working with BottomSheetDialog and i have to get Behavior so can set setBottomSheetCallback() to handle some stuff. As google says i had to put Coordinator on parentView and add

Solution 1:

BottomSheetDialog is a rather peculiar Dialog implementation. It is not added to, nor does it rely on, a CoordinatorLayout in your Activity's layout. It sets up its own CoordinatorLayout internally, and within that, a FrameLayout with BottomSheetBehavior, into which your View is placed. The BottomSheetDialog itself fills the whole screen, and has a transparent background, so that it can handle the bottom sheet interaction, and any outside touches.

If you need access to that bottom sheet and its BottomSheetBehavior, we'll need to get it from the Dialog's View hierarchy. That's as simple as calling findViewById(R.id.design_bottom_sheet) on the Dialog, but we'll need to wait until the Dialog is shown to modify the BottomSheetBehavior. Furthermore, since BottomSheetDialog sets its own BottomSheetCallback, we must ensure that we replace it appropriately. That is, we must take care of cancelling the Dialog when it hits the closed state. For example:

finalBottomSheetDialogbsd=newBottomSheetDialog(MainActivity.this);
bsd.setContentView(R.layout.your_dialog_layout);
bsd.show();

FrameLayoutbottomSheet= (FrameLayout) bsd.findViewById(R.id.design_bottom_sheet);
BottomSheetBehaviorbehavior= BottomSheetBehavior.from(bottomSheet);
behavior.setBottomSheetCallback(newBottomSheetBehavior.BottomSheetCallback() {
        @OverridepublicvoidonStateChanged(View bottomSheet, int newState) {
            // This is the crucial bit.if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                bsd.cancel();
            }
        }

        @OverridepublicvoidonSlide(View bottomSheet, float slideOffset) {}
    }
);

If you're using a BottomSheetDialogFragment, the Dialog is shown in DialogFragment's onStart(), and we can override that method to do our modifications there, after the super call. For example:

publicclassMyFragmentextendsBottomSheetDialogFragment {
    publicMyFragment() {}

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.your_dialog_layout, container, false);
    }

    @OverridepublicvoidonStart() {
        super.onStart();

        FrameLayoutbottomSheet= getDialog().findViewById(R.id.design_bottom_sheet);
        BottomSheetBehaviorbehavior= BottomSheetBehavior.from(bottomSheet);
        behavior.setBottomSheetCallback(newBottomSheetBehavior.BottomSheetCallback() {
                @OverridepublicvoidonStateChanged(View bottomSheet, int newState) {
                    // This is the crucial bit.if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                        getDialog().cancel();
                    }
                }

                @OverridepublicvoidonSlide(View bottomSheet, float slideOffset) {}
            }
        );
    }
}

In either case, you can do pretty much whatever you want in the BottomSheetCallback, as long as you cancel() the Dialog in onStateChanged() when newState == BottomSheetBehavior.STATE_HIDDEN.


Solution 2:

in your CoordinatorLayout you have child that have this app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

so you need to make BottomSheetBehavior behavior = BottomSheetBehavior.from(your CoordinatorLayout child);

And on it you need to make setBottomSheetCallback. behavior.setBottomSheetCallback(...)

Post a Comment for "Bottomsheetdialog Get Behavour Always Returns Null"