Felix ZY Felix ZY - 4 months ago 9
Android Question

Change app background

I'm currently making an android app in which I'd like the user to be able to change the background image.
I've got 3 images and a screen where I can choose a picture and a button for applying.

The issue:
I can allow the user to see all images in the way I want, but I don't know how to set the selected image as app background.

What I want to do:
I want the user to click a button, which exports the selected image to "bakgrund.png" in "/res/drawable-mdpi" and replaces the current one. This would allow me to easily integrate the background switcher. Renaming of current files also works.

PS: My current background images are located in /res/drawable-mdpi named 1.png 2.png and 3.png.

Answer

Easiest way would be to call the setBackgroundResource(image_id) method on the root layout. Like if you have a LinearLayout which has android:id="@+linear" as the root layout in the layout xml, then this code will help:-

LinearLayout linearLayout=(LinearLayout) findViewById(R.id.linear);
linear.setBackgroundResource(R.drawable.1);//call this in the OnClickListener's OnClick Method