2

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.

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265

1 Answers1

2

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).

Ivan
  • 4,186
  • 5
  • 39
  • 72
  • Thank you Ivane, I know this but let say that I have 20 activities, how can I change the bacground for all of them when the user set's a background image. – Milos Cuculovic Jun 05 '12 at 11:54
  • Thank you Ivan, and now, when the user change the background image and exit the app. Then he will come back, there will appear one more time the old background, not the new one? – Milos Cuculovic Jun 05 '12 at 12:11
  • No, if you save the background using sharedpreferences, it is stored as a persistent configuration parameter, then if you read the SharedPreferences during startup of the application (usually in OnCreate) you can set the background image with user choice. – Ivan Jun 05 '12 at 12:23
  • @Ivan Instead of some resources, I am trying to do this with user's picture from gallery. It works fine except on some devices (out of memory error). I am loading the bitmap and doing `imageView.setBitmap(myBitmap)`. Any idea what's wrong here ? – Thomas Mar 24 '14 at 09:17
  • mmm this is just a guess, but usually the bitmap is a huge format, and maybe you are trying to load an image that is too big, and it cause the out of memory error. – Ivan Mar 24 '14 at 09:28