Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I happen to had exactly the same problem but I figured it out, hope this helps.</p> <p>You need to do the following steps in order to add a Custom Preference without using an XML Reouser</p> <p>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..)</p> <pre class="lang-java prettyprint-override"><code>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")); } } </code></pre> <p>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</p> <pre class="lang-java prettyprint-override"><code>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; } </code></pre> <p>Please let me know if this helps best,</p> <p>EDIT: Code for bindPreferenceSummaryToValue</p> <pre class="lang-java prettyprint-override"><code>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(),"")); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload