Lars
Lars

Reputation: 915

Make a static background image

I have a question, i want to have a static background image in my application. The image is in the res/drawable dirs of the project.

The listview and other components should not be affected.

How can i do this? Thx

Please see my below code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ListView
    android:id="@+id/kpn"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

<Button
    android:id="@+id/btn_login"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txt_username"
    android:layout_marginLeft="2dp"
    android:layout_marginTop="3dp"
    android:layout_x="2dp"
    android:layout_y="270dp"
    android:text="Login" />

<EditText
    android:id="@+id/txt_username"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/kpn"
    android:layout_marginLeft="1dp"
    android:layout_marginTop="30dp"
    android:layout_x="1dp"
    android:layout_y="220dp" />

<Button
    android:id="@+id/cancel_button"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txt_password"
    android:layout_alignTop="@+id/btn_login"
    android:layout_x="120dp"
    android:layout_y="270dp"
    android:text="Cancel" />

<EditText
    android:id="@+id/txt_password"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/txt_username"
    android:layout_marginLeft="177dp"
    android:layout_x="120dp"
    android:layout_y="220dp"
    android:inputType="textPassword" />

</RelativeLayout>

Edit:

I solved it by setting the following code in onCreate(...) of my Activity, like so:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//Set background image
View view = getWindow().getDecorView(); 
int orientation = getResources().getConfiguration().orientation; 
if (Configuration.ORIENTATION_LANDSCAPE == orientation) { 
           view.setBackgroundResource (R.drawable.yourepicture); 
} else { 
           view.setBackgroundResource (R.drawable.yourepicture); 
} 

Thx to MisterSquonk`s tip..

Upvotes: 1

Views: 3104

Answers (1)

Dimitris Makris
Dimitris Makris

Reputation: 5183

You have to apply a custom theme for your activity (or the whole application) where you will set the windowBackground item.

<style name="CustomTheme" parent="android:Theme.Light">
    <item name="android:windowBackground">@drawable/my_drawable</item>
</style>

Then, on your manifest:

<activity android:name="..." android:theme="@style/CustomTheme">

Hope this helps!

Upvotes: 2

Related Questions