BottomSheetDialogFragment opens half

11,191

Solution 1

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.

Solution 2

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.

Share:
11,191

Related videos on Youtube

Khemraj Sharma
Author by

Khemraj Sharma

Email : [email protected] LinkedIn : https://www.linkedin.com/in/khemrajsharma/ Flutter, Android, Kotlin, Java, Dart, JavaScript Just love to code Pluralsight Java Expert & Android Proficient HackerRank Java 5 Star

Updated on September 16, 2022

Comments

  • Khemraj Sharma
    Khemraj Sharma almost 2 years

    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
    Khemraj Sharma almost 6 years
    So a small thing need to be fixed by 6 lines code. This is bad about Android.
  • Khemraj Sharma
    Khemraj Sharma almost 6 years
    Thanks but I just got answer there stackoverflow.com/a/35976745/6891563
  • Jyubin Patel
    Jyubin Patel almost 6 years
    Welcome @Khemraj
  • ADM
    ADM almost 6 years
    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
    ADM almost 6 years
    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 . I have wasted 2 days to get it . Thought it might worth to share . No other solution worked for me except this one .
  • Khemraj Sharma
    Khemraj Sharma almost 6 years
    Thanks @ADM, it's a useful share, but already solved problem with only using STATE_EXPANDED. I did not use setSkipCollapsed & setHideable.
  • androidcodehunter
    androidcodehunter over 5 years
    @ADM you saved my day, thanks :)
  • parvez rafi
    parvez rafi over 5 years
    Well Deserved man!!
  • prateek
    prateek about 4 years
    I would prefer this way since it does not require hard coding any resource id BottomSheetBehavior bottomSheetBehavior = ((BottomSheetDialog)dialog).getBehavior(); bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPAN‌​DED);