ShareThis

Monday, April 2, 2012

Android: How to Retrieve Shared Preferences Values

To a beginner this may be daunting. But it is a really easy solution!

Example preference activity:

public class FooActivity extends PreferenceActivity {

 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  addPreferencesFromResource(R.xml.preference);
 }
}

Preference xml file:


Method to access those items: (use in any class necessary)


import android.preference.PreferenceManager;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// then you use
prefs.getBoolean("keystring", true);

Set your android:key= "" to the key value you want to access later on. That simple! No need for ids on checkbox preferences either (unless you're doing dynamic changes to the settings and need to)


0 comments:

Post a Comment