Home » Android » Nested PreferenceScreens under a dynamic list of preferences on Android

Nested PreferenceScreens under a dynamic list of preferences on Android

Posted by: admin June 15, 2020 Leave a comment


I’m trying to create a preferences screen with a dynamic list of entries, and when clicking each of them, I have another screen of preferences.
As an example, think of a list of mail accounts and each one having their account settings available.

While I can create the nesting I want using just PreferenceScreens, this can’t easily be scaled to multiple entries without creating the sub preferences structure in code for each one.

I see several different variations throughout the Android UI.

Is there a recomended way to create such a structure as this?

Possibilites include:

  1. Separate, independant activities
    Works but is messy in my opinion

  2. Nested, code created PreferenceScreens
    Pain in the ass for maintainence and it means the preferences are no longer stored as XML fragments

  3. Nested, inflated PreferenceScreens
    I can’t find a way to expand another XML file into a sub tree

  4. One “sub” PreferenceScreen that is shown using setPreferenceScreen() for each one
    I can’t find a way to hide the “template” PreferenceScreen and it breaks the navigation.

Sample XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    <EditTextPreference android:key="add_console"
    <PreferenceCategory android:title="@string/prefs_consoles_title"
        <PreferenceScreen android:summary="http://cctv.icode.co.uk/"
            android:title="iCode Console">
        <PreferenceScreen android:summary="http://test.icode.co.uk/"
            android:title="Test Console">
    <PreferenceScreen android:title="Console (template)"
        <EditTextPreference android:title="@string/prefs_console_host"
            android:summary="@string/prefs_not_set" android:key="host"></EditTextPreference>
        <CheckBoxPreference android:title="@string/prefs_console_auth"
            android:summary="@string/prefs_console_auth_summary" android:key="auth"></CheckBoxPreference>
        <EditTextPreference android:shouldDisableView="true"
            android:title="@string/prefs_console_authuser" android:key="authuser"
            android:dependency="auth" android:summary="@string/prefs_not_set"></EditTextPreference>
        <EditTextPreference android:title="@string/prefs_console_authpass"
            android:key="authpass" android:dependency="auth" android:summary="@string/prefs_not_set"></EditTextPreference>
        <CheckBoxPreference android:title="@string/prefs_console_pair"
            android:summary="@string/prefs_console_pair_summary" android:key="pair"></CheckBoxPreference>

I want the entries under list to be dynamic and show the console preferences under each.

All other ideas welcome.


How to&Answers:

Have you tried to include dynamically XML files into the main one used for the preferences?

This technique is detailed in these 2 articles:



They explain how to do what you were asking in your point 3:

Nested, inflated PreferenceScreens

Basically the idea would be to use this simple tag to include another XML file into an existing one:

    layout="@layout/layout_to_nest" />

I don’t know if you tried that yet but if not, it seems to be worth a try!