0

I have an app that uses a color picking dialog to change the app background color, and the text color. The color picker works fine, but when the app closes for any reason it reverts back to default.

I have checked: http://developer.android.com/guide/topics/data/data-storage.html#pref and Android Shared preferences example

the below code is the result of my research. The problem I'm having is all my text fields start off without text color, when i use the color dialog the colors change just fine, but are not saved.

Any pointers on where I'm going wrong would be greatly appreciated.

public class TipCalculator extends ActionBarActivity implements ColorPickerDialog.OnColorChangedListener {

    //UI element objects to be manipulated.
    TextView tipper;
    TextView diner;
    /*few TextView items left out to save space*/
    int color;
    RelativeLayout RLayout;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tip_calculator);

        preferences = getSharedPreferences("TipCalculator", MODE_PRIVATE);
    //color = preferences.getInt("TipCalculator", color); did not change anything
    color = preferences.getInt("bg_color", color);


        RLayout = (RelativeLayout) findViewById(R.id.RLayout);//layout object for background color manipulation

        bill = (EditText) findViewById(R.id.bill_amount_text);//UI elements placed in objects for text color manipulation
        billNtip = (TextView) findViewById(R.id.bill_tip_text);
        /*few TextView items left out to save space*/

        bill.setSelection(bill.getText().length());
        bill.addTextChangedListener(billWatcher);
    }

    @Override
    public void onPause(){// onStop does not seem to change how the app currently runs
        super.onPause();

        /*preferences = getSharedPreferences("TipCalculator", MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit(); does not work*/

    SharedPreferences.Editor editor = getSharedPreferences("TipCalculator", MODE_PRIVATE).edit();
    //editor.putInt("TipCalculator", color); does not change anything
    editor.putInt("bg_color", color);
    editor.commit();


        tipper.setTextColor(color);
        bill.setTextColor(color);
        /*few TextView items left out to save space*/

    }
    @Override
    public void colorChanged(String key, int color) {
        color = newColor;

        if (decide.equals("font"))
        {
            tipper.setTextColor(color);
            bill.setTextColor(color);
            /*few TextView items left out to save space*/
        }
        else if (decide.equals("background"))
        {
            RLayout.setBackgroundColor(color);
        }
    }
Community
  • 1
  • 1
ComSof-I
  • 127
  • 1
  • 1
  • 8

2 Answers2

0

Save the color to the SharedPreferences in onPause() instead of onResume(), like this:

@Override
public void onPause() {
    SharedPreferences.Editor editor = getSharedPreferences("TipCalculator", MODE_PRIVATE).edit();
    editor.putInt("bg_color", color);
    editor.commit();

    super.onPause();
}

onResume() is called when your app resumes, so if you exit but don't resume, the value won't get saved.

Somewhere in your initialization you will also need to read the color value out of the SharedPreferences e.g:

preferences = getSharedPreferences("TipCalculator", MODE_PRIVATE);
color = preferences.getInt("bg_color", android.R.color.white);  // default to white or whatever color you want
tipper.setTextColor(color);
bill.setTextColor(color);
samgak
  • 22,290
  • 4
  • 50
  • 73
  • Thank you for the tip. Solved part of the problem. Now the text fields show up as expected. Unfortunately the color changes do not persist. – ComSof-I Apr 13 '15 at 20:10
  • I did some code searching and found that the color in OnResume() is not the same as the "int color" in colorChanged(). – ComSof-I Apr 13 '15 at 21:17
  • Change the name of the second parameter of colorChanged() to newColor and set color = newColor; at the start of the function – samgak Apr 13 '15 at 22:34
  • Still not working proper. I made the change suggested, the program still changes all the background colors as expected but when i use the back button- back to default. This next question might make me sound really bad but in this line of code editor.putInt("bg_color", color); the "bg_color" is nowhere else in my project, I just saw it used in some examples and couldn't find a reference to it in them either, so figured it was required as it is. is it? – ComSof-I Apr 14 '15 at 08:09
  • Are you actually reading the values from the SharedPreferences anywhere? That's the key you need to read the value. See the edit to my answer. – samgak Apr 14 '15 at 08:11
  • Thanks for the help with this. Added that line of code to OnCreate() and then to OnPause() no changes in how the app works. Changing "bg_color" to "TipCalculator" also did nothing – ComSof-I Apr 14 '15 at 09:53
  • No problem. Writing the value to SharedPreferences is done in onPause. Reading is done in onCreate. Can you edit your question to include your updated code please? – samgak Apr 14 '15 at 11:11
  • It doesn't look like you are setting the color on the tipper and bill objects after you load it. In onCreate() after you call getInt() add these lines: tipper.setTextColor(color); bill.setTextColor(color); You will need to initialize these objects first obviously. – samgak Apr 15 '15 at 01:03
0

It doesn't look like you are setting the color on the tipper and bill objects after you load it. In onCreate() after you call getInt() add these lines: tipper.setTextColor(color); bill.setTextColor(color); You will need to initialize these objects first obviously. – samgak

This was the final thing I was missing.

Thanks again for the help.

ComSof-I
  • 127
  • 1
  • 1
  • 8