Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BottomSheetDialogFragment doesn't show full height in landscape mode

Tags:

android

I am using BottomSheetDialogFragment in my activity, the dialog shows full height in portrait mode but doesn't when I switch to landscape mode.

Portrait Mode

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.

like image 973
Gautam Chibde Avatar asked Aug 10 '17 12:08

Gautam Chibde


People also ask

How do I make Bottomsheetdialogfragment full screen?

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".


2 Answers

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.         }     }); } 
like image 132
avez raj Avatar answered Sep 17 '22 12:09

avez raj


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
}
like image 24
MakinTosH Avatar answered Sep 16 '22 12:09

MakinTosH