package coderefactory.net.popmovies;

import android.content.SharedPreferences;
import android.os.Bundle;

import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceScreen;
import android.util.Log;


public class SettingsFragment extends PreferenceFragmentCompat {

    private static final String TAG = SettingsFragment.class.getSimpleName();

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        Log.d(TAG, "onCreatePreferences");
        addPreferencesFromResource(R.xml.preferences);

        setPreferenceSummaries();
    }

    private void setPreferenceSummaries() {
        final PreferenceScreen preferenceScreen = getPreferenceScreen();
        final SharedPreferences sharedPreferences = preferenceScreen.getSharedPreferences();

        final int count = preferenceScreen.getPreferenceCount();
        for (int index = 0; index < count; index += 1) {
            final Preference preference = preferenceScreen.getPreference(index);
            if (preference instanceof ListPreference) {
                final String value = sharedPreferences.getString(preference.getKey(), "");
                setListPreferenceSummary((ListPreference) preference, value);
            }
        }
    }

    private void setListPreferenceSummary(final ListPreference listPreference, final String value) {
        final int index = listPreference.findIndexOfValue(value);
        if (index != -1) {
            final CharSequence valueLabel = listPreference.getEntries()[index];
            listPreference.setSummary(valueLabel);
        }
    }
}

@@ -409,17 +409,82 @@
 ntCompat
- 
+%0A    implements SharedPreferences.OnSharedPreferenceChangeListener
 %7B%0A%0A    p
@@ -593,26 +593,382 @@
 d on
-CreatePreferences(
+Start() %7B%0A        super.onStart();%0A        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);%0A    %7D%0A%0A    @Override%0A    public void onStop() %7B%0A        super.onStop();%0A        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);%0A    %7D%0A%0A    @Override%0A    public void onCreatePreferences(final 
 Bund
@@ -989,16 +989,22 @@
 ceState,
+ final
  String 
@@ -1146,32 +1146,332 @@
 aries();%0A    %7D%0A%0A
+    @Override%0A    public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) %7B%0A        Log.d(TAG, %22onSharedPreferenceChanged%22);%0A        final Preference preference = findPreference(key);%0A        setPreferenceSummary(sharedPreferences, preference, key);%0A    %7D%0A%0A
     private void
@@ -1884,89 +1884,29 @@
     
-if (preference instanceof ListPreference) %7B%0A                final String value = 
+setPreferenceSummary(
 shar
@@ -1922,19 +1922,22 @@
 nces
-.getString(
+, preference, 
 pref
@@ -1955,12 +1955,8 @@
 ey()
-, %22%22
 );%0A 
@@ -1966,22 +1966,36 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-setLis
+private void se
 tPre
@@ -2013,45 +2013,62 @@
 ary(
-(ListPreference) preference, value);%0A
+final SharedPreferences sharedPreferences,%0A           
     
@@ -2079,18 +2079,16 @@
     
-%7D%0A
         
 %7D%0A  
@@ -2087,118 +2087,294 @@
     
-%7D%0A
     
-%7D%0A%0A    private void setListPreferenceSummary(final ListPreference listPreference, final String value) %7B%0A
+   final Preference preference, final String key) %7B%0A        if (preference != null && (preference instanceof ListPreference)) %7B%0A            final ListPreference listPreference = (ListPreference) preference;%0A            final String value = sharedPreferences.getString(key, %22%22);%0A    
     
@@ -2439,24 +2439,28 @@
 e);%0A        
+  
+  
 if (index !=
@@ -2462,24 +2462,28 @@
 ex != -1) %7B%0A
+    
             
@@ -2562,16 +2562,20 @@
         
+    
 listPref
@@ -2605,16 +2605,113 @@
 Label);%0A
+            %7D%0A        %7D else %7B%0A            Log.e(TAG, %22Settings summary on unknown preference%22);%0A
         
@@ -2718,10 +2718,12 @@
 %7D%0A    %7D%0A
+%0A%0A
 %7D%0A

Update setting summary as soon as preference changed