How to set default value for SwitchPreference in Android?

14,119

Solution 1

As I told, I write preferences programmatically:

PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
PreferenceCategory catView = new PreferenceCategory(this);
catView.setTitle(R.string.preference_category_view);
root.addPreference(catView);

final SwitchPreference switchSplash= new SwitchPreference(this);
switchSplash.setKey(PreferenceKeys.SPLASH); 

//-----the above code----
switchSplash.setChecked(false);       // LINE 1
catView.addPreference(switchSplash);  // LINE 2

While debugging I found that true value is set in LINE 1, but when I add switchSplash into catView, the values of switchSplash is reset to false, because catView sets values from preferences.xml.
That's why I changed all needed values into the XML

SharedPreferences.Editor editor = root.getPreferenceManager().getSharedPreferences().edit();
editor.putBoolean(PreferenceKeys.SPLASH, true);  
editor.commit();

Solution 2

You can use the XML attribute android:defaultValue="true" on your <SwitchPreference /> to set the default to true.

Solution 3

If you trying to get the boolean out of newValue

        boolean selected =   Boolean.parseBoolean(newValue.toString());      

your going about this in a strange and I guess incorrect way. If newValue is a Boolean, (check with instanceof) then just cast newValue to be a Boolean.

        boolean selected =   (Boolean) newValue;

Is that what your trying to achieve?

Share:
14,119
Lidia
Author by

Lidia

Updated on June 04, 2022

Comments

  • Lidia
    Lidia about 2 years

    Does anyone used SwitchPreference class from Android and knows how to set the default value? I have implemented it programmatically:

    SwitchPreference switch = new SwitchPreference(this);
    switch.setKey("preference_my_key");
    switch.setTitle(R.string.preference_title_my_title);
    switch.setSummary(R.string.preference_summary_my_summary);
    Boolean isChecked = Manager.myMethodIsChecked(MyActivity.this);
    switch.setChecked( isChecked ); 
    
    switch.setOnPreferenceChangeListener(new OnPreferenceChangeListener()  {                
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
        try {
                boolean selected =   Boolean.parseBoolean(newValue.toString());      
            if ( !selected ) {
                //do something
            }
        } catch (Throwable e) {
           e.printStackTrace();
        }               
       return true;
       }
    });         
    category.addPreference(switch);
    

    Preferences saves all values into its XML file: app_package_name_preferences.xml. First time when app is loaded, switch has default "false " values. But I need sometimes to make default value "true". I tried few methods,but nothing works.

    switch.setChecked( true );  
    switch.setDefaultValue(true);
    
  • acrespo
    acrespo over 10 years
    I think im having the same problem as you, but your solution is not very clear. What did you do then? I think my switchPreference is not reading my shared preferences, if i use CheckBoxpreference everything works well, but with SwitchPreference, everytime i open my settings activity it is set to false, which is not even the default value!
  • Allen Vork
    Allen Vork about 7 years
    @Lidia why don't you post ur solution
  • Allen Vork
    Allen Vork about 7 years
    What if I have multi SwitchPreference ? I think it will set all the values to true.
  • Willi Mentzel
    Willi Mentzel almost 5 years
    @Lidia Following your explanation I think you mean switchSplash.setChecked(true); and not switchSplash.setChecked(false); right?