Search code examples
androidandroid-preferencespreferenceactivity

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


Solution

  • 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()