The thing is I want to store my toogle button status in shared preference, so that I when I return to the application my toogle button wil stay in previous state. This is kind of something Do you want to use this. If user make the button enable that means when he return it will show the button will enable. So far I did is
tb_vibrate = (ToggleButton)this.findViewById(R.id.tb_vibrate);
tb_vibrate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(tb_vibrate.isChecked())
{
Toast.makeText(ProfileActivity.this, "Toggle button is on", Toast.LENGTH_LONG).show();
tb_vibrate.setChecked(true);
}
else {
Toast.makeText(ProfileActivity.this, "Toggle button is Off", Toast.LENGTH_LONG).show();
tb_vibrate.setChecked(false);
}
}
});
And this is my SharePreference class
public class NotificationManager {
private static final String PREFS_FILE_NAME = "AppNotificationManager";
private static final String VIBRATE = "vibrate";
private static final String NOTIFICATION_ALERT = "notification_alert";
private static final String NOTIFICATION_SOUND = "notification_sound";
private static final String CALL_RINGTONE = "call_ringtone";
private static final String RINGTONE_VIBRATION = "ringtone_vibrate";
public static void setVibrate(final Context ctx, final String vibrate) {
final SharedPreferences prefs = ctx.getSharedPreferences(NotificationManager.PREFS_FILE_NAME, Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = prefs.edit();
editor.putString(NotificationManager.VIBRATE, vibrate);
editor.commit();
}
public static String getVibrate(final Context ctx) {
return ctx.getSharedPreferences(NotificationManager.PREFS_FILE_NAME,
Context.MODE_PRIVATE).getString(NotificationManager.VIBRATE, "");
}
}
So how to create my shared preference class for storing the data and use it later?
you can use SharedPreferences to store state of your
ToggleButton
Sample code
getting values use the below code