Anna
Anna

Reputation: 147

How to set dialog outer margins? (Android)

I need add custom margins around dialog. Any suggestion how to do this? (dialog position top : 100 px, left 50 px, etc)

Upvotes: 5

Views: 14985

Answers (3)

LMaker
LMaker

Reputation: 1610

I know this is a old question, but if anyone still needs a help...

You can use a insetto set margins to a Custom dialog.

Create a dialog_inset.xml in your drawable folder:

<inset
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/dialog_background"
android:insetRight="15dp"
android:insetBottom="15dp"
android:insetLeft="15dp">
</inset>

Look, the android:drawableattribute is calling my dialog custom background. In my case, is a white window rounded.

This is my dialog_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
    <shape android:shape="rectangle">
        <corners android:radius="20dp"/>
        <solid android:color="#ffffff"/>
        <stroke android:color="#4c252525" android:width="1dp"/>
    </shape>
</item>
</selector>

And now you need to put this android:background="@drawable/dialog_isset" at your parent view of your custom dialog layout.

Upvotes: 13

DMan
DMan

Reputation: 268

I did it like this:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, R.style.cutomDialog);
...
...
Dialog dialog = dialogBuilder.create();

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

params.gravity = Gravity.TOP;
params.x = locationByX;
params.y = locationByY;
params.width = dialogWidth;
params.height = dialogHeight;

dialog.getWindow().setAttributes(params);

Upvotes: 4

Panther
Panther

Reputation: 9408

Try this

dialog.getWindow().getDecorView().setTop(100);
dialog.getWindow().getDecorView().setLeft(100);

Upvotes: -7

Related Questions