1

On an Android Activity, I want to set an image as the background in the bottom left corner. Here's how you do it: Background Image Placement

However, I also want to be able to specify that the rest of the background which is not this image has a certain color. Is this possible?

(It's so I can have a background which is one solid color with a small logo in one corner)

Thanks,

EDIT: I have done a test project which apparently should work, but it crashes for me. https://github.com/jarofgreen/AndroidTestBackground E/AndroidRuntime(213): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class

Community
  • 1
  • 1
James
  • 3,265
  • 4
  • 22
  • 28

4 Answers4

3

Yes it is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00FF00"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:layout_gravity="bottom|left"
        android:background="#FF0000"
        android:scaleType="fitXY" />

</LinearLayout>
Dmytro Danylyk
  • 19,684
  • 11
  • 62
  • 68
  • I'm assuming your ImageView should have a android:src? I already wondered if something like this would work, but My android app crashes and no stack trace appears if I do this. – James Feb 06 '12 at 15:41
  • This is working for me, android:src isn`t required for ImageView. You can easily repalce android:background to any src file. – Dmytro Danylyk Feb 06 '12 at 15:45
  • Strange. I know android:src isn`t required for ImageView, but in this case, how does the code above know which graphic to use for the logo? – James Feb 06 '12 at 15:48
  • I am setting android:background="#FF0000" color for ImageView – Dmytro Danylyk Feb 06 '12 at 15:55
  • If I do exactly what you have, but replace the 2nd android:background with an android:src I get a crash on 2.1 Emulator. Odd. Oh well, thanks for trying. – James Feb 06 '12 at 16:01
  • https://github.com/jarofgreen/AndroidTestBackground This is my attempt - can you see what is wrong with this? – James Feb 12 '12 at 23:03
  • E/AndroidRuntime(213): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class – James Feb 13 '12 at 09:07
  • Try this: http://stackoverflow.com/questions/2605999/android-how-to-track-down-the-origin-of-a-inflateexception – Dmytro Danylyk Feb 13 '12 at 10:28
  • As you can see in https://github.com/jarofgreen/AndroidTestBackground I don't use themes :-( – James Feb 13 '12 at 11:00
  • Maybe try Project->Clean, Right click on project -> Android Tools ->Fix project properties – Dmytro Danylyk Feb 13 '12 at 11:31
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/your image"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/your Color">
    </LinearLayout>
</LinearLayout>
Vajani Kishan
  • 293
  • 4
  • 13
1

I solved this by using a 9-patch image.

http://developer.android.com/guide/developing/tools/draw9patch.html

I made a 9-patch image with a wide area of background color and set the background color as the expandable area.

9-patch

Then just set this image as the background of the view!

(One thing I don't understand tho, I thought content would only be placed in the expandable area, leaving a wide margin on the left and bottom. We were going to accept that as a necessary sacrifice but actually content appears over all parts of the background. )

James
  • 3,265
  • 4
  • 22
  • 28
0

Use a relative layout with two images. The first, is your logo, which is set to be aligned in the bottom right corner (with width\height scaled to it's actual size). The second is your background image, which can be aligned however, because you will set it's width\height to fill_parent.

Booger
  • 18,579
  • 7
  • 55
  • 72
  • So use the Relative layout with 2 images in res/drawable/bg.xml and then in your normal layout set the background as "@drawable/bg". Do you have an example of this? Will be giving this a try ... – James Feb 07 '12 at 17:32