Felix ZY
Felix ZY

Reputation: 986

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.

Upvotes: 2

Views: 455

Answers (2)

0xC0DED00D
0xC0DED00D

Reputation: 20368

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

Upvotes: 6

Ogulcan Orhan
Ogulcan Orhan

Reputation: 5317

Firstly, you need different themes which has different backgrounds. So you may use this.setTheme method in your Activity.

Indeed I suggest you, two different layout (with different backgrounds but have same components) and using setContentView during onClick.

I hope it solves your issue.

Upvotes: 2

Related Questions