Dynamically create CheckBoxPreferences
Asked Answered
G

4

12

I am currently building out a list of rows with checkboxes dynamically using content from a web service. However, this ListView will need to do pretty much what a PreferenceActivity would accomplish.

I don't know the number of rows as the content is dynamic so I can't create each CheckBoxPreference in XML. How do I go about building a PreferenceActivity that will display an unknown number rows with a CheckBoxPreference dynamically?

Gidgetgie answered 7/6, 2011 at 23:0 Comment(0)
C
27

I think you're looking for something like this:

public class MyPreferenceActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.my_preference_activity);

        //fetch the item where you wish to insert the CheckBoxPreference, in this case a PreferenceCategory with key "targetCategory"
        PreferenceCategory targetCategory = (PreferenceCategory)findPreference("targetCategory");

        //create one check box for each setting you need
        CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);
        //make sure each key is unique  
        checkBoxPreference.setKey("keyName");
        checkBoxPreference.setChecked(true);

        targetCategory.addPreference(checkBoxPreference);
    }
}
Connotation answered 2/7, 2011 at 0:9 Comment(1)
for title: checkBoxPreference.setTitle("my title");Vernavernacular
H
8

Well @Jodes, actually both of you are right, but the correct way of doing this would be using a ListPreference.

I would use a entire programmatic approach, from my experience it's easier to be consistent; either create an entire XML layout via code, or via XML, but mixing the 2 can be weird and you cannot alter everything set via XML...

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

However, using this approach you will run into the platform's limitations of not having a multiple select ListPreference, and you'll probably want to implement something else.

I found this solution, which works great. You'll have to read the comments to find clues about how to debug the code though...

Halfbound answered 1/8, 2012 at 13:54 Comment(2)
this is the way to go.Luxor
You just made my whole day. Clear answer and showed a bit more with using the PreferenceCategory with a list. Thank you!Spindry
I
0

You need a ListView for that, a PreferenceActivity. As discussed in this link, PreferenceActivity should only be used for actually saving preferences.

Instead you could either create a simple dialog with single or multiple choice options: http://developer.android.com/guide/topics/ui/dialogs.html

Or use a ListView as in the API examples Google provides, they give a simple example:

http://hi-android.info/docs/resources/samples/ApiDemos/src/com/example/android/apis/view/List10.html

Iand answered 8/6, 2011 at 0:58 Comment(1)
Thanks for the quick response. This list is meant for saving preferences. The items in this list are filter settings for the app's content and these filters are pulled from a web service. Never mind the logic behind how this app works, I just need to know how to properly build a dynamic PreferenceActivity programmatically with CheckBoxPreferences in each row. It sounds like just using a ListView I know, but it would save me a world of time if it could be done using preferences instead, since thats what it needs to function like.Gidgetgie
Y
0

Use PreferenceFragmentCompat from Preference Compat Library

compile 'com.android.support:preference-v7:23.4.0'

Check this article for the implementation details https://medium.com/@arasthel92/dynamically-creating-preferences-on-android-ecc56e4f0789#.71ssvjses

Yasmeen answered 8/6, 2016 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.