Android: Display custom dialog in center of the container

Kamal picture Kamal · May 2, 2012 · Viewed 25.1k times · Source

How to display custom dialog as a center of the container?

Dialog customdialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar); 
Window window = customdialog.getWindow();
window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
window.setGravity(Gravity.CENTER);

R.style.Theme_Translucent_NoTitleBar - is used to remove border for cutomdialog. Once i used this line the dialog will appear at the topleft corner of the tablet?

Can anyone help me to solve this issue?

Answer

Sreedev R picture Sreedev R · May 2, 2012

Change the fill_parent to wrap_content.I hope this will be the issue the dialog appear at the corner of the activity.It takes the space of whole layout.So changing this may help you to get what you realy wanted.

Window window = customdialog.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);

Use above code after dialog.show() statement