0

I am new to android development I created a checkbox and how to save check/uncheck using share preference

final Button add = (Button) findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                addNewBubble();
                add.setEnabled(false);
            }
        });

        CheckBox checkBox = (CheckBox)findViewById(R.id.add_fb);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                isCheckedValue = isChecked;

            }
        });

    }


private void addNewBubble() {
        BubbleLayout bubbleView = (BubbleLayout)LayoutInflater.from(MainActivity.this).inflate(R.layout.bubble_layout, null);
        bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener() {
            @Override
            public void onBubbleRemoved(BubbleLayout bubble) {
                finish();
                System.exit(0);



            }
        });
        bubbleView.setOnBubbleClickListener(new BubbleLayout.OnBubbleClickListener() {

            @Override
            public void onBubbleClick(BubbleLayout bubble) {
                Intent in = new Intent(MainActivity.this, PopUpWindow.class);
                in.putExtra("yourBoolName", isCheckedValue );
                startActivity(in);

            }
        });
        bubbleView.setShouldStickToWall(true);
        bubblesManager.addBubble(bubbleView, 60, 20);
    }

There are two Check boxes in this code add and add_fb I want to make the app remember if the checkbox are checked or unchecked

Gladwin James
  • 493
  • 2
  • 10
  • 21
  • you can refer to this answer if you dont know how to use `sharedPreference` at all http://stackoverflow.com/a/23024962/2792812 – himanshu1496 Aug 16 '16 at 12:36
  • Please explain the scenario, you want to save data and reuse in application re launch or just want to pass it some other activity. – Ramit Aug 16 '16 at 12:48
  • @Ramit Yes these check-boxes are linked to button in different activity when the checked box is checked the button should appear(these parts are taken care) but when I reenter the app the check box is unchecked and the button is not shown.... – Gladwin James Aug 16 '16 at 17:43
  • `in.putExtra("yourBoolName", isCheckedValue );` is the code I am passing to make the button activate when necessary on clicking the checkbox – Gladwin James Aug 16 '16 at 17:56
  • Then use shared preference to save your data and read it when you start activity and then do action what you want(set check box and button) Take help of answers posted here. In case of any difficulty let us know. – Ramit Aug 16 '16 at 18:55

3 Answers3

6
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final CheckBox checkBox = (CheckBox)findViewById(R.id.checkBox);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = preferences.edit();
        if(preferences.contains("checked") && preferences.getBoolean("checked",false) == true) {
           checkBox.setChecked(true);
        }else {
            checkBox.setChecked(false);

        }
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(checkBox.isChecked()) {
                    editor.putBoolean("checked", true);
                    editor.apply();
                }else{
                    editor.putBoolean("checked", false);
                    editor.apply();
                }
            }
        });
    }
Gladwin James
  • 493
  • 2
  • 10
  • 21
  • Could you please edit your answer and add an explanation of how your code solves the problem? – Simon Hänisch Aug 16 '16 at 13:36
  • Sure, when you enter your app firstly and click checkbok, checkBox.setOnCheckedChangeListener(...) will work and your sharedpreference will keep checked key which the value is true. And when you enter your app next time you have already kept checked key in your preference. For this reason if(...) statement is true and your checkbox will checked – Gunay Abdullayeva Aug 16 '16 at 14:06
  • @GunayAbdullayeva Even though I unchecked the checkbox, next time when I enter the app its shows like the box is checked why is that so?the code remembers the check we have did but the It forgets the action which its meant to do. – Gladwin James Aug 16 '16 at 17:47
  • Can you? please it would really mean a lot to me and i have updated the balance code `in.putExtra("yourBo‌​olName", isCheckedValue );` is the code I am passing to make the button activate when necessary on clicking the checkbox – Gladwin James Aug 16 '16 at 18:03
  • if it is hard to understand my code, i can explain. when you call checkBox.setOnCheckedChangeListener(...) method if(...) statement will check that your checkBox is checked or not. if checkBox is checked your checked key will true else false and checked key will be kept in your sharedpreference. When you enter your app if(...) statement will be checked if it is true checkBox.setChecked(true) will call else other statement will call – Gunay Abdullayeva Aug 16 '16 at 18:17
0

when you click Ok button if checkbox clicked

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("myActivity", 0);
    editor.commit();

And when you launch your app check like this..

int result;
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    result = sharedPreferences.getInt("showActivity", -1);
    if(result == 0)
      // do not show the activity.. start the next activity
Riyaz Parasara
  • 1,126
  • 11
  • 21
0
final String FILE_NAME = "com.myproject.prefs";
final Boolean ADD = "add";
final Boolean ADD_FB = "add_fb";

// prefs file..
SharedPreferences prefs = getSharedPreferences(FILE_NAME, Activity.PRIVATE);
SharedPreferences.Editor editor = prefs.edit();

// by default both will be unchecked..
checkBoxAdd.setChecked(prefs.getBoolean(ADD, false);
checkBoxAddFb.setChecked(prefs.getBoolean(ADD_FB, false);

// now on checkedchancedEvent update the preferences
checkBoxAdd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

         editor.putBoolean(ADD, isChecked);
         editor.apply();


        }
    });

// similary the second one...
checkBoxAddFb.setOnCheckedChangeListener(new 
CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

         editor.putBoolean(ADD_FB, isChecked);
         editor.apply();


        }
    });

Updated

  • Here we store our preferences in a file of name "com.myproject.prefs". We need to store two preferences values of Boolean data type (one is add, another one is add_fb, as given in the Q).
  • Now get SharePreferences and its editor objects.
  • Set values of checkBoxes from stored preferences, if the preferences were not added before then set these to false. So, on the very first time when you run your app, these will get false values as there aren't any values added before.
  • Whenever you want to get these preferences values
    pass your preferences file_name and mode to getSharedPreferences();
    // then use those objects to get any specific value like
    prefs.getBoolean(your_value_name, default_value); Already mentioned above

  • Whenever you want to change these preferences values use SharedPreferences.Editor object. Pass your value name and it's new value to putBoolean().
    like
    editor.putBoolean(your_value_name, true/false); then apply the editing.

Hope, it helps

isamirkhaan1
  • 631
  • 6
  • 18