Reputation: 2224
Question
I'm using the BottomSheetDialogFragment
for my modal bottom sheet and would like to set a maximum width so that on tablets/large screens the BottomSheet
doesn't occupy the entire width of the screen. How do I approach solving this? Thanks!
Relevant code & resources
fragment_bottomsheet.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/BottomSheetStyle">
<GridLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alignmentMode="alignBounds"
android:columnOrderPreserved="false"
android:columnCount="3"
android:paddingTop="16dp"
android:paddingBottom="8dp"
android:paddingRight="8dp"
android:paddingLeft="8dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/image1"
android:text="Open"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/image2"
android:text="Save"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/image3"
android:text="Send"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/image4"
android:text="Upload"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/image5"
android:text="Share"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/iamge6"
android:text="More"/>
</GridLayout>
</android.support.design.widget.CoordinatorLayout>
res/values/styles.xml:
<style name="BottomSheetStyle">
<item name="android:layout_height">match_parent</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_gravity">center_horizontal</item>
</style>
res/values-w600dp/styles.xml:
<style name="BottomSheetStyle">
<item name="android:layout_height">match_parent</item>
<item name="android:layout_width">640dp</item>
<item name="android:layout_gravity">center_horizontal</item>
</style>
Upvotes: 14
Views: 8878
Reputation: 1303
This works for me on BottomSheetDialogFragment
. I was able to set the max width with this:
dialog.maxWidth = Resources.getSystem().displayMetrics.widthPixels
You can set this in the onCreateDialog
function. This example sets the width of the bottom sheet to the width of the display.
Hope this helps!
Upvotes: 0
Reputation: 361
If you want to do it purely in xml, you can set a style and use android:maxWidth
For example:
<LinearLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
style="@style/styleWithFixedWidth"
>
and in styles.xml
<style name="styleWithFixedWidth">
<item name="android:maxWidth">550dp</item>
</style>
Upvotes: 0
Reputation: 41448
A variation of the OP's answer that takes into consideration device's pixel density and orientation:
public final class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {
...
private static int dpToPx(int dp) {
// https://developer.android.com/guide/practices/screens_support.html#dips-pels
float density = Resources.getSystem().getDisplayMetrics().density;
return (int) ((dp * density) + 0.5f);
}
@Override
public void onResume() {
super.onResume();
Configuration configuration = getActivity().getResources().getConfiguration();
if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE &&
configuration.screenWidthDp > 450) {
// you can go more fancy and vary the bottom sheet width depending on the screen width
// see recommendations on https://material.io/components/sheets-bottom#specs
getDialog().getWindow().setLayout(ViewUtils.dpToPx(450), -1);
}
}
}
Upvotes: 7
Reputation: 1536
As you mention, BottomSheetDialogFragment does not seem to respect "wrap_content" (it always matches parent on width, even if all the views it contains have a width of "wrap_content"). I found that the best workaround for me was to utilize OnGlobalLayoutListener to listen for the appropriate time to adjust the width. Example:
@Override
public void setupDialog(final Dialog dialog, int style) {
super.setupDialog(dialog, style);
View v = View.inflate(getActivity(), R.layout.my_bottom_sheet, null);
View viewWithGreatestWidth = v.findViewById(R.id.my_super_wide_view);
// Your view setup code goes here ....
if(getResources().getBoolean(R.bool.isTablet)) {
v.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
v.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
// Can also just use your own preset "max width" value here;
// This code just lets you fake "wrap_content" value
getDialog().getWindow().setLayout(viewWithGreatestWidth.getMeasuredWidth(), WindowManager.LayoutParams.WRAP_CONTENT);
}
});
}
dialog.setContentView(v);
}
Upvotes: 0
Reputation: 2224
I found a solution which works for me:
@Override
public void onResume() {
super.onResume();
// Resize bottom sheet dialog so it doesn't span the entire width past a particular measurement
WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
int width = metrics.widthPixels < 1280 ? metrics.widthPixels : 1280;
int height = -1; // MATCH_PARENT
getDialog().getWindow().setLayout(width, height);
}
Essentially this allows me to dynamically specify dimensions for my BottomSheet
based on display.
Upvotes: 9