4

My custome style for AlertDialog look like:

<style name="Testing.Dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/color_accent</item>
    <item name="android:textColorPrimary">@color/text_color_primary</item>
    <item name="android:background">@color/color_primary</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

I need change width and height, because it is too large on my tablet. Any ideas?

Below code not working properly:

   <item name="windowMinWidthMajor">@dimen/abc_dialog_min_width_major</item>
   <item name="windowMinWidthMinor">@dimen/abc_dialog_min_width_minor</item>
waclaw
  • 433
  • 1
  • 7
  • 18

3 Answers3

18

I was able to adjust the width like so:

<style name="NarrowDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="windowFixedWidthMajor">70%</item>
    <item name="windowFixedWidthMinor">70%</item>
</style>

Dialog dialog = new AlertDialog.Builder(this, R.style.NarrowDialog)...

There is also a minimum width you might wish to adjust:

<item name="windowMinWidthMajor">65%</item>
<item name="windowMinWidthMinor">65%</item>

Attribute details:

  • windowFixedHeightMajor:

A fixed height for the window along the major axis of the screen, that is, when in portrait.

  • windowFixedHeightMinor:

A fixed height for the window along the minor axis of the screen, that is, when in landscape.

  • windowFixedWidthMajor:

A fixed width for the window along the major axis of the screen, that is, when in landscape.

  • windowFixedWidthMinor:

A fixed width for the window along the minor axis of the screen, that is, when in portrait.

Tim Malseed
  • 6,003
  • 6
  • 48
  • 66
  • 1
    How is this not accepted answer, thanks @Tim, for other: more info on attributes taken from here http://stackoverflow.com/questions/29003591/android-fixed-size-dialog-dimension-what-is-major-minor#answer-29004580 – Nitin Misra Nov 25 '16 at 16:33
8

If it is a customizes dialog box then you can set the height and width in new created XML file only. but if you are using AlertDialog.builder then use this.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height.

And follow this Hope it may help you out.

Community
  • 1
  • 1
Tara
  • 2,598
  • 1
  • 21
  • 30
6

I found that an easier way is to set the style in styles.xml and then set android:windowMinWidthMajor and android:windowMinWidthMinor

<style name="Testing.Dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item> 
</style>
dkoukoul
  • 61
  • 2
  • 4