Android Making a drawable XML composed of several bitmaps

I have this background for several LinearLayouts throughout my app:

android:background="@drawable/metal_plate"

drawable\metal_plate.xml:

  • Android - failure on loading library
  • How to enable logging for apache commons HttpClient on Android
  • Android design library CoordinatorLayout, AppBarLayout and DrawerLayout
  • how to remove a task from ScheduledExecutorService?
  • Android studio logcat nothing to show
  • Padding bars remain when scrolling GridView
  • <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/diamond_metal"
        android:tileMode="repeat"
        android:dither="true">
    </bitmap>    
    

    I would like to place 4 bitmaps of screws in the 4 corners of the metal plate.
    I use the metal plate in several places, so I prefer to define it as a single drawable than having to place the 4 screws on every ocasion with a RelativeLayout.

    Is it possible to define an XML in the drawable folder to combine the tiled metal plate and the 4 screws?

    Related posts:

    How to resize a Java Script element to fit its container window
    R file is missing android
    How to differentiate between a missed call and an refuse to answer?
    ViewPager does not redraw content, remains/turns blank
    How to simulate driving a route in real device
    Getting latitude and longitude from latlng object in android
  • Using Rest to store data in Sqlite
  • android.support.v4.util.Pair vs android.util.Pair
  • Multiple TextViews update very slowly
  • How can I submit a text field in the POST request that uploads a blob to the Blobstore and retrieve it in the upload handler for the blob?
  • Google Play services out of date. Requires 7571000 but found 6774470
  • unknown error (code 14): Could not open database
  • 2 Solutions collect form web for “Android Making a drawable XML composed of several bitmaps”

    Unfortunately I can’t really test this right now, but I believe you can pull this off with a LayerListDrawable like this:

    <?xml version="1.0" encoding="utf-8"?>
    
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <bitmap
                android:src="@drawable/diamond_metal"
                android:tileMode="repeat" />
        </item>
    
        <item android:right="10dp" android:bottom="10dp">
            <bitmap
                android:src="@drawable/screw"
                android:gravity="bottom|right" />
        </item>
    
        <item android:left="10dp" android:bottom="10dp">
            <bitmap
                android:src="@drawable/screw"
                android:gravity="bottom|left" />
        </item>
    
        <item android:top="10dp" android:left="10dp">
            <bitmap
                android:src="@drawable/screw"
                android:gravity="top|left" />
        </item>
    
        <item android:top="10dp" android:right="10dp">
            <bitmap
                android:src="@drawable/screw"
                android:gravity="top|right" />
        </item>
    </layer-list>
    

    Replacing the 10dp values with whatever inset you need for the screws.

    This could probably be easily done using a NinePatch. You can create a NinePatch drawable then just set it as the background of any Layouts that you want to have the background. This just requires you create a square version of the background, then I would recommend using the Draw 9-Patch tool to make it into a .9.png for Android to use.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.