I have 7 edit text boxes established as preferences they are workiing correctly and when selected they display their vaue.
But I would rather that they display their vaue from the preferences screen if a value has been entered instead of requiring the need to click the preference to open its dialog.

The code I found and works but in my application it does not due to depriciation. I would rather not change the min max sdk versions to get this working corectly.

Maybe there is an easier way I am new to Java coding soo I'm fighting through the tutorials and such as I find them.

Depriciated items are
addPreferencesFromResource
getPreferenceScreen
findPreference

package named.resourcetest;

import android.content.Intent;
    import android.os.Bundle;
    import android.preference.PreferenceActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
    import android.preference.EditTextPreference;
    import android.preference.ListPreference;
    import android.preference.Preference;
    import android.preference.PreferenceCategory;
    import android.preference.PreferenceManager;

    public class PrefHelper extends PreferenceActivity  implements
        OnSharedPreferenceChangeListener {

        @Override
        public void onCreate(Bundle savedInstanceState) {       
            super.onCreate(savedInstanceState);        
            addPreferencesFromResource(R.xml.preferences);
            PreferenceManager.setDefaultValues(this, R.xml.preferences,
        false);
    for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
        initSummary(getPreferenceScreen().getPreference(i));
        }
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(Menu.NONE, 0, 0, "Show current settings");
            return super.onCreateOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case 0:
                    startActivity(new Intent(this, EmerNum.class));
                    return true;
            }
            return false;
        }


    @Override
    protected void onResume() {
    super.onResume();
    // Set up a listener whenever a key changes
    getPreferenceScreen().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
    super.onPause();
    // Unregister the listener whenever a key changes
    getPreferenceScreen().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(this);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    updatePrefSummary(findPreference(key));
    }

    private void initSummary(Preference p) {
    if (p instanceof PreferenceCategory) {
        PreferenceCategory pCat = (PreferenceCategory) p;
        for (int i = 0; i < pCat.getPreferenceCount(); i++) {
            initSummary(pCat.getPreference(i));
        }
    } else {
        updatePrefSummary(p);
    }
    }

    private void updatePrefSummary(Preference p) {
    if (p instanceof ListPreference) {
        ListPreference listPref = (ListPreference) p;
        p.setSummary(listPref.getEntry());
    }
    if (p instanceof EditTextPreference) {
        EditTextPreference editTextPref = (EditTextPreference) p;
        p.setSummary(editTextPref.getText());
    }
    }
}

Edited 3 Years Ago by JamesCherrill: Fixes spelling of title to help searches

Deprecated basically means: this is out of date. there's a newer (read: better) way to do this.
especially when writing new software, it is not wise to use deprecated calls in your code. in order to allow older software still to work they won't remove them from the api's, but it's not recommended to use them. usually, the api recommends which method to use instead.

I tried a quick Google on android.preference.Preference and "deprecated", and immediately found pages that explain the replacement for your deprecated methods. It seems, for some reason, that the API doc in this case fails to explain how to replace them, but there are plenty of other sites that do. I don't know Android, so I won't attempt to say any more, except "just google it".

Edited 3 Years Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.