Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BottomSheetDialogFragment that does not take whole width

How do I make BottomSheetDialogFragment that does not cover entire width of the screen (something like "Share via" sheet in Chrome on tablets)?

like image 550
UfoXp Avatar asked May 31 '16 15:05

UfoXp


2 Answers

As a developer suggested in the issue linked by @UfoXp, the problem is BottomSheetDialog.onCreate() setting the window to MATCH_PARENT both ways:

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Dialog dialog = super.onCreateDialog(savedInstanceState);

    if ((isTablet(getContext()) || isLandscape(getContext()))) {
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogINterface) {
                dialog.getWindow().setLayout(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
            }
        });
    }
    return dialog;
}

private boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

private boolean isLandscape(Context context) {
    return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
}
like image 86
Roberto Leinardi Avatar answered Sep 27 '22 21:09

Roberto Leinardi


Just use getWindow().setLayout() after showing the Dialog

dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
like image 30
Abdallah Alaraby Avatar answered Sep 27 '22 22:09

Abdallah Alaraby