KISH
KISH

Reputation: 41

How to set the position of a AlertDialog off the screen at top in Android?

I want to set the AlertDialog position behind the status bar, when the content in my Dialog will increase, How to do that? I am creating a custom AlertDialog using my own layout.... Please help me out....


Below is my code, I am setting the height and x-y position of alertDialog, but still it doesnot show its effect..

AlertDialog.Builder builder = new AlertDialog.Builder(this);
       LayoutInflater inf = getLayoutInflater();
        View layout = inf.inflate(R.layout.main, null);
        builder.setView(layout);
        builder.setTitle("Add to Home screen");
    AlertDialog dialog = builder.create();
        WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
        int dialogOriginalHeight = WMLP.height;
    WMLP.height += 750;
    Log.i("XnY", "x="+WMLP.x+", y="+WMLP.y);
    WMLP.x = -10;   //x position
    WMLP.y = -10;   //y position
    Log.i("XnY", "x="+WMLP.x+", y="+WMLP.y);
    dialog.getWindow().setAttributes(WMLP);
    Log.i("POSITION", "POS::HEIGHT:"+WMLP.height);
    dialog.show();

Upvotes: 4

Views: 8964

Answers (2)

CoDe
CoDe

Reputation: 11146

WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

wmlp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
wmlp.x = 50;   //x position
wmlp.y = 50;   //y position

dialog.show();

can be a good option

Upvotes: 1

FuegoFingers
FuegoFingers

Reputation: 2199

I know this is really old but for anyone that sees this later:

x and y are being ignored because you are using default gravity.

From the docs:

X position for this window. With the default gravity it is ignored. When using LEFT or START or RIGHT or END it provides an offset from the given edge.

I needed mine to be offset from the top right so I set my gravity to 0x00000035. This is top and right. (top is 0x00000030 and right is 0x00000005). This will cause x and y to not be ignored.

alert.getWindow().setGravity(0x00000035);

Upvotes: 1

Related Questions