Search code examples
androidandroid-fragmentsmaterial-designandroid-coordinatorlayoutbottom-sheet

Bottomsheetbehavior adds additional space to the top of bottomsheet


I generate Android Studio's BottomSheetDialogFragment it works fine with little modifications but as soon i add app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" to bottom sheet for callback it adds blank white space to the top of bottom sheet. I also tried

app:behavior_hideable="true"

app:behavior_peekHeight="128dp" After Adding BottomSheetBehaviorBefore adding BottomSheetBehavior

and solutions from here and here but its not working in my case here is my layout and fragment

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

<RelativeLayout
    android:id="@+id/bottomSheet"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    >



    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/topBarBottomSheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Spinner
            android:id="@+id/attachmentOption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginLeft="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.019"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <SearchView
            android:id="@+id/searchView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/closeButton"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />

        <ImageButton
            android:id="@+id/closeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:background="@android:drawable/ic_menu_close_clear_cancel" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <EditText
        android:id="@+id/searchViewEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/topBarBottomSheet"
        android:text="TextView" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleviewGallery"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:layout_below="@id/searchViewEditText"
        android:clipToPadding="false"
        android:paddingTop="@dimen/list_item_spacing_half"
        android:paddingBottom="@dimen/list_item_spacing_half"
        tools:listitem="@layout/recycler_view_item_3"
        tools:spanCount="3"
        tools:layoutManager="GridLayoutManager" />
</RelativeLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

BottomSheetDialogFragment

 public class BottomSheet extends BottomSheetDialogFragment {
        public static String TAG = "Bottom Sheet";


        // TODO: Customize parameter argument names
        private static final String ARG_ITEM_COUNT = "item_count";
        private Listener mListener;

        // TODO: Customize parameters
        public static BottomSheet newInstance() {
            /*final Bundle args = new Bundle();
            args.putInt(ARG_ITEM_COUNT, itemCount);
            fragment.setArguments(args);*/
            return new BottomSheet();
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
            getRecentImages();
            return inflater.inflate(R.layout.fragment_bottemsheet_list_dialog, container, false);
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            final RecyclerView recyclerView = view.findViewById(R.id.recycleviewGallery);
            View parentView = view.findViewById(R.id.bottomSheet);
            BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parentView);
            bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(@NonNull View view, int i) {
                    switch (i){
                        case BottomSheetBehavior.STATE_COLLAPSED:
                            Log.d(TAG,"Collapsed");
                            break;
                        case BottomSheetBehavior.STATE_DRAGGING:
                                Log.d(TAG,"Dragging");
                                break;
                        case BottomSheetBehavior.STATE_EXPANDED:
                            Log.d(TAG,"Expanded");
                            break;
                        case BottomSheetBehavior.STATE_HALF_EXPANDED:
                            Log.d(TAG,"Half Expanded");
                            break;
                        case BottomSheetBehavior.STATE_HIDDEN:
                            Log.d(TAG,"Hidden");
                            break;
                        case BottomSheetBehavior.STATE_SETTLING:
                            Log.d(TAG,"Settling");
                            break;

                    }
                }

                @Override
                public void onSlide(@NonNull View view, float v) {

                }
            });

        }

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            final Fragment parent = getParentFragment();
            Log.d(TAG,"Parent = "+parent+" Context "+context);
            if (parent != null) {
                mListener = (Listener) parent;
            } else {
                mListener = (Listener) context;
            }
        }

        @Override
        public void onDetach() {
            mListener = null;
            super.onDetach();
        }

        public interface Listener {
            void onBottomSheetClicked(int position);
        }

    }

Solution

  • I made my bottomsheet fully working without adding app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" I removed behavior from xml file and then in BottomSheetDialogFragment i override onCreateDialog like this

    @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    //Get the BottomSheetBehavior
                    BottomSheetDialog d = (BottomSheetDialog) dialog;
                    FrameLayout bottomSheet = d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
                    if (bottomSheet != null) {
                        bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
                        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
                        bottomSheet.setMinimumHeight(350);
                        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                            @Override
                            public void onStateChanged(@NonNull View view, int i) {
                                switch (i){
                                    case BottomSheetBehavior.STATE_COLLAPSED:
                                        Log.d(TAG,"Collapsed");
    
                                        break;
                                    case BottomSheetBehavior.STATE_DRAGGING:
                                        Log.d(TAG,"Dragging");
    
                                        break;
                                    case BottomSheetBehavior.STATE_EXPANDED:
                                        Log.d(TAG,"Expanded");
    
                                        break;
                                    case BottomSheetBehavior.STATE_HALF_EXPANDED:
                                        Log.d(TAG,"Half Expanded");
                                        break;
                                    case BottomSheetBehavior.STATE_HIDDEN:
                                        Log.d(TAG,"Hidden");
                                        dismiss();
                                        break;
                                    case BottomSheetBehavior.STATE_SETTLING:
                                        Log.d(TAG,"Settling");
                                        break;
    
                                }
                            }
    
                            @Override
                            public void onSlide(@NonNull View view, float v) {
    
                            }
                        });
                    }
                }
            });
    
            return dialog;
        }
    

    If you notice in above code in onShow method i am creating instance of BottomSheetDialog and with the help of instance giving FrameLayoutobject android(x) default bottomsheet

    BottomSheetDialog d = (BottomSheetDialog) dialog;
                    FrameLayout bottomSheet = d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
    

    then giving object of framelayout to BottomSheetBehavior

    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
    

    and then finally add callback to bottomsheet

    bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback()