0

I want to change the background colour of an activity using AlertDialog. Here is what I have done:

private SharedPreferences prefs;    
private static final String SELECTED_ITEM = "SelectedItem"; 
private Editor sharedPrefEditor;

btnchangeColor = (ImageButton) findViewById(R.id.btnchangeColor);
btnchangeColor.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        final CharSequence[] items={"Red","Green","Blue", "Yellow", "#EE82EE"};

        AlertDialog.Builder builder = new AlertDialog.Builder(
                ContentView_2.this);

        builder.setTitle("Pick a Color");
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() { 

            @Override
            public void onClick(DialogInterface dialog, int which) {}
        });

        builder.setSingleChoiceItems(items, getSelectedItem(), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                wvContent = (WebView) findViewById(R.id.wvContent);
                //LinearLayout ll=(LinearLayout)findViewById(R.id.wvContent); 

                if("Red".equals(items[which]))
                {
                    wvContent.setBackgroundColor(Color.RED);
                }
                else if("Green".equals(items[which]))
                {
                    wvContent.setBackgroundColor(Color.GREEN);
                    }
                else if("Blue".equals(items[which]))
                {
                    wvContent.setBackgroundColor(Color.BLUE);
                    }
                else if("Yellow".equals(items[which]))
                {
                    wvContent.setBackgroundColor(Color.YELLOW);
                    }
                else if("#EE82EE".equals(items[which]))
                {
                    wvContent.setBackgroundColor(Color.rgb(184,184,184));
                    }
                saveSelectedItem(which);
            }
        });
        builder.show();

    }});
    }

private int getSelectedItem() {
    if (prefs == null) {
        prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
    }
    return prefs.getInt(SELECTED_ITEM, -1);
}

private void saveSelectedItem(int which) {
    if (prefs == null) {
        prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
    }
    sharedPrefEditor = prefs.edit();
    sharedPrefEditor.putInt(SELECTED_ITEM, which);
    sharedPrefEditor.commit();
}

I also add this code to OnCreate:

wvContent = (WebView) findViewById(R.id.wvContent); 
    wvContent.setBackgroundColor(getSelectedItem());

The colour is selected and the web view (wvContent) changes to chosen colour, BUT this is not saved to and/or loaded from Shared Preferences. It returns to its default colour when the activity is relaunched.

So the question is: How can properly I save the selected colour to SharedPreferences and load it when activity relaunches?

Many thanks for help.

Niamh Doyle
  • 1,909
  • 7
  • 30
  • 42

1 Answers1

3

Currently you are saving position of selected item instead of color code in SharedPreferences. do it as:

wvContent = (WebView) findViewById(R.id.wvContent);
int bg_color=Color.TRANSPARENT;
 if("Red".equals(items[which]))
   {
       wvContent.setBackgroundColor(Color.RED);
       bg_color=Color.RED;
   }
 else if("Green".equals(items[which]))
  {
      wvContent.setBackgroundColor(Color.GREEN);
       bg_color=Color.GREEN;
  }
........
// save color in SharedPreferences
 saveSelectedItem(bg_color);

Now getSelectedItem() method return color code for previously selected value from spinner.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Superb! This is exactly what I have been looking for. Thank you so much. – Niamh Doyle Mar 22 '14 at 08:14
  • I have no idea why in Jelly Bean it changes the colour right when a radio (colour) is selected, but it doesn't in Ice Cream until the activity is relaunched. Is this the problem of the Android versions or of my code? – Niamh Doyle Mar 23 '14 at 10:56