9

I am trying to get an image on the background to tile until the background is full.

My current code is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/cartoonclouds"
    android:contentDescription="@string/desc"
    android:tileMode="repeat" />
  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/hello" />
  </LinearLayout>
</RelativeLayout>

However that just makes the image covering (not tiled) bottom to top, but NOT left to right. What should I be doing?

Edit: Attempted the XML version:

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:tileMode="repeat"
    android:src="@drawable/cartoonclouds" />

The main.xml could find the cartoonclouds in the SAME folder as that XML file, but could not find that XML file.

1 Answers1

36

First, place your image in a file like res/drawable/cloud.png. This makes it accessible to your app as @drawable/cloud. But it doesn't tile (yet).

Next, you should define a bitmap resource(1) with android:tileMode="repeat". For example, you can define it on mytileablebitmap.xml:

<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:tileMode="repeat" 
android:src="@drawable/cloud"/>

This is referencing your original tile image (drawable/cloud) and making a Drawable that knows how to tile itself to fill the available space.

Then, when you want to use the tiled background, use "@drawable/mytileablebitmap".

(1): http://developer.android.com/guide/topics/resources/drawable-resource.html

Janusz
  • 187,060
  • 113
  • 301
  • 369
Bruno Oliveira
  • 5,056
  • 18
  • 24
  • 2
    Ok, I did what you said, however; in my main.xml it recognises `@drawable/cloud` but not `@drawable/mytileablebitmap` when both exist, one as .jpg and one as .xml. (gives me an error in the XML file) –  Jan 18 '12 at 02:16
  • 10
    Oh, and I found that if the idea is to use this tiling drawable in an ImageView, then I found it necessary to set it's `ScaleType` to `fitXY`. I also altered the bitmap drawable - set `android:gravity` to `fill_vertical` where I needed tilling to along the Y-axis, and `fill_horizontal` where I needed tilling along the X-axis. That might help someone. – JWL Aug 20 '13 at 08:39
  • @nemesisfixx : ScaleType to fitXY was the only part which I was missing. This is one of the most important point ! – Nishant Chauhan Oct 10 '17 at 09:21