Search code examples
androidscaledrawable

Android enlarge transparent background of icon drawable


I have many different dynamic icons in my Android app and I would like to put a rectangular frame around all of them. I tried to use LayerDrawable but I think it scales the smaller drawable to the size of the larger one so in the end the icons overlap with the frame instead of within it. (the icon drawables are 64x64 while the frame drawable is 96x96). Is there an way to enlarge the transparent background of the icon drawables to the same size as the frame drawable without scaling the actual icon?

Any help would be appreciated!


Solution

  • You should be able to use a LayerDrawable for this; just tested it myself and it seems to work just fine. Define the frame first, then add another item with the specified insets.

    <?xml version="1.0" encoding="utf-8"?>
    
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#FF0000"/>
            </shape>
        </item>
        <item android:left="20dp" android:top="20dp" android:bottom="20dp" android:right="20dp">
            <bitmap android:src="@drawable/ic_launcher"/>
        </item>
    </layer-list>