sat sat - 6 months ago 238
Android Question

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

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();


I am using the above code to display an Alert Dialog. By default, it fills the screen in width and wrap_content in height.

How can I control the width and height of default alert dialog ?

I tried:

alert.getWindow().setLayout(100,100); // It didn't work.


How to get the layout params on the alert window and manually set the width and height?

Answer

I dont know whether you can change the default height/width of AlertDialog but if you wanted to do this, I think you can do it by creating your own custom dialog. You just have to give android:theme="@android:style/Theme.Dialog" in the android manifest.xml for your activity and can write the whole layout as per your requirement. you can set the height and width of your custom dialog from the Android Resource XML.