Store value in SharedPreference and get From it in android - android

i am new to android and i have been using this code to save the value of my Checkboxes
when the app closes which works fine (in Settings.class)
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;
}
but i want to use the same saved value in another activity (Progress.class)
here are my activities
Settings.class
checkBox_one = (CheckBox) findViewById(R.id.checkBox1);
boolean isChecked = getBooleanFromPreferences("isChecked");
Log.i("start",""+isChecked);
checkBox_one.setChecked(isChecked);
//checkBox_one.setChecked(true);//Enable By Default
checkBox_one.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Log.i("boolean",""+isChecked);
Settings.this.putBooleanInPreferences(isChecked,"isChecked");
}
});
checkBox_two = (CheckBox) findViewById(R.id.checkBox2);
boolean isCheckedTwo = getBooleanFromPreferences("isCheckedTwo");
checkBox_two.setChecked(isCheckedTwo );
//checkBox_two.setChecked(true);//Enable By Default
checkBox_two.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedTwo");
}
});
checkBox_three = (CheckBox) findViewById(R.id.checkBox3);
boolean isCheckedThree = getBooleanFromPreferences("isCheckedThree");
checkBox_three.setChecked(isCheckedThree );
checkBox_three.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedThree");
}
});
checkBox_four = (CheckBox) findViewById(R.id.checkBox4);
boolean isCheckedFour = getBooleanFromPreferences("isCheckedFour");
checkBox_four.setChecked(isCheckedFour );
//checkBox_four.setChecked(true);//Enable By Default
checkBox_four.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedFour");
}
});
checkBox_five = (CheckBox) findViewById(R.id.checkBox5);
boolean isCheckedFive = getBooleanFromPreferences("isCheckedFive");
checkBox_five.setChecked(isCheckedFive );
checkBox_five.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedFive");
}
});
}
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;
}
//-------------------------//
and here is Progress.Class
progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateProgressBars();
}
});
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateProgressBars();
}
});
}
public void updateProgressBars() {
progressBar1.setVisibility(View.GONE);
progressBar2.setVisibility(View.GONE);
if (checkBox1.isChecked() && checkBox2.isChecked()) {
progressBar2.setVisibility(View.VISIBLE);
} else if (checkBox1.isChecked()) {
progressBar1.setVisibility(View.VISIBLE);
}

I have recently implemented same requirement in my application. I followed below process.
Since you already created the sharedpreferences, Now in target activity use as below.
SharedPreferences setprefsd=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
now get boolean as show below:
setprefsd.getBoolean(Key,Value) //key is the one which you have used while declaring
Let me know how it goes
Edit--------------------------------------------------------------------------------
Change key like this as key should be a String value and make isChecked static to access from another activity
To put the value in shared preferences
editor.getBoolean("key", isChecked);
To retrive the value
setprefsd.getBoolean("Key", isChecked)
Edit2------------------------------------------------------
change your share preferences insertion as below.
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("key", isChecked);
editor.commit();
}

Related

SharedPreference to remember the checkbox is visible and checked

I have a checkbox that is invisible, however when I click on the menu item the checkbox is visible. What should the SharedPreferences look like if the checkbox is checked, and if I leave the app and return to the app to keep the checkbox visible. Now, when I leave the app and the checkbox is checked, and when I return to the app the checkbox is invisible and did not remember that the checkbox was checked.
<CheckBox
android:id="#+id/my_check"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Save Audio file"
android:textColor="#000000"
android:layout_marginTop="20dp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
private CheckBox my_check;
// onCreate
addListenerOnSaveAudio();
switch (item.getItemId()) {
case R.id.save_audio:
saveAudio.setVisibility(View.VISIBLE);
}
public void addListenerOnSaveAudio() {
my_check = (CheckBox) findViewById(R.id.my_check);
my_check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
// My code
} else {
saveAudio.setVisibility(View.INVISIBLE);
}
}
});
}
Here is the doc for sharedPreference and how to use it.
You need to save the state of your checkbox when someone clicks your menu to do that you can use:
// in onCreate
Context context = getActivity();
SharedPreferences preferences = context.getSharedPreferences(
"NAME OF YOUR CHOICE", Context.MODE_PRIVATE);
CheckBox checkBox = findViewById(R.id.my_check);
SharedPreferences.Editor editor = preferences.edit();
if(preferences.contains("my_check") && preferences.getBoolean("my_check",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("my_check", true);
editor.apply();
}else{
editor.putBoolean("my_check", false);
editor.apply();
}
}
});
private boolean state;
private SharedPreferences sharedPreferences;
private Checkbox my_check;
public void addListenerOnSaveAudio() {
//Instance for save state of my_check
sharedPreferences = getSharedPreferences("name_preferences",Context.MODE_PRIVATE);
my_check = (CheckBox) findViewById(R.id.my_check);
// If my check not exist by default is false else true
state = sharedPreferences.getBoolean("my_check", false);
my_check.setChecked(state);
my_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//When do click in my_check will change state in preferences and when open your app is apply changes in view.
sharedPreferences.edit()
.putBoolean("my_check",isChecked);
if (isChecked) {
// My code
} else {
saveAudio.setVisibility(View.INVISIBLE);
}
}
});
}
You can use onCheckedChanged() to register a listener to your checkBox, and then save its check status into shared preference.
And get the shared preference value each time you launch your app, i.e. in onCreate() method
So modify your activity's onCreate() and your addListenerOnSaveAudio() as below
public class MyActivity extends AppCompatActivity implements View.OnClickListener {
private CheckBox my_check;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
my_check = findViewById(R.id.my_check);
SharedPreferences sharedPrefs = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
boolean isChecked = sharedPrefs.getBoolean("checkValue", false); // default value is false
my_check.setChecked(isChecked);
addListenerOnSaveAudio();
}
public void addListenerOnSaveAudio() {
my_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("checkValue", isChecked);
editor.apply();
if (!isChecked)
saveAudio.setVisibility(View.INVISIBLE);
}
});
}
}

