Android tiled background

The Android framework allow to create a drawable that wraps a bitmap and can be tiled, stretched, or aligned. First of all, we need the background pattern to use as background tile. The pattern can be any drawable located in the drawable directory of the application, for example :

Android background pattern

Then we need to create the drawable that wraps our pattern. BitmapDrawable can be defined in an XML file with the <bitmap> element :

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/pattern" 
    android:tileMode="repeat" />

The tileMode attribute defines the way the pattern is repeated :

disabled
Do not tile the bitmap. This is the default value.
clamp
Replicates the edge color.
repeat
Repeats the bitmap in both direction.
mirror
Repeats the shader’s image horizontally and vertically, alternating mirror images so that adjacent images always seam.
Android tiled background

Tags: