Search code examples
androidandroid-layoutandroid-intentandroid-emulatorandroid-widget

Changing position of the Dialog on screen android


I made a simple AlertDialog in my Activity:

View view = layoutInflater.inflate(R.layout.my_dialog, null);
AlertDialog infoDialog = new AlertDialog.Builder(MyActivity.this)
                    .setView(view)  
                    .create();

infoDialog.show();

With above code, the dialog shows at the (about) the center of the screen.

I am wondering, how can I customize the dialog position to make it showing just under the top Action Bar ? (Is there anyway to change the gravity or something of the dialog?) and how to do it based on my code??


Solution

  • I used this code to show the dialog at the bottom of the screen:

    Dialog dlg = <code to create custom dialog>;
    
    Window window = dlg.getWindow();
    WindowManager.LayoutParams wlp = window.getAttributes();
    
    wlp.gravity = Gravity.BOTTOM;
    wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(wlp);
    

    This code also prevents android from dimming the background of the dialog, if you need it. You should be able to change the gravity parameter to move the dialog about