I was working on a android project where I had to change background image in a layout at at run time. For instance, the user selects an image and it becomes the background of the layout. It is fairly simple to do in Android. See below on how I achieved it:

I have a Frame Layout in which I have defined a background in my case the image file name is “wooden.png” so I have, ‘ android:background=”@drawable/wooden” ‘ in the layout definition. By having this line in my layout, Android will use the wooden.png  that is in the “drawable” directory as the background for the activity .

See below for xml extract:

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

.. ...



In the code if I want to change the background I could achieve this by calling “setBackgroundResource” method on the layout.

See below for an example.


View mainLayout = findViewById(R.id.main_layout); // getting the layout

mainLayout.setBackgroundResource(R.drawable.stone); // changing the background to 'stone.png'