Android: Bottomsheet Doesn't Disappear Normally
Solution 1:
"By attaching a BottomSheetBehavior to a child View of a
CoordinatorLayout(i.e., addingapp:layout_behavior=”android.support.design.widget.BottomSheetBehavior”), you’ll automatically get the appropriate touch detection to transition between five state...".Try changing yourLinear LayouttoFrame Layoutwill do.
Try to use like this.
<FrameLayoutandroid:id="@+id/bottom_sheet"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#ff0000"app:behavior_hideable="true"app:layout_behavior="android.support.design.widget.BottomSheetBehavior"><Buttonandroid:id="@+id/buildProjectButton"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/buildProject" /><Buttonandroid:id="@+id/buildTaskButton"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/buildTask" /></FrameLayout>Modify your code as this.
ViewbottomSheet= findViewById(R.id.bottom_sheet);
BottomSheetBehaviorbottomSheetBehavior= BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
bottomSheetBehavior.setBottomSheetCallback(newBottomSheetBehavior.BottomSheetCallback() {
@OverridepublicvoidonStateChanged(@NonNull View bottomSheet, int newState) {
// React to state change
Log.e("onStateChanged", "onStateChanged:" + newState);
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
// Hide your state here.
}
}
@OverridepublicvoidonSlide(@NonNull View bottomSheet, float slideOffset) {
// React to dragging events
Log.e("onSlide", "onSlide");
}
});
EDIT 1:
Bottom sheets behavior is not hideable by default. You need to add this.
bottomSheetBehavior.setHideable(true);
Solution 2:
You are using a Persistent bottom sheet, this type of bottom sheet responds mainly to drag events i.e. you have to close it by dragging it down. If you want a bottom sheet that responds to clicking outside the dialog, consider Modal bottom sheet.
Alternatively, you can also use the fab to toggle the state of the bottom sheet i.e onClick of the fab it should expand the sheet if collapsed or collapse it if expanded.
Post a Comment for "Android: Bottomsheet Doesn't Disappear Normally"