9

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"
    android:title="@string/prefs_title">
    <EditTextPreference android:key="add_console"
        android:title="@string/prefs_add_console"></EditTextPreference>
    <PreferenceCategory android:title="@string/prefs_consoles_title"
        android:key="list">
        <PreferenceScreen android:summary="http://cctv.icode.co.uk/"
            android:title="iCode Console">
        </PreferenceScreen>
        <PreferenceScreen android:summary="http://test.icode.co.uk/"
            android:title="Test Console">
        </PreferenceScreen>
    </PreferenceCategory>
    <PreferenceScreen android:title="Console (template)"
        android:key="console">
        <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>
    </PreferenceScreen>
</PreferenceScreen>

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

All other ideas welcome.

Thanks

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • I'm not sure I understand the question. We can already do what you want (I think) with just the basic implementation. What is it about the current implementation that you don't like? – user432209 Jan 25 '11 at 01:20
  • well, I can't figure out how to implement it yet... :p – Deanna Jan 25 '11 at 06:40
  • I know I can add each sub tree in code beneath the dynaimc entries I add, but was hoping I could add the sub tree by inflating an XML file. – Deanna Jan 25 '11 at 06:44
  • (damn enter key) Either that or use multiple activities, but I don;t know the best approach. – Deanna Jan 25 '11 at 06:45

1 Answers1

0

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

This technique is detailed in these 2 articles:

http://www.aslingandastone.com/2010/dynamically-changing-android-views/

http://android-developers.blogspot.com/2009/02/android-layout-tricks-2-reusing-layouts.html

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:

<include
    android:id="@+id/layout_to_nest"
    layout="@layout/layout_to_nest" />

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

Yoann Hercouet
  • 17,894
  • 5
  • 58
  • 85