Wednesday, August 24, 2011

[android-developers] Re: Trouble programmatically setting default value of a Preference

You should do something like:

public void onCreate() {
addPreferencesFromResource(R.xml.prefs);
CheckBoxPreference pref = (CheckBoxPreference)
findPreference("blargh");
if(pref != null) {
pref.setChecked(true);
}
}


On Aug 23, 11:24 pm, b0b <pujos.mich...@gmail.com> wrote:
> I have a simple CheckBoxPreference "blargh" defined in an XML file. I
> want to programatically set its default value with code like this
> which does not work
>
> in subclass of PreferenceActivity:
>
> public void onCreate() {
>   addPreferencesFromResource(R.xml.prefs);
>    findPreference("blargh").setDefaultValue(true);
>
> }
>
> I would expect with code above that the first time the activity is
> displayed, the checkbox preference is checked due to true set as the
> default value. This isn't the case, it is unchecked.
> (In code above I force default value to true but in reality this is a
> computed value).
> I made sure to uninstall the app before reinstalling it to ensure
> first run conditions.
>
> What I'm doing wrong ? What's the correct way to dynamically set a
> default value for a preference loaded from XML ?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment