F.X.
F.X.

Reputation: 7317

Re-implement a preference screen in Android

In my app, I'm trying to have a list of custom servers, and I want to be able to add them and edit their respective settings individually.

The standard Android PreferenceFragment looks and works great for my purposes. Is there a way I can use it to edit selected items? Or, alternatively, can I re-create its look and behavior easily?

In a nutshell :

----------------                                    ----------------
|             +|                                    |              |
| Item A       |                                    |  Preference  |
| Item B       |  -->  Click on "A", "B" or "+" --> |    Screen    |
| ...          |                                    |              |
|              |                                    |              |
----------------                                    ----------------

Note that I also know how to use a screen hierarchy, and that doesn't fit the bill since I can't add or remove items dynamically, while the app is running.

Upvotes: 0

Views: 328

Answers (1)

Waza_Be
Waza_Be

Reputation: 39538

From what I understand, you want to add preferences dynamically in your fragment.. Am I right??

Just look at this sample I found:

onCreate(){
    this.setPreferenceScreen(createPreferenceHierarchy());
}

public PreferenceScreen createPreferenceHierarchy(){
    PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);

    // category 1 created programmatically
    PreferenceCategory cat1 = new PreferenceCategory(this);
    cat1.setTitle("title");
    root.addPreference(cat1);

    ListPreference list1 = new ListPreference(this);
    list1.setTitle(getResources().getString(R.string.some_string_title));
    list1.setSummary(getResources().getString(R.string.some_string_text));      
    list1.setDialogTitle(getResources().getString(R.string.some_string_pick_title));
    list1.setKey("your_key");

    CharSequence[] entries  = calendars.getCalenders(); //or anything else that returns the right data
    list1.setEntries(entries);
    int length              = entries.length;
    CharSequence[] values   = new CharSequence[length];
    for (int i=0; i<length; i++){
        CharSequence val = ""+i+1+"";
        values[i] =  val;
    }
    list1.setEntryValues(values);

    cat1.addPreference(list1);

    return root;
}//end method

Upvotes: 1

Related Questions