1

I want the dialog doesn't stretch to fit width of display screen. I want the dialog had about 90% of screen width and center its. How can I do it?

Thank you very much ^^

T.Dack
  • 29
  • 1
  • 3

3 Answers3

9

You can give style with below attributes to get dialog size as percentage of screen.

<style name="YourDialogTheme">
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
</style>

Then Apply theme as below in your dialog:

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.YourDialogTheme));

See windowMinWidthMinor and windowMinWidthMajor

pRaNaY
  • 24,642
  • 24
  • 96
  • 146
4

For the easiest way you can do something like this:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/defaultMargin"
android:paddingRight="@dimen/defaultMargin">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</RelativeLayout>

also, this link can referred

Deepak Gupta
  • 552
  • 7
  • 22
1

Here is the example to set margins in dialogs

final Dialog dialog = new Dialog(context);
// ... 

// e.g. top + right margins: 
dialog.getWindow().setGravity(Gravity.TOP|Gravity.RIGHT);
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.x = 100; // right margin
layoutParams.y = 170; // top margin
dialog.getWindow().setAttributes(layoutParams);

// e.g. bottom + left margins: 
dialog.getWindow().setGravity(Gravity.BOTTOM|Gravity.LEFT);
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.x = 100; // left margin
layoutParams.y = 170; // bottom margin
dialog.getWindow().setAttributes(layoutParams);
Mahesh Babariya
  • 4,560
  • 6
  • 39
  • 54
  • It doesn't work for me. I try it before and if I want to set layout margin for only left and right. What gravity I have to set ?. And I should use this code before or after dialog.show() and dialog.setContentview() function – T.Dack Feb 25 '17 at 04:19