Show AlertDialog in any position of the screen

The question:

When we show an AlertDialog in android it shows in the center of the screen. Is there any way to change the position?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

After searching in various post I have found the solution.

The code is posted below:

private CharSequence[] items = {"Set as Ringtone", "Set as Alarm"};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {

            if(item == 0) {

            } else if(item == 1) {

            } else if(item == 2) {


     AlertDialog dialog = builder.create();
     WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

 wmlp.gravity = Gravity.TOP | Gravity.LEFT;
 wmlp.x = 100;   //x position
 wmlp.y = 100;   //y position;

Here x position’s value is pixels from left to right. For y position value is from bottom to top.

Method 2

If you for example want to move the progressdialog a bit further down, and not set the exact pixel position, then this is sufficient:

progressDialog.getWindow().getAttributes().verticalMargin = 0.2F;

Method 3

To make the setting come info effect, I added the following code

after change the value of wmlp in gypsicoder‘s answer, or the setting of wmlp doesn’t take into effect by my test.

Method 4

These answers will move the position of the AlertDialog, however, the position of the displayed dialog will also include the padding around the dialog.

If you want to get rid of this padding (for example, to place your dialog flush against the bottom of the screen), you will also need to override the default AlertDialog style in your styles.xml to set the windowBackground to null, like so:

    <!--  Example app theme - mine uses the below -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:alertDialogTheme">@style/MyDialogTheme</item>

    <style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!-- Full width -->
        <item name="android:layout_width">fill_parent</item>

    <!-- Null window background kills surrounding padding -->
        <item name="android:windowBackground">@null</item>
        <item name="android:windowNoTitle">true</item>


As well as setting the Window.LayoutParameters as described in the accepted answer.

Special shout-out to @David Caunt whose answer at: remove border, padding from Dialog completed this picture.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment