Bottomsheetdialogfragment Doesn't Show Full Height In Landscape Mode
I am using BottomSheetDialogFragment in my activity, the dialog shows full height in portrait mode but doesn't when I switch to landscape mode. MainActivity.java public class M
Solution 1:
the solution for this issue is.
@OverridepublicvoidonViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {
@OverridepublicvoidonGlobalLayout() {
if (Build.VERSION.SDK_INT < 16) {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
BottomSheetDialogdialog= (BottomSheetDialog) getDialog();
FrameLayoutbottomSheet= (FrameLayout)
dialog.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehaviorbehavior= 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.
}
});
}
Solution 2:
This worked for me and was the cleanest approach:
overridefunonCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = BottomSheetDialog(requireContext(), theme)
dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
return dialog
}
Solution 3:
Thanks to @avez raj and Prevent dismissal of BottomSheetDialogFragment on touch outside I wrote in onCreateDialog()
.
overridefunonCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
dialog.setOnShowListener {
val bottomSheet = dialog.findViewById<View>(
com.google.android.material.R.id.design_bottom_sheet) as? FrameLayout
val behavior = BottomSheetBehavior.from(bottomSheet)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
return dialog
}
Currently I use MakinTosH solution, it is shorter.
Solution 4:
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.
classExpandedBottomSheetDialog(context: Context) : BottomSheetDialog(context) {
overridefunshow() {
super.show()
// androidx should use: com.google.android.material.R.id.design_bottom_sheetval view = findViewById<View>(R.id.design_bottom_sheet)
view!!.post {
val behavior = BottomSheetBehavior.from(view)
behavior.setState(BottomSheetBehavior.STATE_EXPANDED)
}
}
}
Solution 5:
You can simply do the following:
overridefunonViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
dialog?.let {
val sheet = it as BottomSheetDialog
sheet.behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
// rest of your stuff
}
Post a Comment for "Bottomsheetdialogfragment Doesn't Show Full Height In Landscape Mode"