Search code examples
androidandroid-layoutgrid-layoutbottom-sheet

Get BottomSheetBehaviour from BottomSheetDialog


This is my layout for BottomSheetDialog. I've a grid layout inside as well. That grid layout's scrolling is not proper. I mean it scrolls only in expanded state of BottomSheetDialog.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_behavior="@string/bottom_sheet_behavior"
    >

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/view_padding_medium"
        android:gravity="center_vertical">

        <ImageView
            android:layout_width="@dimen/profile_image"
            android:layout_height="@dimen/profile_image"
            android:src="@drawable/icon" />

        <TextView
            android:id="@+id/title1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/view_padding_medium"
            android:text="@string/smart_action_share"
            android:textColor="@color/white"
             />
    </LinearLayout>



    <GridView
        android:id="@+id/gridView11"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="3"
        >

    </GridView>


</LinearLayout>

This is the way i'm creating a bottom sheet dialog:

BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context);
        LayoutInflater inflater = ((Activity) Constants.getContext()).getLayoutInflater();

        View view = inflater.inflate(R.layout.dialog_share1, null);

        bottomSheetDialog.setContentView(view);

    final GridView grid = (GridView) view.findViewById(R.id.gridView11);

    CustomAdapter adapter = new CustomAdapter (context);
    grid.setAdapter(adapter);

    bottomSheetDialog.show();

How can the behaviour of dialog be accessed so that i can fix the grid layout scrolls or is there any other way of fixing that?

Just to make everything clear: Grid view scroll should be enabled everytime irrespective of the state of bottom sheet.


Solution

  • Grid view scroll should be enabled everytime irrespective of the state of bottom sheet.

    I don't think you should do this because the BottomSheet height should match the content's height.

    This means that if the content is scrollable and exceeds the parent's height, scroll will only work if the BottomSheet is expanded because of the default behavior, which makes sense.

    To have access to the behavior, you can do:

    View view = inflater.inflate(R.layout.dialog_share1, null);
    bottomSheetDialog.setContentView(view);
    
    BottomSheetBehavior behavior = BottomSheetBehavior.from((View) view.getParent());
    

    And then, to customize the behavior on state different from expanded:

    behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    
        @Override
        public void onStateChanged(@NonNull View bottomSheet,
                @BottomSheetBehavior.State int newState) {
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                dismiss();
            }else{
                if (newState != BottomSheetBehavior.STATE_EXPANDED) {
                    // Implement your logic here
                }
            }
        }
    
        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset){
    
        }
    };