I have a dialog with a custom style that's using a layout that I defined in an XML file. I'd like this dialog to fill the width of the screen in portrait mode, but only about 400dip in landscape mode. MaxWidth seems to be the perfect way to accomplish that, but I can't figure out how to assign a MaxWidth to the dialog style or the XML layout.
4 Answers
Assuming you use next layout file called layout/dialog_core.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
//All your dialog views go here....
</LinearLayout>
You can then create two more files: layout/dialog.xml
and layout-land/dialog.xml
.
The layout/dialog.xml
file will not limit the width:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<include layout="@layout.dialog_core.xml" />
</LinearLayout>
While your layout-land/dialog.xml
should have width limitation:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:maxWidth="400dp" >
<include layout="@layout.dialog_core.xml" />
</LinearLayout>
And of course, you need to use R.layout.dialog
layout in your code now.
P.S. I used LinearLayout
for example purposes only. Any layout view will do.

- 74,247
- 24
- 188
- 156
-
1I tried this, but android:maxWidth doesn't appear in the auto-complete proposals window for LinearLayout so I assumed I couldn't use it. I tried putting it in anyway and the project builds, but the property has no effect and the dialog still stretches to fill the screen. – chefgon May 26 '11 at 18:52
-
@chefgon `android:maxWidth` isn't what you're looking for. What you want is to `android:layout_width="fill_parent"` in portrait mode. Do as inazaruk says and don't forget to put `dialog-land.xml` in the layout folder `res/layout-land`. Android will automatically load the correct layout (either `dialog.xml` or `dialog-land.xml`) depending on the screen orientation. – Tony Chan Jul 07 '11 at 20:03
-
As far as I am aware, and according to the dev docs, `dialog-land.xml` should be named `dialog.xml` as well, but placed in `res/layout-land` as @Turbo says - I believe @inazaruk named them differently to differentiate during his explanation. – ataulm Apr 08 '12 at 18:36
You can find an actual solution here: setting a max width for my dialog
The answer is for an activity using a dialog theme, but considering it's based on a Window object, it should be too complicated to adapt it for a Dialog.
I achieved this for a BottomSheetDialog by changing the dialog's window's layout after the dialog was shown:
private int maxWidthDp = 600;
public void showDialog(Context context) {
BottomSheetDialog dialog = new BottomSheetDialog(context);
// set up the dialog...
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
Configuration configuration = context.getResources().getConfiguration();
if (configuration.screenWidthDp > maxWidthDp) {
// ensure maximum dialog size
dialog.getWindow().setLayout(dpToPixels(maxWidthDp), -1);
}
}
});
dialog.show();
}
dpToPixels
is a helper function that converts dp values to pixel values, e.g. like here https://stackoverflow.com/a/8399445/1396068

- 2,702
- 26
- 36
Have you tried to set android:minWidth
and android:minHeight
in your custom xml

- 4,280
- 1
- 24
- 38

- 2,823
- 5
- 18
- 17
-
18No, but I don't understand how minWidth and minHeight would help me set a maximum width. – chefgon May 27 '11 at 16:16