Reputation: 20223
I have an android app with an background image. My goal is to create 3 or 4 background images and give the possibility to the user to change the background.
Actually, I have 4 images named bg1.png, bg2.png, bg3.png and bg4.png. I have think about something to declare an string value "background " in Strings.xml with the name of the image. The name of the background image will allways be take from the background string and when the users will change the background, this wil change only the value of the string background.
Is this idea good or is there something easyer? How could I set the background of my layout in the xml layout file with the "background" string's value ? Shuld i do this programatically?
Thank you.
Upvotes: 2
Views: 752
Reputation: 4234
I think if you want to change the background to the activity layout, you can do it using setBackground method for the Layout for example:
activityLayout = (LinearLayout)findViewById(R.id.tableLayout1);
activityLayout.setBackgroundDrawable(getResources().getDrawable(R.id.somedrawable))
You can store the background image using for example SharedPreference, then when you launch an activity you read the preference that contains the background. For example when the user choice the background:
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefEditor.putInt("backgroudResourceId", userchoice);
prefEditor.commit();
And when an activity start, you must read the resourceId from the SharedPreference:
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
drawableid = myPrefs.getInt("backgroundResourceId", defaultvalue);
yourlayout.setBackground(drawableid);
where defaultvalue is the default value if the preference is not set. yourLayout is supposed to be initialized (in the same way of activity layout).
Upvotes: 2