How to set default value for SwitchPreference in Android?
Asked Answered
O

3

9

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);
Overwind answered 25/9, 2012 at 13:10 Comment(1)
You might want to be a bit more specific as to what you want to achieve. And "switch" in the code above is not a boolean, and can therefore not be set to true or false.Geraldo
O
7

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();
Overwind answered 26/9, 2012 at 14:49 Comment(3)
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!Impersonate
What if I have multi SwitchPreference ? I think it will set all the values to true.Flavorous
@Overwind Following your explanation I think you mean switchSplash.setChecked(true); and not switchSplash.setChecked(false); right?Moraine
P
3

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

Popgun answered 11/5, 2020 at 11:3 Comment(0)
G
1

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?

Geraldo answered 25/9, 2012 at 16:35 Comment(1)
@Overwind why don't you post ur solutionFlavorous

© 2022 - 2024 — McMap. All rights reserved.