0

I am attempting to add a tiled image as a background of the program.

The code that I am currently using in main.xml, which is crashing:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/cartoonclouds"
android:contentDescription="@string/desc" >
<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>
</ImageView>

However, when the ImageView is removed and the xmlns is moved to the LinearLayout, it functions fine (without, of course, the image).

I can't see any related errors in LogCat.

More information:

Using Eclipse and Android 2.2, API 8. The program runs but crashes instantly.

2 Answers2

0

You should not add a linear layout inside an imageview. Have a linearlayout at the top as root and then add the image view in there

rfsk2010
  • 8,571
  • 4
  • 32
  • 46
0

You can not put anything in an ImageView. It's not a layout. Use the background attribute of the LinearLayout to set the background to your image.

Another option would be to wrap everything in a RelativeLayout. Separate the ImageView and the LinearLayout. The image will fill the RelativeLayout and the LinearLayout will be on top of it.

Example:

<?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" />
  <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>
DeeV
  • 35,865
  • 9
  • 108
  • 95
  • Did the job perfectly. However, how do I get the image to tile? –  Jan 17 '12 at 16:25
  • That I don't know. I've never tried. With `ImageView` it merely scales images. The `background` attribute does the same thing. This might warrant another question because I'm curious if it's possible too. – DeeV Jan 17 '12 at 16:30
  • http://stackoverflow.com/questions/8899222/tiling-images-in-android-with-imageview-in-xml –  Jan 17 '12 at 17:46