Nathiel Barros
Nathiel Barros

Reputation: 715

Android bottom sheet does not collapse

I'm trying to create a simple Bottom Sheet layout, One button shows and other collapse, but its not working. I was following this tutorial but still nothing.So I'm trying to pass the state inside of buttons and then pass into the callback method to hide or expand, and the bottom sheet can't be scrolled down!

This is the layout.

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_botton_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="24dp">

        <Button
            android:id="@+id/button_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 1"
            android:padding="16dp"
            android:layout_margin="8dp"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_green_dark"/>

        <Button
            android:id="@+id/button_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:layout_margin="8dp"
            android:text="Button 2"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_blue_light"/>

        <Button
            android:id="@+id/button_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:layout_margin="8dp"
            android:text="Button 3"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_red_dark"/>

    </LinearLayout>

</ScrollView>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:clipToPadding="true"
    android:background="@android:color/holo_orange_light"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="opaa"
        android:padding="16dp"
        android:textSize="16sp"/>

</android.support.v4.widget.NestedScrollView>

Activity :

private View bottomSheet;
private Button button1,button2,button3;
private BottomSheetBehavior mBottomSheetBehavior;

bottomSheet = findViewById( R.id.bottom_sheet );
    button1 = (Button)findViewById(R.id.button_1);
    button2 = (Button)findViewById(R.id.button_2);
    button3 = (Button)findViewById(R.id.button_3);

    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);
            }
            if(newState == BottomSheetBehavior.STATE_EXPANDED){
                mBottomSheetBehavior.setPeekHeight(500);
            }
        }

        @Override
        public void onSlide(View bottomSheet, float slideOffset) {
        }
    });
     button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    });
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }
    });

Upvotes: 3

Views: 7027

Answers (2)

farindk
farindk

Reputation: 260

I ran into the same issue when trying that tutorial myself.

You have to set the BottomSheetBehavior to be hideable. Moreover, the COLLAPSED state may not be what you are looking for. To make the BottomSheet disappear completely, you have to set the state to HIDDEN. Like this:

mBottomSheetBehavior.setHideable(true);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

Upvotes: 4

Gevork Safaryan
Gevork Safaryan

Reputation: 237

Put TextView into LinearLayout

Upvotes: 0

Related Questions