Forum Xamarin Xamarin.Android


RodRod GBMember
edited October 2012 in Xamarin.Android

I am completely new to Android developent - so apologies if this is a basic question, but search doesn't yield any result(s) I think I am looking for...

I am trying to get a saved CheckBoxPreference and a ListPreference from the SharedPreferences and change them.

I can read the preferences okay - I believe - as they are being displayed in the emulator,

I think I just somehow need to wire up an event handler and then take the values from the args when a change occurs - but I am having trouble translating some Java code I am using as a basis.

The Java sample I am looking at is like so (within the onCreate() method)...

fontSizePref = (ListPreference) getPreferenceScreen().findPreference("font_pref");
fontSizePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    fontSizePref.setValue((String) newValue);
                    SharedPreferences.Editor ed = mPrefs.edit();
                    ed.putInt("font_size", Integer.valueOf((String) newValue));
                    return false;

I have this (but I'm having difficulty knowing what I need to do to wire up the event)

    protected override void OnCreate(Bundle savedInstanceState)
        sharedPreferences = PreferenceManager.GetDefaultSharedPreferences(this);
        fontSize = (ListPreference)FindPreference("fontSize");

        // Event wiring or something goes here??????


Can someone please give me a pointer how I can do this for a CheckBoxPreference and a ListPreference?



  • Joseph_AndersonJoseph_Anderson USMember
    edited October 2012


    Does this help:

    resources > xml > settings.xml

    [Activity(Label = "Instructions")]
    public class MySettingsHelper
        private ISharedPreferences _settings;
        public MySettingsHelper(ISharedPreferences settings)
            _settings = settings;
         * gets whether phone should win sync and does phone to database sync
         * @return
        public bool PhoneWinsSync()
            return _settings.GetBoolean("phoneWinsSync", false);
         * determines whether phone uses bar code scanning software or dummy
         * @return
        public bool DoRealBarcode()
            return _settings.GetBoolean("doRealBarcode", false);
  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    The literal translation of your Java sample is:

    var fontSizePref = PreferenceScreen.FindPreference ("font_pref").JavaCast<ListPreference>();
    fontSizePref.PreferenceChange += (sender, e) => {
        fontSizePref.Value = e.NewValue.ToString ();
        var ed = mPrefs.Edit ();
        ed.PutInt ("font_size", Convert.ToInt32 (e.NewValue.ToString ()));
        ed.Commit ();
        e.Handled = false;

    Note: You need to inherit from PreferenceActivity in order to use the PreferenceActivity.PreferenceScreen property, not Activity,

  • RodRod GBMember
    edited October 2012

    @Jonathan - the translation you have done for me and changing the class to inherit from PreferenceActivity done the trick.

    Many thanks.

Sign In or Register to comment.