Custom Dialog size to match Theme.Holo.Light.Dialog

MikkoP picture MikkoP · Nov 25, 2013 · Viewed 17.1k times · Source

If I have an Activity that has it's theme set to Theme.Holo.Light.Dialog, it will scale great. It will fill the screen on phones in portrait mode almost entirely but in landscape mode it won't stretch unreasonably long. For example, in this picture from Google, you can see the dialog not filling the whole screen.

enter image description here

It won't either collapse to match the width of the title like what will happen if you have your own Dialog build by having a class that extends the Dialog class.

This is what will happen with my layout.

enter image description here

What properties do I need to apply to the LinearLayout to make it scale pretty?

Answer

Vikram picture Vikram · Dec 1, 2013

You can use Theme.Holo.Light.Dialog.MinWidth for sizing your layout properly.

From the documentation:

public static final int Theme_Holo_Light_Dialog_MinWidth

Variant of Theme.Holo.Light.Dialog that has a nice minimum width for a regular dialog.

The way to use this would be through passing a ContextThemeWrapper in place of Context (using this) to your custom Dialog's constructor:

YourCustomDialog cDialog = new YourCustomDialog(
                        new ContextThemeWrapper(this, 
                              android.R.style.Theme_Holo_Light_Dialog_MinWidth));

This is how Theme.Holo.Light.Dialog.MinWidth is defined:

<style name="Theme.Holo.Light.Dialog.MinWidth">
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>

From dimens.xml:

@android:dimen/dialog_min_width_major:

<!-- The platform's desired minimum size for a dialog's width when it
     is along the major axis (that is the screen is landscape).  This may
     be either a fraction or a dimension. -->
<item type="dimen" name="dialog_min_width_major">65%</item>

@android:dimen/dialog_min_width_minor:

<!-- The platform's desired minimum size for a dialog's width when it
     is along the minor axis (that is the screen is portrait).  This may
     be either a fraction or a dimension. -->
<item type="dimen" name="dialog_min_width_minor">95%</item>

As it seems, the dialog's width in the picture you posted is around 65%. In the portrait mode, it would be 95%.

Honestly, the width doesn't look like 95 % in portrait mode, but it's better than before :):

enter image description here