Search code examples
androidmargindrawableandroid-xmllayer-list

Create a drawable containing another drawable and a margin


Is there a way to create a drawable in xml, which uses an image and some margin to offset it? This is for adding a drawable to a place where you can't set the margin, i.e. when changing the 'up' caret of the action bar.


Solution

  • Found out with a little searching on Android's developer page. You can create a layer-list, and in it have a single item:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
         <item
          android:left="[margin in pixels(dp)]"
          android:drawable="@drawable/side_bar_button"/>
    </layer-list>
    

    This offsets the drawable to the left by the amount of pixels you'd like. This can also be done for right, top and bottom.