UfoXp
UfoXp

Reputation: 619

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)?

Upvotes: 8

Views: 3532

Answers (3)

Abdallah Alaraby
Abdallah Alaraby

Reputation: 2249

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

dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT);

Upvotes: 3

Roberto Leinardi
Roberto Leinardi

Reputation: 14389

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;
}

Upvotes: 6

UfoXp
UfoXp

Reputation: 619

Looks like this is not possible right now: https://code.google.com/p/android/issues/detail?id=204670

Upvotes: 0

Related Questions