Created
November 14, 2012 20:09
-
-
Save devunwired/4074446 to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> | |
<!-- Apply the margin value on the "item" element --> | |
<!-- This example creates a 15dp visible margin around the dialog --> | |
<item | |
android:top="15dp" | |
android:bottom="15dp" | |
android:left="15dp" | |
android:right="15dp"> | |
<shape | |
android:shape="rectangle"> | |
<!-- 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 --> | |
<padding | |
android:top="25dp" | |
android:bottom="25dp" | |
android:left="25dp" | |
android:right="25dp" /> | |
</shape> | |
</item> | |
</layer-list> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> | |
</style> |
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
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.