Question

I'm using this code to apply the editTextPreference title when the user put a value inside the box. The problem is that when I click on "ok" it doesn't change the title but only if I re-click on the editTextPreference it applies the before-written value.

public void handleTEST(){
  final EditTextPreference pref = (EditTextPreference)findPreference("test");       
    pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){   
        @Override
        public boolean onPreferenceClick(Preference preference) {
            pref.setTitle(pref.getText());

            return true;
        }
    });

}

So how can I apply the title on "OK" click?

Was it helpful?

Solution

As far as I can understand, you just want to display the value stored in preference in the title of that particular preference. So try this:

public void handleTEST(){
    final EditTextPreference pref = (EditTextPreference) findPreference("test");
    pref.setTitle(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("test", "Default Title"));
    // Loads the title for the first time
    // Listens for change in value, and then changes the title if required.
    pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            pref.setText(newValue.toString());
            return false;
        }
    });
}

Hope this helps :)

OTHER TIPS

Short answer matching the question:

public class SomePrefenceScreen extends PreferenceFragment implements
        OnSharedPreferenceChangeListener {

    private EditTextPreference edittext;

    private Context context;

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

        context = getActivity();
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.your_settings_xml);

        edittext = (EditTextPreference) getPreferenceScreen().findPreference("test");

    }

    @Override
    public void onStart() {
        updateAll();
        super.onStart();
    }

    @Override
    public void onResume() {
        PreferenceManager.getDefaultSharedPreferences(context)
                .registerOnSharedPreferenceChangeListener(this);

        super.onResume();
    }

    @Override
    public void onPause() {
        PreferenceManager.getDefaultSharedPreferences(context)
                .unregisterOnSharedPreferenceChangeListener(this);
        super.onPause();
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        updateAll();
    }

    private void updateAll() {
        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(context);

        // test_key being the android:key defined in XML in your_settings_xml
        String titleString = sharedPrefs.getString("test_key", "");
        if (!titleString.isEmpty()) {
            edittext.setTitle(titleString);
        }

    }

}

Long answer and more generic approach with helper classes:

public class GoogleMapsSettings extends DaggerPreferenceFragment implements
        OnSharedPreferenceChangeListener {

    private static final String TAG = "GoogleMapsSetings";

    private StatusCheck statusCheck;

    private ListPreference address_entry;
    private ListPreference city_entry;

    @Inject @ForActivity Context context;
    @Inject Preferences preferences;
    @Inject GoogleMapsModule googleMapsModule;

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

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

        address_entry = (ListPreference) getPreferenceScreen().findPreference(
                getString(R.string.key_googlemaps_address_entry));

        city_entry = (ListPreference) getPreferenceScreen().findPreference(
                getString(R.string.key_googlemaps_city_entry));

    }

    @Override public void onStart() {
        updateAllSummaries();

        statusCheck = new StatusCheck(context, googleMapsModule, this);
        statusCheck.runCheck();
        super.onStart();
    }

    @Override public void onResume() {
        PreferenceManager.getDefaultSharedPreferences(context)
                .registerOnSharedPreferenceChangeListener(this);

        super.onResume();
    }

    @Override public void onPause() {
        PreferenceManager.getDefaultSharedPreferences(context)
                .unregisterOnSharedPreferenceChangeListener(this);
        super.onPause();
    }

    @Override public void onSharedPreferenceChanged(SharedPreferences prefs,
            String key) {

        if (key.equals(getString(R.string.key_module_activated_googlemaps))) {
            statusCheck.runCheck();
        }

        updateAllSummaries();
    }

    private void updateAllSummaries() {
        SummaryUpdater.updateEntry(this, preferences,
                R.string.key_googlemaps_address_entry,
                R.string.prefs_setting_summary_address);
        SummaryUpdater.updateEntry(this, preferences,
                R.string.key_googlemaps_city_entry,
                R.string.prefs_setting_summary_city);
    }

The SummaryUpdater is just a helper class to update the relevant text, which helps me reduce code redundancy:

public class SummaryUpdater {

    public static void updateString(PreferenceFragment pref,
            Preferences preferences, int resource) {
        updateString(pref, preferences, resource, "");
    }

    public static void updateString(PreferenceFragment pref,
            Preferences preferences, int resource, String postfix) {
        Preference preference = (Preference) pref.findPreference(pref
                .getString(resource));
        if (preference != null) {
            String summaryString = preferences.getString(resource);
            summaryString = (postfix.isEmpty()) ? summaryString : summaryString
                    + " " + postfix;

            if (!summaryString.isEmpty()) {
                preference.setSummary(summaryString);
            }
        }
    }

    public static void updateEntry(PreferenceFragment pref,
            Preferences preferences, int resourcekey, int resourcedefault) {
        Preference preference = pref
                .findPreference(pref.getString(resourcekey));
        if (preference != null) {
            int currentEntry = Integer.parseInt(preferences
                    .getString(resourcekey));

            if (currentEntry != 0) {
                preference.setSummary(pref.getString(R.string.entry) + ": "
                        + currentEntry);
            } else {
                preference.setSummary(pref.getString(resourcedefault));
            }
        }
    }

    public static void updateInt(PreferenceFragment pref,
            Preferences preferences, int resource) {
        updateInt(pref, preferences, resource, "");
    }

    public static void updateInt(PreferenceFragment pref,
            Preferences preferences, int resource, String postfix) {
        Preference preference = (Preference) pref.findPreference(pref
                .getString(resource));
        if (preference != null) {
            String summaryString = "" + preferences.getStringInt(resource);
            summaryString = (postfix.isEmpty()) ? summaryString : summaryString
                    + " " + postfix;
            preference.setSummary(summaryString);
        }
    }
}

The Preferences object simply access read/write of sharedPreferences, here is for instance the get/set of String values:

public void putString(int resource, String value) {
    String key = context.getString(resource);
    boolean success = sharedPrefs.edit().putString(key, value).commit();
}

public String getString(int resource) {
    String key = context.getString(resource);
    if (sharedPrefs != null && context.getString(resource) != null) {
        return sharedPrefs.getString(key, "");
    }
    return "";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top