How to Control the Width and Height of the Default Alert Dialog in Android

How to control the width and height of the default Alert Dialog in Android?

Only a slight change in Sat Code, set the layout after show() method of AlertDialog.

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.

Or you can do it in my way.

alertDialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 150;
lp.height = 500;
lp.x=-170;
lp.y=100;
alertDialog.getWindow().setAttributes(lp);

Set Height of Alert Dialog

For what it's worth, using your code (just the dialog builder, not the window-tweaking bit at the end) I get:

import android.app.AlertDialog - expands properly at Default font size in the system settings, stays small and scrolls at Largest. (Almost like it's trying to maintain a consistent size, based on the space needed for Default)

import androidx.appcompat.app.AlertDialog - expands properly at Default and Largest

That's using appcompat:1.4.0 with an old test project on an API 30 emulator, should be the same on the latest version I expect!

How to set the height and width of the Default Alert Dialog

You need to set the parameters for Height & Width after alertDialog.show();

So you need to call,

alertDialog.getWindow().setLayout(100, 100);

After calling

alertDialog.show();

For more check this answer.

How do I change the width and height of AlertDialog in Kotlin

You can achieve that by using this

loginProgressDialog.window?.setLayout(100, 100)

You should only use it right after showing the AlertDialog using loginProgressDialog.show()

How to get the width and height of AlertDialog - Flutter Web

I can't understand what your purpose is, rather specifying a size getting it's measurement.

Within the context of alert dialog you can place other components inside it, using the following. Using the following code you can get the measurements inside the UI component that you consider as context.

alert_dialog_height=MediaQuery.of(context).size.height;
alert_dialog_width=MediaQuery.of(context).size.width;

if you need to place something in the middle height of alert dialog just use

alert_dialog_height/2


Related Topics



Leave a reply



Submit