Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
XML Shape Drawable for Dialog Backgrounds. Applies a fixed outer margin to keep background from fully stretching to screen edge, and allows for additional internal padding to apply to the dialog content.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="">
<!-- Apply the margin value on the "item" element -->
<!-- This example creates a 15dp visible margin around the dialog -->
<!-- Add attributes specific to your background (e.g. corner radius, colors, etc.) -->
<!-- Set "padding" at minimum to match margin and add additional content padding as well, if desired -->
<!-- This example creates 10dp of internal padding for content -->
android:right="25dp" />
<!-- Possible example of applying the background to your application dialogs. -->
<!-- You can also set the background directly on your dialog's content view hierarchy if you have customized the theme another way. -->
<style name="Theme.CustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/background_dialog</item>

This comment has been minimized.

Copy link
Owner Author

@devunwired devunwired commented Nov 14, 2012

This is a method for implementing custom Dialog backgrounds in Android that provide outer margins and inner padding in the same fashion as developers expect from the default framework implementations. The framework achieves this through 9-patch images, which is also a great solution. This shows how to achieve the same result if your background drawable is generated in XML.


This comment has been minimized.

Copy link

@Charlie-Green Charlie-Green commented Feb 5, 2020

Helped me to realize how to add margins to shape drawables - just what the doctor ordered! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment