KMI KMI - 4 months ago 18
Android Question

how to keep the toggle button in preferences for the settings page in android?

I want to create settings page for my android application.In the settings page is it possible to keep the toggle button in preferences ? if yes ,kindly give me example .thanks in advance

Answer

Use the below two method to save and retreive the data in/from shared prefernce

public boolean getBooleanFromSP(Context context) {
// TODO Auto-generated method stub
SharedPreferences preferences = context.getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean("togleButtonIsChecked", false);
}//getPWDFromSP()

public void saveBooleanInSP(Context context, boolean value){
SharedPreferences preferences = context.getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("togleButtonIsChecked", value);
editor.commit();
}//savePWDInSP()