Muhammad Farhan Habib
Muhammad Farhan Habib

Reputation: 1809

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

I have a problem in setting the height and width of an Alert Dialog, below is my Code that i have written in OnCreateDialog() method:

AlertDialog dialog = new AlertDialog.Builder(context ,AlertDialog.THEME_HOLO_LIGHT)
    .setTitle("Title")        
    .setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            switch(which){

            case 0:
                //some Code
            break;
            case 1:                 
                //some Code
            break;
            }

        }
    })
    .create();


WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();


WMLP.x = 80;   //x position
WMLP.y = -100;   //y position
WMLP.height = 100;
WMLP.width = 100;

dialog.getWindow().setAttributes(WMLP);
     //OR
dialog.getWindow().setLayout(100, 100);

Kindly tell me what is the problem here in the Code. Thanks

Upvotes: 2

Views: 10005

Answers (1)

Lalit Poptani
Lalit Poptani

Reputation: 67286

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.

Upvotes: 8

Related Questions