Note that there are some explanatory texts on larger screens.

plurals
  1. POBuilding Preference screen in code depending on another setting
    primarykey
    data
    text
    <p>I have searched here and looked at samples but haven't yet found an answer to what is essentially a simple problem. Depending on the choice made in a preceding ListPreference, I want to build a preference screen of CheckBoxPreferences dynamically in code, which is then shown when I click on a simple preference with a click listener. The list and number of check boxes will be different in each case.</p> <p>This is where I have got to so far - just a simple bit of code to test the concept in the onClick listener, but how to get the check box preference screen to appear? There must be a simple explanation why it doesn't. What am I doing wrong?</p> <p>Part of my xml code:</p> <pre><code>&lt;PreferenceCategory android:title="Filters"&gt; &lt;PreferenceScreen android:key="FilterScreen" android:title="Filters" android:summary="Click to change filter settings"&gt; &lt;ListPreference android:title="Filter type" android:summary="Set to: Gliding" android:key="filterType" android:defaultValue="0" android:entries="@array/filterTypeOptions" android:entryValues="@array/filterTypeValues" /&gt; &lt;CheckBoxPreference android:title="" android:summary="Include Aerodrome Notams" android:defaultValue="false" android:key="filterIncludeAerodrome" /&gt; &lt;CheckBoxPreference android:title="" android:summary="Delete night-time Notams" android:defaultValue="true" android:key="filterDeleteNighttime" /&gt; &lt;ListPreference android:title="Select category to change" android:summary="Set to: Airspace organisation" android:key="filterCategory" android:defaultValue="0" android:entries="@array/filterCategoryOptions" android:entryValues="@array/filterCategoryValues" /&gt; &lt;Preference android:title="Show filters for category" android:summary="Click to choose subjects to delete" android:key="filterShow" /&gt; &lt;/PreferenceScreen&gt; &lt;/PreferenceCategory&gt; </code></pre> <p>The contents of "Show filters for category" will depend on the "Filter type" and "Select category to change" settings.</p> <p>This is the simple test code I have for the "Show filters" click listener (cut down just to show essentials):</p> <pre><code>public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener { ------ public static final String KEY_FILTER_SHOW = "filterShow"; ------ private Preference mFilterShow; ------ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.layout.settings); // Get a reference to the preferences ------ mFilterShow = (Preference)findPreference(KEY_FILTER_SHOW); ------ // Set the click listener for Show Filter options mFilterShow.setOnPreferenceClickListener(new OnPreferenceClickListener() { public boolean onPreferenceClick(Preference pref) { Context ctx = pref.getContext(); PreferenceScreen screen = pref.getPreferenceManager().createPreferenceScreen(ctx); CheckBoxPreference cb1 = new CheckBoxPreference(ctx); cb1.setTitle("This is cb1"); cb1.setKey("cb1_key"); cb1.setDefaultValue(false); screen.addPreference(cb1); return true; } }); </code></pre> <p>I am sure that there must be a simple answer to what I have done wrong, or what I've missed! Please help...</p>
    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.
 

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