Skip to content Skip to sidebar Skip to footer

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)

Solution 1:

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"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="false"><shapeandroid:shape="rectangle"><cornersandroid:radius="20dp"/><solidandroid:color="#ffffff"/><strokeandroid: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.

Solution 2:

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);

Solution 3:

Try this

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

Post a Comment for "How To Set Dialog Outer Margins? (android)"