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);