Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a background image to an Android Activity?

Tags:

My goal is to develop a GUI application on top of a background image with buttons in specific places on the image. The first step is to display the background.

The image can be displayed with resources and is described in several FAQs including this one:

how-to-add-background-image-to-activity

It compiles and runs without errors, but the background is black.

Here is the main.xml file:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/rootRL" 
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@drawable/background">
</RelativeLayout>

The image has been in png, 9.png, and jpg format with basenames of 'main' and 'background'. It builds but does not display. Making clean and recompiling does not help.

This behavior occurs on both the emulator and on hardware -- an Atmel AT91SAM9M10-G45-EK. The SDK version is 2.0.1.

Thank you for your help.

like image 596
markjuggles Avatar asked Oct 12 '10 15:10

markjuggles


People also ask

Can activity run in background android?

However, activity can't be run unless it's on foreground. In order to achieve what you want, in onPause(), you should start a service to continue the work in activity.

How do you set a picture as your background on an android?

Go to the 'Photos' app and select the photo you want to use. Click on the share icon on the lower left corner of the screen, then select 'Use as Wallpaper. ' Then choose to set the photo as either the lock screen, home screen or both.

What is background activity in Android?

"Allow background activity", under Battery, refers to whether apps are allowed to run in the background (when you're not using them onscreen, or the app is not visible to you) or not.


1 Answers

Problem solved: The code was based on code that had a text view and set up the window dynamically.

The Hello Android example does call setContentView(R.layout.main) which was the clue I needed.

Of course the background in defined in res/layout/main.xml so it is necessary to call setContentView(R.layout.main) to get those settings.

like image 54
markjuggles Avatar answered Sep 27 '22 23:09

markjuggles