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 MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomBottomSheetDialog customBottomSheetDialog = new CustomBottomSheetDialog();
customBottomSheetDialog.show(getSupportFragmentManager(),customBottomSheetDialog.getTag());
}
}
CustomBottomSheetDialog
public class CustomBottomSheetDialog extends BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return View.inflate(getContext(), R.layout.view_config, null);
}
}
CustomBottomSheetDialog layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#fdf107"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="196dp"
android:gravity="center"
android:textColor="@color/colorAccent"
android:text="BottomSheetDialogFragment"/>
</LinearLayout>
in landscape mode, i have to drag BottomSheetDialogFragment to see the whole content.
I used such way: add a view with "android:layout_height="match_parent" into root layout. when dialog is shown (or before this) update of BottomSheetBehavior with "isFitToContents = false" and "state = BottomSheetBehavior. STATE_EXPANDED".
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. } }); }
This worked for me and was the cleanest approach:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = BottomSheetDialog(requireContext(), theme)
dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
return dialog
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With