How to apply shared preference to checkbox

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
#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();
}
}
});
}
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

Sharedpreferences with switch button

I have a project, i want to save state of switch button on sharedpreferences. but it not working. Switch button always off when i switch activity or close app and reopen. what wrong in my code ? It working on first test, when i rebuid app again it wrong,
public class huongdan extends Activity implements CompoundButton.OnCheckedChangeListener{
Switch vt,mbf,vnp,vnm,gmb;
TextView vtmb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_huongdan);
// hien thi man hinh float
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
getWindow().setLayout((int) (width*1),(int) (height*0.80));
// endhien thi man hinh float
//switch viettel
vt = (Switch) findViewById(R.id.switchvt);
vt.setChecked(getFromSP("sw1"));
vt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true){
OneSignal.sendTag("viettel", "viettel");
}
else
OneSignal.deleteTag("viettel");
}
});
//switch mobi
mbf = (Switch) findViewById(R.id.switchmbf);
mbf.setChecked(getFromSP("sw2"));
mbf.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true){
OneSignal.sendTag("mobifone", "mobifone");
}
else
OneSignal.deleteTag("mobifone");
}
});
//switch vina
vnp = (Switch) findViewById(R.id.switchvnp);
vnp.setChecked(getFromSP("sw3"));
vnp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true){
OneSignal.sendTag("vinaphone", "vinaphone");
}
else
OneSignal.deleteTag("vinaphone");
}
});
//switch vietnammobile
vnm = (Switch) findViewById(R.id.switchvnm);
vnm.setChecked(getFromSP("sw4"));
vnm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true){
OneSignal.sendTag("vietnammobile", "vietnammobile");
}
else
OneSignal.deleteTag("vietnammobile");
}
});
//switch gmobile
gmb = (Switch) findViewById(R.id.switchgmb);
gmb.setChecked(getFromSP("sw5"));
gmb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true){
OneSignal.sendTag("gmobile", "gmobile");
}
else
OneSignal.deleteTag("gmobile");
}
});
}
private boolean getFromSP(String key){
SharedPreferences preferences = getApplicationContext().getSharedPreferences("com.sopu89.napthenoti", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
private void saveInSp(String key,boolean value){
SharedPreferences preferences = getApplicationContext().getSharedPreferences("com.sopu89.napthenoti", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.switchvt:
saveInSp("sw1",isChecked);
break;
case R.id.switchmbf:
saveInSp("sw2",isChecked);
break;
case R.id.switchvnp:
saveInSp("sw3",isChecked);
break;
case R.id.switchvnm:
saveInSp("sw4",isChecked);
break;
case R.id.switchgmb:
saveInSp("sw5",isChecked);
break;
}
}
}
You need to retrieve the values from shared preferences and then set the button state in onCreate method
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean tgpref = preferences.getBoolean("tgpref", true); //default is true
if (tgpref = true) //if (tgpref) may be enough, not sure
{
tg.setChecked(true);
}
else
{
tg.setChecked(false);
}
the onCheckedChanged-method in the end will never be called and therefore your states are not going to be saved.
The reason for this is, that you are declaring a new listener for every switch.
call
saveinSp("sw1", isChecked);
in the listener of switch one or set your class as listener for the switches and handle all changes there:
switch.setOnCheckedChangedListener(this);

Saving State of Multiple Checkboxes - Android

I am able to save the state of a Single CheckBox but how do i save All the Five CheckBoxes in my Activity?
checkBox = (CheckBox) findViewById(R.id.checkBox1);
boolean isChecked = getBooleanFromPreferences("isChecked");
Log.i("start",""+isChecked);
checkBox.setChecked(isChecked);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Log.i("boolean",""+isChecked);
Settings.this.putBooleanInPreferences(isChecked,"isChecked");
}
});
}
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;
For second checkbox do the following
boolean isCheckedTwo = getBooleanFromPreferences("isCheckedTwo");
checkBox2.setChecked(isCheckedTwo );
checkBox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedTwo");
}
});
and similarly for the rest
Use a Arraylist to store the state of all the checkboxes
Put each one with different name. and remember the key comes first not the value. Call your save method each time with different name

