Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

addpreferencesfromresource and findPreference deprecated #316

Open
armoun opened this issue Nov 26, 2016 · 7 comments
Open

addpreferencesfromresource and findPreference deprecated #316

armoun opened this issue Nov 26, 2016 · 7 comments

Comments

@armoun
Copy link

armoun commented Nov 26, 2016

Java

  public class SettingsActivity extends PreferenceActivity
        implements Preference.OnPreferenceChangeListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Add 'general' preferences, defined in the XML file
        **addPreferencesFromResource**(R.xml.pref_general);

        // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
        // updated when the preference changes.
        bindPreferenceSummaryToValue(**findPreference**(getString(R.string.pref_location_key)));
    }

    /**
     * Attaches a listener so the summary is always updated with the preference value.
     * Also fires the listener once, to initialize the summary (so it shows up before the value
     * is changed.)
     */
    private void bindPreferenceSummaryToValue(Preference preference) {
        // Set the listener to watch for value changes.
        preference.setOnPreferenceChangeListener(this);

        // Trigger the listener immediately with the preference's
        // current value.
        onPreferenceChange(preference,
                PreferenceManager
                        .getDefaultSharedPreferences(preference.getContext())
                        .getString(preference.getKey(), ""));
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();

        if (preference instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list (since they have separate labels/values).
            ListPreference listPreference = (ListPreference) preference;
            int prefIndex = listPreference.findIndexOfValue(stringValue);
            if (prefIndex >= 0) {
                preference.setSummary(listPreference.getEntries()[prefIndex]);
            }
        } else {
            // For other preferences, set the summary to the value's simple string representation.
            preference.setSummary(stringValue);
        }
        return true;
    }

}

XML

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">


    <EditTextPreference
        android:key="@string/location_label"
        android:title="@string/pref_location_key"
        android:defaultValue="@string/pref_location_default"
        android:inputType="text"
        android:singleLine="true"

        />
</PreferenceScreen>

error

@cdlei
@ceruleanotter
How I can use that in API 10+ ?
thanks

@armoun
Copy link
Author

armoun commented Nov 26, 2016

Hoyyyyaaaaaaaaaaaaa Finaly I Fixed that

PreferenceActivity 

public class SettingsActivity extends PreferenceActivity
        implements Preference.OnPreferenceChangeListener {

// make a class to use PreferenceFragment because addPreferencesFromResource and findPreferencedeprecated in PreferenceActivity 
public static class Presf extends PreferenceFragment{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.pref_general);

        // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
        // updated when the preference changes.

      //make new class from SettingsActivity because bindPreferenceSummaryToValue is nonstatic class
//and now I can use that by new class
        SettingsActivity m = new SettingsActivity();
        m.bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));

    }

}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Add 'general' preferences, defined in the XML file
       
// finaly for setup Prefrence I used this and replace that by new Prefs Class, Hoya its Worked and data input via user is change and weather is update :D
        getFragmentManager().beginTransaction().replace(android.R.id.content, new Presf()).commit();


    }

    /**
     * Attaches a listener so the summary is always updated with the preference value.
     * Also fires the listener once, to initialize the summary (so it shows up before the value
     * is changed.)
     */
    private void bindPreferenceSummaryToValue(Preference preference) {
        // Set the listener to watch for value changes.
        preference.setOnPreferenceChangeListener(this);

        // Trigger the listener immediately with the preference's
        // current value.
        onPreferenceChange(preference,
                PreferenceManager
                        .getDefaultSharedPreferences(preference.getContext())
                        .getString(preference.getKey(), ""));
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();

        if (preference instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list (since they have separate labels/values).
            ListPreference listPreference = (ListPreference) preference;
            int prefIndex = listPreference.findIndexOfValue(stringValue);
            if (prefIndex >= 0) {
                preference.setSummary(listPreference.getEntries()[prefIndex]);
            }
        } else {
            // For other preferences, set the summary to the value's simple string representation.
            preference.setSummary(stringValue);
        }
        return true;
    }

}

@

@armoun armoun closed this as completed Nov 26, 2016
@armoun armoun reopened this Nov 26, 2016
@armoun armoun closed this as completed Nov 26, 2016
@armoun armoun reopened this Nov 26, 2016
@lovelle3025
Copy link

Sorry guys but I had visitors today. I will try this in a few minutes. Thanks.

@vinaytumbemane
Copy link

Great work @armoun

How did you get this?

    SettingsActivity m = new SettingsActivity();
    m.bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));

// finaly for setup Prefrence I used this and replace that by new Prefs Class, Hoya its Worked and data input via user is change and weather is update :D
getFragmentManager().beginTransaction().replace(android.R.id.content, new Presf()).commit();

@SangTran01
Copy link

Thank you

@armoun
Copy link
Author

armoun commented Dec 5, 2016

@vinaytumbemane
if you see , findPreference is deprecated and cant use that, and bindPreferenceSummaryToValue is nonstatic class and my Presf class is static , and cant call nonstatic class in static, so i make a new nonstatic class in static class and then use that, and excuse me for bad english ;)

@Preeti-gupta02
Copy link

I did the above written changes but still the app crashes when settings option is clicked. Can you suggest me a reason for that , is there any other changes we need to make?

@armoun
Copy link
Author

armoun commented Jan 27, 2017

@Preeti-gupta02 show me your cods and your logs

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants