I am trying to get a checkbox to default to True when the app is launched.
I used this answer Android CheckBoxPreference Default Value but it still defaults to false and prints False in LogCat.
Any ideas where I have messed up? I’ve been looking at this for hours… thanks in advance!
public class Preferences extends PreferenceActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.preferences);
PreferenceManager.setDefaultValues(this, R.layout.preferences, true);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean autoStart = prefs.getBoolean("checkBox1", true);
System.out.println(autoStart);
}
}
XML class:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="Username and password information"
android:title="User request" >
<CheckBoxPreference
android:key="checkBox1"
android:title="request details"
android:defaultValue="true"/>
</PreferenceCategory>
</PreferenceScreen>
In Your main activity you could do following:
main Activity means the activity that starts first with starting app.