Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 742
MikkoP Avatar asked Nov 25 '13 17:11

MikkoP


2 Answers

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

like image 188
Vikram Avatar answered Oct 24 '22 02:10

Vikram


Just add two values to your custom theme.

<item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>

E.g.

<style name="MyDialog" parent="ThDialogBase">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
        <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>
like image 3
Ligen Yao Avatar answered Oct 24 '22 02:10

Ligen Yao