BottomSheetDialogFragment opens half
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.material
in 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.
Related videos on Youtube
![Khemraj Sharma](https://i.stack.imgur.com/l9PyP.png?s=256&g=1)
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, 2022Comments
-
Khemraj Sharma almost 2 years
My
BottomSheetDialogFragment
opens half (mean not fully) when I open it.fragment.show(supportFragmentManager, "my_frag")
- I tried
NestedScrollView
withbehavior_peekHeight
but did not work. - Tried without
NestedScrollView
. with onlyLinearLayout
. - Tried switching height between
match_parent
&wrap_content
I have simple
RecyclerView
inBottomSheetDialogFragment
layout.<android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout ... > <android.support.v7.widget.RecyclerView ... />
- I tried
-
Khemraj Sharma almost 6 yearsSo a small thing need to be fixed by 6 lines code. This is bad about Android.
-
Khemraj Sharma almost 6 yearsThanks but I just got answer there stackoverflow.com/a/35976745/6891563
-
Jyubin Patel almost 6 yearsWelcome @Khemraj
-
ADM almost 6 yearsIn a way its good (just to fulfill all requirement ). You see the flags
setSkipCollapsed
andsetHideable
are very useful in case you do not want to anchor the sheet in between. and there must be more. -
ADM almost 6 yearsBy any chance if you are having a
EditText
in bottom sheet (like a chat view) and you struggle withSoftInputMode
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 almost 6 yearsThanks @ADM, it's a useful share, but already solved problem with only using
STATE_EXPANDED
. I did not usesetSkipCollapsed
&setHideable
. -
androidcodehunter over 5 years@ADM you saved my day, thanks :)
-
parvez rafi over 5 yearsWell Deserved man!!
-
prateek about 4 yearsI would prefer this way since it does not require hard coding any resource id
BottomSheetBehavior bottomSheetBehavior = ((BottomSheetDialog)dialog).getBehavior(); bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);