1 package uk
.me
.njae
.sunshine
;
3 import android
.os
.Bundle
;
4 import android
.preference
.ListPreference
;
5 import android
.preference
.Preference
;
6 import android
.preference
.PreferenceActivity
;
7 import android
.preference
.PreferenceManager
;
10 * A {@link PreferenceActivity} that presents a set of application settings.
12 * See <a href="http://developer.android.com/design/patterns/settings.html">
13 * Android Design: Settings</a> for design guidelines and the <a
14 * href="http://developer.android.com/guide/topics/ui/settings.html">Settings
15 * API Guide</a> for more information on developing a Settings UI.
17 public class SettingsActivity
extends PreferenceActivity
18 implements Preference
.OnPreferenceChangeListener
{
21 public void onCreate(Bundle savedInstanceState
) {
22 super.onCreate(savedInstanceState
);
23 // Add 'general' preferences, defined in the XML file
24 addPreferencesFromResource(R
.xml
.pref_general
);
26 // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
27 // updated when the preference changes.
28 bindPreferenceSummaryToValue(findPreference(getString(R
.string
.pref_location_key
)));
29 bindPreferenceSummaryToValue(findPreference(getString(R
.string
.pref_units_key
)));
34 * Attaches a listener so the summary is always updated with the preference value.
35 * Also fires the listener once, to initialize the summary (so it shows up before the value
38 private void bindPreferenceSummaryToValue(Preference preference
) {
39 // Set the listener to watch for value changes.
40 preference
.setOnPreferenceChangeListener(this);
42 // Trigger the listener immediately with the preference's
44 onPreferenceChange(preference
,
46 .getDefaultSharedPreferences(preference
.getContext())
47 .getString(preference
.getKey(), ""));
51 public boolean onPreferenceChange(Preference preference
, Object value
) {
52 String stringValue
= value
.toString().trim();
54 if (preference
instanceof ListPreference
) {
55 // For list preferences, look up the correct display value in
56 // the preference's 'entries' list (since they have separate labels/values).
57 ListPreference listPreference
= (ListPreference
) preference
;
58 int prefIndex
= listPreference
.findIndexOfValue(stringValue
);
60 preference
.setSummary(listPreference
.getEntries()[prefIndex
]);
63 // For other preferences, set the summary to the value's simple string representation.
64 preference
.setSummary(stringValue
);