8

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
           ...
           />
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212

2 Answers2

38

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.

ADM
  • 20,406
  • 11
  • 52
  • 83
  • So a small thing need to be fixed by 6 lines code. This is bad about Android. – Khemraj Sharma Aug 10 '18 at 09:22
  • In a way its good (just to fulfill all requirement ). You see the flags `setSkipCollapsed` and `setHideable` are very useful in case you do not want to anchor the sheet in between. and there must be more. – ADM Aug 10 '18 at 09:27
  • By any chance if you are having a `EditText` in bottom sheet (like a chat view) and you struggle with `SoftInputMode` just follow [This answer](https://stackoverflow.com/a/37748167/7995966) . I have wasted 2 days to get it . Thought it might worth to share . No other solution worked for me except this one . – ADM Aug 10 '18 at 09:30
  • Thanks @ADM, it's a useful share, but already solved problem with only using `STATE_EXPANDED`. I did not use `setSkipCollapsed` & `setHideable`. – Khemraj Sharma Aug 10 '18 at 09:33
  • 1
    @ADM you saved my day, thanks :) – androidcodehunter Feb 15 '19 at 15:16
  • 1
    Well Deserved man!! – parvez rafi Mar 06 '19 at 10:46
  • I would prefer this way since it does not require hard coding any resource id `BottomSheetBehavior bottomSheetBehavior = ((BottomSheetDialog)dialog).getBehavior(); bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);` – prateek Jun 11 '20 at 09:30
1

You are accessing your parent view so use below code to expand it into Full screen.

View parent = (View) inflatedView.getParent();
parent.setFitsSystemWindows(true);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent);
inflatedView.measure(0, 0);
DisplayMetrics displaymetrics = new DisplayMetrics();        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenHeight = displaymetrics.heightPixels;
bottomSheetBehavior.setPeekHeight(screenHeight);

if (params.getBehavior() instanceof BottomSheetBehavior) {
    ((BottomSheetBehavior)params.getBehavior()).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}

params.height = screenHeight;
parent.setLayoutParams(params);

Hope it helps you.

Jyubin Patel
  • 1,373
  • 7
  • 17