Thomas Thomas - 1 year ago 54
Android Question

Shared Preferences item store

I am reposting this question since I am still stuck at this.

So I am creating a simple android application. I have a menu item which has 3 check boxes in it. As soon as my app starts, the user can select one or more from the given check boxes.but, I want to save the user selection for further reference. I achieved this for 1 of the check boxes using shared preferences but I am unable to achieve this if more than 1 check boxes are selected. Here is my code:

public class MainActivity extends AppCompatActivity {
static Switch swch;
public static boolean red = false;
public static boolean blue = false;
public static boolean green = false;

private static final String TAG = "MainActivity";

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
SharedPreferences settings = getSharedPreferences("settings", 0);
boolean isChecked = settings.getBoolean("checkbox", false);

MenuItem item = menu.findItem(R.id.red);
// MenuItem item1 = menu.findItem(R.id.blue);
// MenuItem item2 = menu.findItem(R.id.green);
item.setChecked(isChecked);
// item1.setChecked(isChecked);
// item2.setChecked(isChecked);
//return super.onCreateOptionsMenu(menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
SharedPreferences settings = getSharedPreferences("settings", 0);
SharedPreferences.Editor editor = settings.edit();
switch (item.getItemId()) {
case R.id.red:
item.setChecked(!item.isChecked());

editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
case R.id.blue:
item.setChecked(!item.isChecked());

editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;
case R.id.green:
item.setChecked(!item.isChecked());

editor.putBoolean("checkbox", item.isChecked());
editor.commit();
return true;

default:
return super.onOptionsItemSelected(item);
}
}


How do I achieve this for all the switch case options using shared preferences?

Thanks.

Answer Source

Update your two methods - onCreateOptionsMenu and onOptionsItemSelected as shown below.

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main_menu, menu);
        SharedPreferences settings = getSharedPreferences("settings", 0);
        boolean red = settings.getBoolean("red", false);
        boolean blue = settings.getBoolean("blue", false);
        boolean green = settings.getBoolean("green", false);

        MenuItem item = menu.findItem(R.id.color1);
        MenuItem item1 = menu.findItem(R.id.color2);
        MenuItem item2 = menu.findItem(R.id.color3);
        item.setChecked(red);
        item1.setChecked(blue);
        item2.setChecked(green);
        //return super.onCreateOptionsMenu(menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        SharedPreferences settings = getSharedPreferences("settings", 0);
        SharedPreferences.Editor editor = settings.edit();
        switch (item.getItemId()) {
            case R.id.color1:
                item.setChecked(!item.isChecked());


                editor.putBoolean("1", item.isChecked());
                editor.commit();
                return true;
            case R.id.color2:
                item.setChecked(!item.isChecked());

                editor.putBoolean("2", item.isChecked());
                editor.commit();
                return true;
            case R.id.color3:
                item.setChecked(!item.isChecked());

                editor.putBoolean("3", item.isChecked());
                editor.commit();
                return true;
            case R.id.item_accessibility_services:
                startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download