11

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?

MikkoP
  • 4,864
  • 16
  • 58
  • 106

5 Answers5

27

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

Vikram
  • 51,313
  • 11
  • 93
  • 122
  • Hey, thanks for this. It is definitely better than what I had but it still doesn't match my Activity with it's theme set to `@android:style/Theme.Holo.Light.Dialog`. Here are some pictures. The dialog in the bottom is the correct one. http://s24.postimg.org/7dzk6dbt1/device_2013_12_01_130746.png http://s18.postimg.org/6hkao2pnd/device_2013_12_01_130722.png I also need to apply my custom theme to the dialog (to change the colors of spinners, seekbars etc), so this is a little bad don't you think? I can't have multiple themes. Can I somehow implement this to my own theme? – MikkoP Dec 01 '13 at 11:16
  • @MikkoP I have a few question about the output you are looking for. Let's [continue this exchange in chat](http://chat.stackoverflow.com/rooms/42277/custom-dialog-size-to-match-theme-holo-light-dialog). – Vikram Dec 01 '13 at 16:27
  • Hey, as we discussed, I need to apply a custom theme for my dialog. Not just a layout but theme for the seekbars and spinners in my layouts. I have this build with a [theme generator](http://android-holo-colors.com/). Do you know how can I apply this theme too, if I use `Theme.Holo.Light.Dialog.MinWidth`? – MikkoP Dec 03 '13 at 14:18
  • @MikkoP Yea, I can help you with that. Let's [talk here](http://chat.stackoverflow.com/rooms/42277/custom-dialog-size-to-match-theme-holo-light-dialog). – Vikram Dec 03 '13 at 15:22
  • @MikkoP Were you able to solve the issue with Spinner showing its options in a separate popup and not in a drop-down list? – Vikram Dec 05 '13 at 19:28
  • I added `android:spinnerMode="dropdown"` in the XML for the spinners andnow they are working correctly. Eclipse isn't very happy with that though: `Resouce id 0x1010081 is not of type STYLE (instead attr)`. – MikkoP Dec 06 '13 at 09:07
3

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>
Ligen Yao
  • 641
  • 1
  • 6
  • 6
0

In my custom dialog I use following properties and set dialog width to match parent and it's width scale properly.

 <item name="android:windowBackground">@null</item>
 <item name="android:windowIsFloating">false</item>
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • What do you mean by `set it's width scale properly`. And where does these two lines of code go? – MikkoP Nov 28 '13 at 16:54
  • In style.xml put this. And set dialog style to CustomDialogTheme and width to match_parent. – vipul mittal Nov 28 '13 at 19:10
  • This will fill the whole screen. If I set `windowIsFloating` to true, it will collapse to match the first TextView in the layout. – MikkoP Nov 29 '13 at 10:29
0

Two Scenarios

a. If you need it to fill the screen completely set your parents minWidth and minHeight to 1000dp

LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:minWidth="1000dp"  
    android:minHeight="1000dp"

b. If you need it to scale without filling the whole screen. Set one of the children of your LinearLayout within a RelativeLayout and set the relative layouts width to match_parent.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
agomes
  • 331
  • 2
  • 8
0

Can you try to change your Dialog dimension at runtime, like this:

DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
yourDialog.show();
yourDialog.getWindow().setLayout((6 * width)/7, LayoutParams.WRAP_CONTENT);

or

Dialog yourDialog = dialogFragment.getDialog();
yourDialog.getWindow().setLayout((6 * width)/7, (4 * height)/5);

if you use Fragments.

JJ86
  • 5,055
  • 2
  • 35
  • 64
  • I could change it in code, but this doesn't set the width to match the original Dialog width either. – MikkoP Nov 30 '13 at 15:33