ImagineThat ImagineThat - 4 months ago 18
Java Question

Changing background color of all activities

I have a "Settings" activity in which you are shown 5 different color options to change the background or text or button color of the app to.

I'm focusing on simply the background change right now. From what I've read, I can do something like this using SharedPreferences:

Right now in my Settings class, I have the following code for each color option:

ImageButton changeBgRed = (ImageButton) findViewById(R.id.bgRed);
changeBgRed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("background", Color.RED);
editor.commit();
}
});


What confuses me is how to code my other classes so that they read in the color from the SharedPreference and change the background.

For my HomeScreen class, I have the following code/idea (I imagine the code could be copied/pasted into other activities for the most part):

@Override
protected void onResume(){
super.onResume();
background = (RelativeLayout) findViewById(R.id.rootLayout);
SharedPreferences settings = getSharedPreferences("Background", Context.MODE_PRIVATE);
if(settings.getInt("background", Color.RED) == Color.RED)
background.setBackgroundColor(Color.RED);
else
background.setBackgroundColor(Color.WHITE);


I figure I would have more cases for the other colors. But right now, this doesn't work; when I run the app the background of the home screen starts red.

Am I not quite understanding how SharedPreferences works? Please guide me in the right direction.

Answer

As stated in Activity documentation, the getPreferences() method from the Activity class:

Retrieve a SharedPreferences object for accessing preferences that are private to this activity. This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.

So you are actually setting the color only for the Settings Activity, and you can't access this value from another Activity.

To fix this you could try the following code in the Settings class:

ImageButton changeBgRed = (ImageButton) findViewById(R.id.bgRed); 
changeBgRed.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view){

        // Changed the line below to get the same preferences used in Home Screen
        SharedPreferences prefs = getSharedPreferences("Background", Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt("background", Color.RED);
        editor.commit();
    }
});

This will be your Home Screen code, considering you want the white color background as default, and by clicking the changeBgRed view on Settings screen, it will turn your Home screen background red. Remember to uninstall your app and install again whenever you want to go back to the default state (white background)

@Override
protected void onResume(){
    super.onResume();
    background = (RelativeLayout) findViewById(R.id.rootLayout);
    SharedPreferences settings = getSharedPreferences("Background", Context.MODE_PRIVATE);
    if(settings.getInt("background", Color.WHITE) == Color.RED)
        background.setBackgroundColor(Color.RED);
    else
        background.setBackgroundColor(Color.WHITE);