Akanksha Kapoor
Akanksha Kapoor

Reputation: 35

Get actual offset of Android Dialog Window

Here is a simple alert dialog. Default gravity is center

AlertDialog.Builder alertBuild = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("Sample Alert")
                        .setMessage("This is sample alert dialog")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                        .setNegativeButton(android.R.string.no, null);

                AlertDialog alert = alertBuild.create();
                alert.show();

I want to get the actual distance of the dialog window from the top of the screen.

I tried setting the below attribute.

WindowManager.LayoutParams attributes = alert.getWindow().getAttributes();
                attributes.y = 205;
                alert.getWindow().setAttributes(attributes);

I am able to get the window's distance from top[alert.getWindow().getAttributes().y].

But when I set the gravity as any of the below, x and y become 0.

attributes.gravity= Gravity.CENTER;
attributes.gravity= Gravity.BOTTOM;

enter image description here

Specifically not working for android.support.design.widget.BottomSheetDialog

Can someone help with how do I get the actual dialog window distance from the top of the screen?

Upvotes: 0

Views: 574

Answers (0)

Related Questions