简体   繁体   中英

Is it possible to create a preference screen with PreferenceFragment without using an XML resource?

I am currently able to create a preference screen with a PreferenceFragment, in which I assign my preferences using:

addPreferencesFromResource(R.xml.preferences);

Instead of using the resource 'R.xml.preferences', I would like to use my SharedPreferences i have already saved, for example:

SharedPreferences prefs = this.getActivity().getSharedPreferences("preferences", 0);

addPreferencesFromResource(prefs);

However, this does not work. Is it possible to do this? If so, How? Or is it required that I use an xml document?

I happen to had exactly the same problem but I figured it out, hope this helps.

You need to do the following steps in order to add a Custom Preference without using an XML Reouser

First: You need to create a Preference Screen, this is as it says a "screen" which can containt several Preferences and must be linked to your PreferenceFragment or PreferenceActivity. Consider the Following PreferenceFragment for example (and consider it is contained in a superior Activity..)

public static class YourPreferenceFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

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

        PreferenceScreen p = createPreferences();
        this.setPreferenceScreen(p);//Set the PreferenceScreen as the current one on this fragment

        //now bind values, notice we use p.findPreference which means whe look into the preferenceScreen Associated with the PreferenceFragment/Activity
        bindPreferenceSummaryToValue(p.findPreference("some_key"));
    }
}

Now Consider that createPreferences is a method that will return a PreferenceScreen with your custom preferences such as a ListPreference or CheckboxPreference. This is how you really create the preferences and add them into a PreferenceScreen

private PreferenceScreen createPreferences()
    {
        PreferenceScreen p =
        getPreferenceManager().createPreferenceScreen(getActivity());

        ListPreference listPref = new ListPreference(getActivity());

        listPref.setKey("some_key"); //Refer to get the pref value
        CharSequence[] csEntries = new String[]{"Item1","Item2"};
        CharSequence[] csValues = new String[]{"1","2"};
        listPref.setDefaultValue(-1);
        listPref.setEntries(csEntries); //Entries(how you display them)
        listPref.setEntryValues(csValues);//actual values
        listPref.setDialogTitle("Dialog title");
        listPref.setTitle("Title");
        listPref.setSummary("Some summary");

        p.addPreference(listPref);

        return p;
    }

Please let me know if this helps best,

EDIT: Code for bindPreferenceSummaryToValue

private static void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

    // Trigger the listener immediately with the preference's
    // current value.


    if (preference instanceof CheckBoxPreference) {
        sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, PreferenceManager.
                getDefaultSharedPreferences(preference.getContext()).
                getBoolean(preference.getKey(),false));

    } else {
        sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, PreferenceManager.
                getDefaultSharedPreferences(preference.getContext()).
                getString(preference.getKey(),""));
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM