BottomSheetDialogFragment doesn't show full height in landscape mode
the solution for this issue is.
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < 16) {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
FrameLayout bottomSheet = (FrameLayout)
dialog.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
behavior.setPeekHeight(0); // Remove this line to hide a dark background if you manually hide the dialog.
}
});
}
Thanks to @avez raj and Prevent dismissal of BottomSheetDialogFragment on touch outside I wrote in onCreateDialog()
.
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
dialog.setOnShowListener {
// For AndroidX use: com.google.android.material.R.id.design_bottom_sheet
val bottomSheet = dialog.findViewById<View>(
android.support.design.R.id.design_bottom_sheet) as? FrameLayout
val behavior = BottomSheetBehavior.from(bottomSheet)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
return dialog
}
The ViewTreeObserver
solution did not work for me, but I found a superior solution here and converted it to Kotlin. This one doesn't have the expensive computational waste which comes with a ViewTreeObserver
and nicely bundles the functionality into the class.
class ExpandedBottomSheetDialog(context: Context) : BottomSheetDialog(context) {
override fun show() {
super.show()
// androidx should use: com.google.android.material.R.id.design_bottom_sheet
val view = findViewById<View>(R.id.design_bottom_sheet)
view!!.post {
val behavior = BottomSheetBehavior.from(view)
behavior.setState(BottomSheetBehavior.STATE_EXPANDED)
}
}
}