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);
Related
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
How to maintain the state of toggle buttons, like if we press button1 then it remembers the on state of button1 and won't allow us to press button2, and vice versa.
Please help me out in solving this problem.
Try this.
Declare variables
ToggleButton mToggleOne;
ToggleButton mToggleTwo;
In onCreate
mToggleOne = (ToggleButton) findViewById(R.id.toggle_button1);
mToggleTwo = (ToggleButton) findViewById(R.id.toggle_button2);
SharedPreferences spref = getSharedPreferences();
if(spref.getBoolean("one", false)) {
mToggleOne.setChecked(true);
mToggleTwo.setEnabled(false);
} else if(spref.getBoolean("two", false)) {
mToggleTwo.setChecked(true);
mToggleOne.setEnabled(false);
}
mToggleOne.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mToggleTwo.setEnabled(!isChecked);
getSharedPreferences().edit()
.putBoolean("one", isChecked)
.apply();
//other code
}
});
mToggleTwo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mToggleOne.setEnabled(!isChecked);
getSharedPreferences().edit()
.putBoolean("two", isChecked)
.apply();
//other code
}
});
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();
}
I have checkboxes for week days in my android application , and I want to put a listener to check if any one of these checkboxes is checked but my way seems hard , isn't there a way to gather all these listeners into one
sun.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
mon.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
tue.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
wed.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
thu.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
fri.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
sat.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
You can check the id of the buttonView that is being triggered and do the proper validation, all you have to do is assign the same onCheckListener to the checkboxes and do something as show below:
private OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.checkId1:
//TODO: Code for checked one...
break;
case R.id.checkId2:
//TODO: Code for checked two...
break;
}
}
};
Hope it Helps!
Regards!
If you have similar logic that you haven't provided here, you can create a list of checkboxes and create listeners for them in a cycle, for example.
Let your activity implement the OnCheckedChangeListener and then you have the onCheckChanged...
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.sun:
break;
case R.id.mon:
break;
default:
break;
}
}
there is; implements View.OnClickListener with his method in your class
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.checbox1:
if(checkbo1.isCheck){
//do logic then dont forget to set to the opposite state
checkbox.setChecked(false);
}
else{
//do logic
checkbox.setChecked(true);
}
break;
//
case R.id.checbox2:
//do logic etc...
break;
}
}
then use a switch case deal between different click event from user
hope it help
Of course there is a simpler way. Just make your Activity where you are doing this implement OnCheckedChangeListener
public Class MyActivity extends Activity implements OnCheckedChangeListener{
//your activity logic in here
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//your Activity will now receive onCheckedChangeEvents here from
//the checkboxes to which you assign it as a listener
}
}
, in onCreate, after you get references to all of your day checkboxes, set the Activity as the listener like this
#Override
public void onCreate(Bundle savedInstanceState){
//first get references to all your checkboxes and assign them to mon, tue, wed etc.
//then set the listeners
mon.setOnCheckedChangeListener(this);
tue.setOnCheckedChangeListener(this);
wed.setOnCheckedChangeListener(this);
thu.setOnCheckedChangeListener(this);
fri.setOnCheckedChangeListener(this);
sat.setOnCheckedChangeListener(this);
sun.setOnCheckedChangeListener(this);
}
, make sure all of your checkboxes have IDs assigned to them in the layout xml, like this
<CheckBox
android:id="#+id/checkMonday"
....
, then you will have one onCheckedChange method where you can handle the different days like this
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
if(isChecked){
counter++; //a day has been checked
}else{
counter--; //a day has been unchecked
}
switch(id){
case R.id.checkMonday:
//logic for Monday
break;
case R.id.checkTuesday:
//logic for Tuesday
break;
...
}
}
That should do the trick!
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.