Android – BottomSheetDialogFragment opens half

androidandroid-layoutbottom-sheet

My BottomSheetDialogFragment opens half (mean not fully) when I open it.

fragment.show(supportFragmentManager, "my_frag")
  • I tried NestedScrollView with behavior_peekHeight but did not work.
  • Tried without NestedScrollView. with only LinearLayout.
  • Tried switching height between match_parent & wrap_content

I have simple RecyclerView in BottomSheetDialogFragment layout.

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            ...
            >
           <android.support.v7.widget.RecyclerView
           ...
           />

Best Answer

By BottomSheetFragment you mean BottomSheetDialogFragment . To open expended sheet you need to make some changes in onCreateDialog().

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    BottomSheetDialog bottomSheetDialog=(BottomSheetDialog)super.onCreateDialog(savedInstanceState);
    bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            BottomSheetDialog dialog = (BottomSheetDialog) dialog;
            FrameLayout bottomSheet =  dialog .findViewById(android.support.design.R.id.design_bottom_sheet);
            BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
            BottomSheetBehavior.from(bottomSheet).setSkipCollapsed(true);
            BottomSheetBehavior.from(bottomSheet).setHideable(true);
        }
    });
    return bottomSheetDialog;
}

Just keep the layout match_parent no need to use NestedScrollView. It worked for me . Let me know if you still face problem .

In case someone is using New Material library . Which is
implementation 'com.google.android.material:material:1.0.0'. Then you need change the id of Parent FrameLayout. So it will be .

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    BottomSheetDialog bottomSheetDialog=(BottomSheetDialog)super.onCreateDialog(savedInstanceState);
    bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dia) {
            BottomSheetDialog dialog = (BottomSheetDialog) dia;
            FrameLayout bottomSheet =  dialog .findViewById(com.google.android.material.R.id.design_bottom_sheet);
            BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
            BottomSheetBehavior.from(bottomSheet).setSkipCollapsed(true);
            BottomSheetBehavior.from(bottomSheet).setHideable(true);
        }
    });
    return bottomSheetDialog;
}

Make sure all your imports from import com.google.android.materialin this case.