package com.example.android.sunshine.app;

import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;

public class SettingsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);

        PreferenceScreen preferenceScreen = getPreferenceScreen();
        int preferenceCount = preferenceScreen.getPreferenceCount();

        for (int i = 0; i < preferenceCount; i++) {
            Preference preference = preferenceScreen.getPreference(i);
            bindPreferenceToSummary(preference);
        }
    }

    private void bindPreferenceToSummary(Preference preference) {

        if (preference instanceof PreferenceCategory) {
            PreferenceCategory category = (PreferenceCategory) preference;
            for (int i = 0; i < category.getPreferenceCount(); i++) {
                bindPreferenceToSummary(category.getPreference(i));
            }
        } else {
            bindSummary(preference);
        }
    }

    private void bindSummary(Preference preference) {
        if (preference instanceof EditTextPreference) {
            String preferenceValue = ((EditTextPreference) preference).getText();
            preference.setSummary(preferenceValue);
        }
        if (preference instanceof ListPreference) {
            String preferenceValue = (String) ((ListPreference) preference).getEntry();
            preference.setSummary(preferenceValue);
        }
    }

}

@@ -36,16 +36,58 @@
 e.app;%0A%0A
+import android.content.SharedPreferences;%0A
 import a
@@ -423,16 +423,86 @@
 Fragment
+%0A        implements SharedPreferences.OnSharedPreferenceChangeListener
  %7B%0A%0A    
@@ -612,62 +612,8 @@
 te);
-%0A%0A        // Load the preferences from an XML resource
 %0A   
@@ -664,16 +664,682 @@
 ences);%0A
+        bindPreferencesToSummaries();%0A    %7D%0A%0A    @Override%0A    public void onResume() %7B%0A        super.onResume();%0A        getPreferenceScreen()%0A                .getSharedPreferences()%0A                .registerOnSharedPreferenceChangeListener(this);%0A    %7D%0A%0A    @Override%0A    public void onPause() %7B%0A        super.onPause();%0A        getPreferenceScreen()%0A                .getSharedPreferences()%0A                .unregisterOnSharedPreferenceChangeListener(this);%0A    %7D%0A%0A    @Override%0A    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) %7B%0A        bindPreferencesToSummaries();%0A    %7D%0A%0A    private void bindPreferencesToSummaries() %7B
 %0A       
@@ -1467,17 +1467,16 @@
 ount();%0A
-%0A
         
@@ -1722,17 +1722,16 @@
 ence) %7B%0A
-%0A
         
@@ -2538,11 +2538,10 @@
 %7D%0A    %7D%0A
-%0A
 %7D%0A

Update preference summary on value change