How to set dialog outer margins? (Android)

Anna picture Anna · Oct 13, 2014 · Viewed 9.4k times · Source

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

Answer

LMaker picture LMaker · Feb 27, 2018

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.