Show AlertDialog in any position of the screen
Asked Answered
O

4

109

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

Owlet answered 29/3, 2011 at 7:4 Comment(0)
O
268

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();
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
     WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

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

 dialog.show();

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

Owlet answered 18/5, 2011 at 20:56 Comment(4)
You could also create a custom alert dialog. I just recently did this and then had to get the displays width and height and then set the X and Y based on a percentage of those values, that way the layout scaled nicely. developer.android.com/guide/topics/ui/dialogs.html#CustomDialogOmentum
It works but I had to add this: WMLP.gravity = Gravity.TOP | Gravity.LEFT; otherwise the x and y values were used as offsets from the center of the screen.Glindaglinka
Just to share the experience: The code above works only as long as the dialog can stay completely on the screen. For example, if the new y-coordinate is too big, the dialog is moved to the lowest position on the screen that still shows the whole dialog. The tricky part here is that the x and y values of the layout paramters show from then on no longer the coordinates of the top left corner of the dialog window anymore and can't be used for computing a relative movement of the window for example when reacting to a MotionEvent.Long
works like charm ..but no need to set wmlp.x = 100; //x position wmlp.y = 100; you just set gravity wmlp.gravity = Gravity.TOP | Gravity.LEFT; is enoughHerbartian
W
17

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;
Wolof answered 9/8, 2012 at 12:49 Comment(1)
I want to give around 10 dp of margin to the alert dialog whose gravity is center. Can I use the above code? If not, then is there any other way to do this?Downrange
I
8

To make the setting come info effect, I added the following code
dialog.getWindow().setAttributes(wmlp);

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

Inward answered 2/11, 2013 at 12:22 Comment(0)
H
1

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:

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

    <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>

     </style>
</resources> 

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.

Harriman answered 10/12, 2015 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.