Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create CheckBoxPreferences

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?

like image 666
askilondz Avatar asked Jun 07 '11 23:06

askilondz


2 Answers

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);
    }
}
like image 67
plowman Avatar answered Oct 20 '22 03:10

plowman


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...

like image 8
slinden77 Avatar answered Oct 20 '22 02:10

slinden77