Can't hide bottom sheet, Android
I have problems with my bottom-sheetbecause when I open the activity it is on, blocking the view
This is happening, I think, because of the XML attribute declaring bottom-sheet with 350dp height:
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="?android:attr/windowBackground"
android:clipToPadding="true"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
The thing is, I cannot change this value to 0dp, because next time I try to open bottom-sheet, not bottom-sheetbecause the height is 0dp so it doesn't show anything. My question is, is there a way to declarebottom-sheetoff? (I tried to set STATE_COLLAPSED to STATE_COLLAPSED, but it didn't work). Below is the Java code that interacts with the bottom sheet. JAVA:
View bottomSheet = findViewById( R.id.bottom_sheet );
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
//mBottomSheetBehavior.setPeekHeight(0);
//mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
//mBottomSheetBehavior.isHideable();
}
}
@Override
public void onSlide(View bottomSheet, float slideOffset) {
}
});
source to share
first you need to add the attribute
app:behavior_hideable="true"
in
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="?android:attr/windowBackground"
android:clipToPadding="true"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
And then you can hide the bottom sheet using
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN)
but not
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)
the COLLAPSED state is between HIDDEN and EXPANDED, and its attribute must be specified by the attribute:
app:behavior_peekHeight="200dp"
source to share
In my case, I used BottomSheetDialog
.
app:behavior_hideable
- the attribute is used to determine if our bottom sheet will hide when it is scrolled down . In other words, the top of the topsheet will be off if the gap height is not set.
app:behavior_peekHeight
is the attribute value used to represent how many pixels will be displayed by the bottom sheet.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="10dp"
android:orientation="vertical"
android:background="@color/colorPrimaryDerived"
app:layout_behavior="@string/bottom_sheet_behavior"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"> ........... </LinearLayout>
I have set peekHeight to 50dp. And the peep height has nothing to do with the height of the bottom sheet layout itself, which I set to 200dp (for example only).
You can preview the changes in your XML viewer if the bottom sheet is expanded if so added app:behavior_peekHeight = 0dp
from the xml layout and it will hide and also tell you the current state.
source to share
You can manually hide this bottom sheet by setting the visibility of the parent line layout to skip that line in your code when you want
if (confirmLayoutBehaviour.getState() != BottomSheetBehavior.STATE_EXPANDED) {//todo hide your bottom sheet if its already open confirmLayout.setVisibility(View.GONE); } else {//set it to visible if its not open confirmLayout.setVisibility(View.VISIBLE); }
this worked for me please try
source to share