Changing position of the Dialog on screen android

150,979

Solution 1

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

Solution 2

private void showPictureialog() {
    final Dialog dialog = new Dialog(this,
            android.R.style.Theme_Translucent_NoTitleBar);

    // Setting dialogview
    Window window = dialog.getWindow();
    window.setGravity(Gravity.CENTER);

    window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    dialog.setTitle(null);
    dialog.setContentView(R.layout.selectpic_dialog);
    dialog.setCancelable(true);

    dialog.show();
}

you can customize you dialog based on gravity and layout parameters change gravity and layout parameter on the basis of your requirenment

Solution 3

I found this code snippet from @gypsicoder code here

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.

Solution 4

New BottomSheetDialog:

BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);    
dialog.setContentView(YourView);

dialog.show();

Solution 5

For me, this worked out pretty well where I was trying to position my dialog somewhere exactly at the bottom of the textview where it gets selected.

public void setPosition(int yValue) {
    Window window = getWindow();
    WindowManager.LayoutParams param = window.getAttributes();
    param.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
    param.y = yValue;
    window.setAttributes(param);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
Share:
150,979
Leem.fin
Author by

Leem.fin

A newbie in software development.

Updated on July 08, 2022

Comments

  • Leem.fin
    Leem.fin almost 2 years

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

  • Leem.fin
    Leem.fin about 12 years
    Hi, thank you. I set its gravity to top, the dialog goes on the top of the screen, but it also covered my action bar, I would like the dialog on top but just under the action bar...how to adjust this?
  • Leem.fin
    Leem.fin about 12 years
    Hi, thank you. I set its gravity to top, the dialog goes on the top of the screen, but it also covered my action bar, I would like the dialog on top but just under the action bar...how to adjust this?
  • Aleks G
    Aleks G about 12 years
    You might try using wlp.x and wlp.y fields to explicitly set the location of the dialog on screen. I haven't tried it myself, but it should probably work.
  • Ramesh Solanki
    Ramesh Solanki about 12 years
    you can set margin top of layout
  • Rubberduck
    Rubberduck about 10 years
    The clearing of the dimming didn't work for me (though the rest worked like a charm). I found a solution to the dimming in another spot, in the form of: window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND‌​);
  • Kshitij
    Kshitij over 9 years
    @AleksG is there a way to also change color of dim background keeping dailog at center? I tried setting windowIsFloating=false to dailog style. But it align dailog to top!
  • Aleks G
    Aleks G over 9 years
    @Kshitij If you want to keep dialogue in the centre, you don't need any of this code - Android centres dialogues by default. As for changing the dim colour, this is controlled by the theme. You may be able to override it by including corresponding theme in your app. I never tried it though.
  • Joao Polo
    Joao Polo over 8 years
    By WindowManager source, wlp.y only works if the gravity is setted to TOP or BOTTOM
  • fWd82
    fWd82 over 7 years
    'FILL_PARENT' is deprecated. I am on API 21. As by Developer Reference
  • William
    William over 7 years
    @fWd82 so use 'MATCH_PARENT' instead
  • CopsOnRoad
    CopsOnRoad over 7 years
    Remove wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND; to get back your shadows.
  • Aleks G
    Aleks G about 6 years
    @KyleR If you look at the date of the answer, you'll realise that it was written way before API 22 (I believe, I was using API 11 at the time). When you have floating buttons (i.e. full screen app), all app UI will be behind the floating navigation buttons - always.
  • Harsh Vardhan
    Harsh Vardhan over 3 years
    You forgot to set the LayoutParams after modifying them.
  • user905686
    user905686 almost 3 years
    @HarshVardhan the parameters are modified within the object returned by getAttributes(), which is the actual object used by the window manager.