Using CheckBox value in other Activites, Android

I have been trying to use/save the boolean value of a checkbox in other activites but haven't had much luck.
I know you have to use SharedPreferences however I can't set it up right. I have made a preferences class which has
private static final String OPTION_PREF = "my.main.project";
private SharedPreferences optionPreferences;
private Editor optionEditor;
private boolean checkbox;
public Preferences(Context context)
{
this.optionPreferences = context.getSharedPreferences(OPTION_PREF, Activity.MODE_PRIVATE);
this.optionEditor = optionPreferences.edit();
}
public boolean getChecked()
{
return optionPreferences.getBoolean("is_checked", checkbox);
}
public void saveChecked(boolean checkBox)
{
optionEditor.putBoolean("save_check_pref", checkBox);
optionEditor.commit();
}
And then in an options menu for example,
boolean veggieChecked;
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
Preferences pref;
pref = new Preferences(getApplicationContext());
veggieChecked = pref.getChecked();
final CheckBox checkBox = (CheckBox) findViewById(R.id.vegetarian);
if(checkBox.isChecked())
veggieChecked = true;
pref.saveChecked(veggieChecked);
I cannot really see what I am doing wrong as I am new to Android and have not used sharedpreferences before.. any help would be appreciated!
You can use something like this:
CheckBox Equities = (CheckBox) findViewById(R.id.Equities);
CheckBox FixedIncome = (CheckBox) findViewById(R.id.FixedIncome);
CheckBox Currencies = (CheckBox) findViewById(R.id.Currencies);
CheckBox Commodities = (CheckBox) findViewById(R.id.Commodities);
CheckBox Derivatives = (CheckBox) findViewById(R.id.Derivatives);
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
prefsEditor = myPrefs.edit();
int prefEq = myPrefs.getInt("Equities", 0);
int prefFI = myPrefs.getInt("FixedIncome", 0);
int prefCu = myPrefs.getInt("Currencies", 0);
int prefCo = myPrefs.getInt("Commodities", 0);
int prefDe = myPrefs.getInt("Derivatives", 0);
if(prefEq == 1)
{
Equities.setChecked(true);
}
else
{
Equities.setChecked(false);
}
if(prefFI == 1)
{
FixedIncome.setChecked(true);
}
else
{
FixedIncome.setChecked(false);
}
if(prefCu == 1)
{
Currencies.setChecked(true);
}
else
{
Currencies.setChecked(false);
}
if(prefCo == 1)
{
Commodities.setChecked(true);
}
else
{
Commodities.setChecked(false);
}
if(prefDe == 1)
{
Derivatives.setChecked(true);
}
else
{
Derivatives.setChecked(false);
}
Equities.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
prefsEditor.putInt("Equities", 1);
prefsEditor.commit();
}
else
{
myArray[0] = false;
prefsEditor.putInt("Equities", 0);
prefsEditor.commit();
}
}
});
FixedIncome.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
prefsEditor.putInt("FixedIncome", 1);
prefsEditor.commit();
}
else
{
prefsEditor.putInt("FixedIncome", 0);
prefsEditor.commit();
}
}
});
Currencies.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
prefsEditor.putInt("Currencies", 1);
prefsEditor.commit();
}
else
{
prefsEditor.putInt("Currencies", 0);
prefsEditor.commit();
}
}
});
Commodities.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
prefsEditor.putInt("Commodities", 1);
prefsEditor.commit();
}
else
{
prefsEditor.putInt("Commodities", 0);
prefsEditor.commit();
}
}
});
Derivatives.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
prefsEditor.putInt("Derivatives", 1);
prefsEditor.commit();
}
else
{
prefsEditor.putInt("Derivatives", 0);
prefsEditor.commit();
}
}
});
At first glance you're putting in the key is_checked and later trying to pull save_check_pref.

Categories

Resources