Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set MaxWidth of an Android Dialog?

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.

like image 796
chefgon Avatar asked Nov 26 '22 13:11

chefgon


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

like image 108
inazaruk Avatar answered Nov 28 '22 02:11

inazaruk