2

Example I have 3 activities: activity1, activity2, and activity3. The activity1 contains a button in which it could change the background of activity2 and activity3 upon clicking. Is this possible? If yes how? I have an idea on using the method:

//example for activity1

public void onClick(View v) {
View background = findViewById(R.id.activity1relativeLayout);
background.setBackgroundResource(R.drawable.customBackground);

}

which can be used on a single activity. How about for manipulating multiple activities by using a single button?

Jethro Monzada
  • 104
  • 1
  • 9
  • How are you going to have 3 `Activities` being displayed at the same time? ([`ActivityGroup`](https://developer.android.com/reference/android/app/ActivityGroup.html) is deprecated now). You should use `Fragments` – Emmanuel Dec 07 '14 at 01:53
  • @Emmanuel Oh you misunderstood it pal what I mean is that activities will be displayed 1 at a time, but when you make a change on the current activity(e.g., changing the background of activity 1) then the other 2 activities' background will be changed as well. So example I click a button on activity 1 then its background will be changed and when I moved to other activities their background must have been changed as well. – Jethro Monzada Dec 08 '14 at 06:08

4 Answers4

1

You can save the color in SharedPreferences when you click the button. Then in the color changing activity's onStart read the preference and change the background color.

Button.onClick(...)

SharedPreferences sharedPref = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("background_resource", selected_background);
editor.apply();

Activity.onStart(...)

SharedPreferences sharedPref = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
int bg = sharedPref.getInt("background_resource", android.R.color.white); // the second parameter will be fallback if the preference is not found
getWindow().setBackgroundDrawableResource(bg);
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • Sir I'm not going to use plain colors for background but rather images. If it is possible to do it, can you please write a sample code for it? Thanks in advance. – Jethro Monzada Dec 08 '14 at 06:16
  • @JethroMonzada Done. How to retrieve the resource id is up to you. – Eugen Pechanec Dec 08 '14 at 11:17
  • What I meant to say: Off the top of my head: have a hardcoded `static final LinkedHashMap`, integer would be the drawable or color id, string would be a name you'd present to user. Then you would let the user pick a value from spinner and based on selected position you would save the resource id from map. – Eugen Pechanec Dec 09 '14 at 01:40
  • Ahh ok sir got that but I have one question, is it appropriate to use an ImageView or ViewPager for the background? What could the difference be? – Jethro Monzada Dec 09 '14 at 04:33
  • @EugenPechanec - no need to hold LinkedHashMap in the memory, I adviced him to name each background by sample string: "BG1","BG2"... and in the onresume (or on start) he can check the values of the SharedP.. if "BG1" - set drawable x,if "BG2" - set drawable y and so on... – Esmaeel Ibraheem Dec 09 '14 at 08:46
0

Create a static variable for holding the background resource, and get that referred in your activity. You can change that value from other activities, when you return back, you will see the updated resource.

Hope this helps, let me know if you need code logic also.

Shiv
  • 689
  • 8
  • 23
  • 1
    This is a really bad idea. You will leak the `Activities` if you do this. – Emmanuel Dec 07 '14 at 04:32
  • Can you please elaborate more on this, on why do you think there might be a leak. – Shiv Dec 07 '14 at 06:02
  • Because statics live longer than the `Activity`. See [here](http://www.curious-creature.com/2008/12/18/avoid-memory-leaks-on-android/comment-page-1/) – Emmanuel Dec 07 '14 at 06:22
0

You can have the actual activities change their backgrounds when they actually startup up. However, you can make this imperceivable to the user by using an Intent, specifically, send an extra bundle, or data, with your Intent when you start the activities. Then, based on the option that is selected in the first activity, a different piece of data is sent to the new activity, and it, from there, selects the appropriate background.

Here are some resources to look at: http://developer.android.com/reference/android/content/Intent.html How do I get extra data from intent on Android?

Community
  • 1
  • 1
AeroVTP
  • 336
  • 1
  • 12
0

if you want to change Activity A background in runtime, or make any change from outside the Activity activity2 Send broadCast From activity1 to Activity activity2, the BroadCastReceiver in activity2 make the change you want. - (this is way of changing background when it is visible) in your case, use SharedPreferences to save the background and in onResume() read the SharedPreferences :

example: (I will Write code here without testing it, but you can do)

public void onClick(View v) {
 // set background..
 SharedPreferences bgshared= getSharedPreferences("background", MODE_PRIVATE);
     SharedPreferences.Editor editor = bgshared.edit();
       editor.putString("BKGRND", "bg1").commit();
// OR     editor.putString("BKGRND", "bg2").commit();
// OR editor.putString("BKGRND", "bg3").commit();
 }

In onResume Do :

 SharedPreferences bgshared= getSharedPreferences("background", MODE_PRIVATE);
String Bgfromshared = bgshared.getString("BKGRND", "Error");
if(Bgfromshared.equals("bg1"))
   background.setBackgroundResource(R.drawable.customBackground1);
else if(Bgfromshared.equals("bg2"))
   background.setBackgroundResource(R.drawable.customBackground2);//other drawable
else if(Bgfromshared.equals("bg3"))
   background.setBackgroundResource(R.drawable.customBackground3);
else if(Bgfromshared.equals("Error")); //do no thing (put ; ).

UPDATE:(if you have more than one activity, use one SharedPreferences for all of the activities)

// you are in activity1:
public void onClick(View v) {
 // set background..
 SharedPreferences bgshared= getSharedPreferences("background", MODE_PRIVATE);
     SharedPreferences.Editor editor = bgshared.edit();
       editor.putString("BKGRND_ACTIVITY1", "bg1").commit();
// OR     editor.putString("BKGRND_ACTIVITY1", "bg2").commit();
// OR editor.putString("BKGRND_ACTIVITY1", "bg3").commit();
 } 


// you are in activity2:
public void onClick(View v) {
 // set background..
 SharedPreferences bgshared= getSharedPreferences("background", MODE_PRIVATE);
     SharedPreferences.Editor editor = bgshared.edit();
       editor.putString("BKGRND_ACTIVITY2", "bg1").commit();
// OR     editor.putString("BKGRND_ACTIVITY2", "bg2").commit();
// OR editor.putString("BKGRND_ACTIVITY2", "bg3").commit();
 } 

In onResume Do :

     SharedPreferences bgshared= getSharedPreferences("background", MODE_PRIVATE);
// if you are in activity 1 :   
 String Bgfromshared = bgshared.getString("BKGRND_ACTIVITY1", "Error");
// if you are in activity 2 : 
String Bgfromshared = bgshared.getString("BKGRND_ACTIVITY2", "Error");
    if(Bgfromshared.equals("bg1"))
       background.setBackgroundResource(R.drawable.customBackground1);
    else if(Bgfromshared.equals("bg2"))
       background.setBackgroundResource(R.drawable.customBackground2);//other drawable
    else if(Bgfromshared.equals("bg3"))
       background.setBackgroundResource(R.drawable.customBackground3);
    else if(Bgfromshared.equals("Error")); //do no thing (put ; ).
Esmaeel Ibraheem
  • 316
  • 2
  • 14
  • So sir do you mean that I need to make a SharedPreferences class for every activity? – Jethro Monzada Dec 08 '14 at 06:25
  • yes you can, but a better way is to create a key for each activity in the same SharedPreferences file. I will UPDATE the answer above for many activities. – Esmaeel Ibraheem Dec 09 '14 at 08:26
  • Sorry Sir I'm still at the office. I guess I'll have to come back at this when I got home. I'll give you an update as soon as I've tested your answer. Thank you. – Jethro Monzada Dec 09 '14 at 09:43
  • Sorry sir but I think your answer doesn't correspond to my question. The app being develop needs only a single button to manipulate 3 activities since other activities are already populated with other buttons. – Jethro Monzada Dec 10 '14 at 00:11
  • Think of it like this, Activity 1 has the only button so thus it contains a onClick method. Activiy 2 and 3 are just plane activities with no button, textview, imageview, etc. – Jethro Monzada Dec 10 '14 at 01:10
  • Ok, the Activity with the button should have a value that tell which activity to change (2 Or 3 ...), then in the same Onclick according to the target activity you put the change: if (target=="activity2) editor.putString("BKGRND_ACTIVITY2", "bg1").commit(); – Esmaeel Ibraheem Dec 10 '14 at 07:00