Hello i have implemented the application based on the toggleButton selection. but while i close that application and then reopen it, it will get in to its default selection that is "off". So can any budy tell mw what should i have to save the state of the toogleButton selection and perform some action based on that toggleButton selection state. . . Thanks.
Asked
Active
Viewed 1.3k times
3

Shreyash Mahajan
- 23,386
- 35
- 116
- 188
-
1Use SharedPreferences and store toggle button state on exit. Read more at http://alchemiaandroid.altervista.org/sharedPreferencesTutorial.html – evilone Sep 12 '11 at 05:50
3 Answers
13
Use SharedPreferences.
tg = (ToggleButton) findViewById(R.id.toggleButton1);
tg.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if((tg.isChecked()))
{
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("tgpref", true); // value to store
editor.commit();
}
else
{
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("tgpref", false); // value to store
editor.commit();
}
}
});
And this is how to retrieve the values:
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);
}
I did not verify this code, but look at some examples on the net, it is easy!

erdomester
- 11,789
- 32
- 132
- 234
-
Is it possible to save even if i close the activity or application and again start it ? – Shreyash Mahajan Sep 12 '11 at 06:07
-
which mode i have to add during inserting the preference value ? – Shreyash Mahajan Sep 12 '11 at 06:11
-
@iDroid Explorer Yes, this is the method to save your preferences and you can save them and read them after application is closed/restarted or whatever :) – evilone Sep 12 '11 at 06:30
-
1I have done as u have explained. But if i close the activity and then again run it, the toogleButton still remains in its default condition "off" even if i have selected it as "on". Why it is happend like this? – Shreyash Mahajan Sep 12 '11 at 07:17
-
It should be on, as it is set to true as default...please show us your code – erdomester Sep 12 '11 at 07:21
3
The best way, you can set tgbutton same
screen main
Intent intent = new Intent(this, Something.class);
intent.putExtra(BOOLEAN_VALUE, Boolean.valueOf((tvConfigBoolean.getText().toString())));
startActivity(intent);
screen something
Bundle bundle = getIntent().getExtras();
tbtConfigBoolean.setChecked((bundle
.getBoolean(MainActivity.BOOLEAN_VALUE)));
and save state
editor.putBoolean("BooleanKey", tbtConfigBoolean.isChecked());
editor.commit();
good luck

Hoa Nguyen
- 168
- 1
- 7
3
Use SharedPreferences like erdomester suggested, but I modified little bit his code. There's some unneeded conditions.
tg = (ToggleButton) findViewById(R.id.toggleButton1);
tg.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("tgpref", tg.isChecked()); // value to store
editor.commit();
}
});
And this is how to retrieve the values:
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean tgpref = preferences.getBoolean("tgpref", true); //default is true
tg.setChecked(tgpref);

evilone
- 22,410
- 7
- 80
- 107
-
-
-
You can put false there too. This is your choice what to set for default value. – evilone Sep 17 '11 at 11:53