Search code examples
javaandroidpreferences

How to load dynamic editable Preferences


I am building an App with the new Navigation Drawer. Therefore i created several fragments, which are my app pages. One App page has a preference layout (extending preferencefragments). I have a listpreference loaded with dynamic values. I have setup another preference screen after this list. Now what i want to do is, create depending on what the user selected from the list a dynamic generated preference layout with EditTextPreference.

I can't do this statically in xml, because i can't control how many EditTextPreference items are needed before the app compiles. Is there any way how i can set up a dynamic loadable preferences? Maybe completely without xml only in java? Does android support a layout completely written in java at all? I don't need to take care where this will be saved. It should be loaded dynamically too.

Like (Pseudo-Code);

if (list.item3 == selected)
   show edittext1
   show edittext2
if (list.item1 == selected)
   show edittext8
   show edittext4
   show edittext9

See screenshot for a better explanation.

Any help here is much appreciated!


Solution

  • Check this out: http://developer.android.com/guide/topics/ui/settings.html#Custom

    This is indeed possible and I do it on my app. In Java you need to make a custom class that extends EditTextPreference. Then override the methods you want to change. You can then add your custom view right into your xml like you would a normal EditTextPreference, except when you would create an xml element with the name of EditTextPreference you would do the full package name of your custom preference.

    eg:

    <com.your.name.CustomPreference
    <!-- add all the andoid:x attributes you normaly would:-->
    android:defaultValue="default string"
    etc,
    ....
    />