Posts Tagged ‘mobie’

Android Quick Tip: Tiling a Background Image

Android Quick Tip

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”).

carbon_fibre.gif

Carbon Fiber tile

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

 version="1.0" encoding="UTF-8" ?>
 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/carbon_fibre" 
    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:

sample_layout.xml

 version="1.0" encoding="utf-8"?>
 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/tile_background"
    >
>

And if sample_layout.xml was the assigned content view for your main activity, you would see this when you started your app:

tiled