Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set dialog's position?

I have made a custom dialog.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="myBackgroundStyle"
        parent="@android:style/Theme.Translucent.NoTitleBar" />
</resources>

screenshot

Dialog dialog = new Dialog(this, R.style.myBackgroundStyle);
dialog.setContentView(R.layout.dialog);

dialog.show();

WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.y = 225; params.x = 225;
params.gravity = Gravity.TOP | Gravity.LEFT;       
dialog.getWindow().setAttributes(params); 

But the problem is that it appears in the top left corner and I can't find a way to place it where I need it. params.y=225; params.x=225; somehow don't affect it.

Any ideas?


edit: If I have the xml like that ( style/Theme.Dialog ), then the parameters and location work fine, but a modal shadow appears. Is there a way to remove it?

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="myBackgroundStyle" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">false</item>
    </style>
</resources>
like image 447
Roger Avatar asked Aug 08 '11 08:08

Roger


People also ask

How do I change my dialog flutter position?

You can Use Align widget and align your dialog widget as per your need. Here in example i am setting it to the bottomCenter that is Alignment(0, 1) . Example code: Align( alignment: Alignment(0, 1), child: Material( shape: RoundedRectangleBorder(borderRadius: BorderRadius.

How do I set margin to dialog in android programmatically?

requestWindowFeature(Window. FEATURE_NO_TITLE); dialog. getWindow(). setBackgroundDrawable(new ColorDrawable(0)); LayoutInflater inflator = (LayoutInflater) getApplicationContext() .

How can I show alert dialog on top of any activity in Android app?

setMessage() is used for setting message to alert dialog. setIcon() is to set icon to alert dialog. The following code will create alert dialog with two button. setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog.


1 Answers

Try creating a new set of parameters:

WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.y = 225; params.x = 225;
params.gravity = Gravity.TOP | Gravity.LEFT;       
dialog.getWindow().setAttributes(params);

Edit: if you want to preserve the window's attributes, you could try adding this as the second line:

params.copyFrom(dialog.getWindow().getAttributes());

However, note that the copyFrom method is completely undocumented, so I have no idea if it does what it sounds like it does.

like image 109
Felix Avatar answered Oct 11 '22 23:10

Felix