Development

Tiling a Background Image

Often times you’ll want to tile, or repeat, a small image as the background of an Android view or layout. This is just like using the ‘background-image’ and ‘background-repeat’ CSS options, and almost as easy. First and most importantly, you’ll need an image to tile. If you don’t have one of your own, use the one below (right click and “Save Image”).

tile.png

Now, in your project’s res/drawable path, create a file named tile_background.xml. Fill the file with the following XML:

tile_background.xml

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

Now all you have to do is set the android:background attribute of your target view or layout to the tile_background.xml drawable id, like so:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/tile_background" android:padding="16dp">

</Linearlayout>

After run you would see:

The result

Tags
Close