Select All Checkboxes option in Dialog Box

  • Replies:7
  • OpenNot stickiedAnswered
  • Forum posts: 14

Apr 16, 2013 9:43:49 AM via Website

Hi All,

I have been working on an android application and have the following functionality in place.
1. User clicks on button.
2. A dialog box shows up Name and a Checkbox against each name.
3. The dialog box has Ok and Cancel button.

I want to add a “Select All” option to a list.
Its functionality will be to check all options listed.
Post that when the user clicks on submit; the details values of all available options should be available in the activity.

Can you please suggest how I can proceed further with this?
  • Forum posts: 14

Apr 18, 2013 8:19:21 AM via Website

People ... Appreciate if you can provide your inputs in above mentioned issue !!!

— modified on Apr 18, 2013 8:19:48 AM

  • Forum posts: 16

Apr 18, 2013 11:58:02 AM via Website

I can't answer exactly, but I have a routine to disable all unchecked options in a dialog listview when the required number have been selected. That is, if there are 10 options, once you've checked 2, the rest are disabled until you uncheck one at which point they are enabled again.
Maybe you could do something similar to check each one? Then record the data you need in a list - this could be passed on to the activity from the onclick handler of the button.

Here's code for disabling for example
1ListView dialogListView = ((AlertDialog) getDialog()).getListView();
2
3 for (int position = 0; position < dialogListView.getChildCount(); position++) {
4 if (!selectedItems.contains(position)) {
5 // disable item (grey out) and set as unclickable (so that onclick isn't fired)
6 dialogListView.getChildAt(position).setEnabled(false);
7 dialogListView.getChildAt(position).setClickable(true); // appears reverse logic but works?!
8 }
9 }
  • Forum posts: 16

Apr 18, 2013 1:10:51 PM via Website

I just did a quick test and this works. Call setChecked on the children of the listview.

1private void disableCheckBoxes() {
2 ListView dialogListView = ((AlertDialog) getDialog()).getListView();
3
4 for (int position = 0; position < dialogListView.getChildCount(); position++) {
5 if (!selectedItems.contains(position)) {
6
7 // Check items, disable and make them unclickable
8 ((CheckedTextView)dialogListView.getChildAt(position)).setChecked(true);
9 dialogListView.getChildAt(position).setEnabled(false);
10 dialogListView.getChildAt(position).setClickable(true);
11 }
12 }
13 }
  • Forum posts: 14

Apr 20, 2013 4:55:10 AM via Website

Hey,

Thanks for your inputs.
I haven't been able to try the code that you suggested.

However, following is the code snippet i am using to generate multiple select check boxes in an alert dialog.
I don't see an option in the below code here.
Is implementing via a list view the only option here ??

1final List multiSelectedOptions = new ArrayList();
2 AlertDialog.Builder builder = new AlertDialog.Builder(CountryContact.this);
3 builder.setTitle(R.string.chooseChangeOpt);
4 //showListArr is an array containing strings values. Eg - "John-1" , "Mona-2"
5 builder.setMultiChoiceItems(showListArr, null, new DialogInterface.OnMultiChoiceClickListener() {
6
7 public void onClick(DialogInterface dialog, int which, boolean isChecked) {
8 if (isChecked) {
9 // If the user checked the item, add it to the selected items
10 multiSelectedOptions.add(which);
11 } else if (multiSelectedOptions.contains(which)) {
12 // Else, if the item is already in the array, remove it
13 multiSelectedOptions.remove(Integer.valueOf(which));
14 }
15 }
16 });
17 builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
18 public void onClick(DialogInterface dialog, int id) {
19 Log.i(tagName,"Clicked Items are::"+multiSelectedOptions);
20 selectedContactsForUpdate = multiSelectedOptions;
21 contactsListForUpdate = eligContactsList;
22 LoadingUpdateSelectedContacts updtSelContacts = new LoadingUpdateSelectedContacts();
23 updtSelContacts.execute();
24 }
25 });
26 builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
27 public void onClick(DialogInterface dialog, int id) {
28 }
29 });
30
31 builder.create();
32 builder.show();
  • Forum posts: 16

Apr 20, 2013 12:06:49 PM via Website

If you make a method like the one I posted and stick it in your dialog, then call it from where I highlight below it should work.
The code you have there for building a mulitchoice alert dialog is the same as mine so you shouldn't have a problem.
Best of luck.

Allzhere
Hey,

Thanks for your inputs.
I haven't been able to try the code that you suggested.

However, following is the code snippet i am using to generate multiple select check boxes in an alert dialog.
I don't see an option in the below code here.
Is implementing via a list view the only option here ??

1final List multiSelectedOptions = new ArrayList();
2 AlertDialog.Builder builder = new AlertDialog.Builder(CountryContact.this);
3 builder.setTitle(R.string.chooseChangeOpt);
4 //showListArr is an array containing strings values. Eg - "John-1" , "Mona-2"
5 builder.setMultiChoiceItems(showListArr, null, new DialogInterface.OnMultiChoiceClickListener() {
6
7 public void onClick(DialogInterface dialog, int which, boolean isChecked) {
8 if (isChecked) {
9 // If the user checked the item, add it to the selected items
10 multiSelectedOptions.add(which);
11
12// Add code here to call disableCheckBoxes method
13
14
15 } else if (multiSelectedOptions.contains(which)) {
16 // Else, if the item is already in the array, remove it
17 multiSelectedOptions.remove(Integer.valueOf(which));
18
19// Add code here to enable checkboxes
20 }
21 }
22 });
23 builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
24 public void onClick(DialogInterface dialog, int id) {
25 Log.i(tagName,"Clicked Items are::"+multiSelectedOptions);
26 selectedContactsForUpdate = multiSelectedOptions;
27 contactsListForUpdate = eligContactsList;
28 LoadingUpdateSelectedContacts updtSelContacts = new LoadingUpdateSelectedContacts();
29 updtSelContacts.execute();
30 }
31 });
32 builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
33 public void onClick(DialogInterface dialog, int id) {
34 }
35 });
36
37 builder.create();
38 builder.show();
  • Forum posts: 14

Apr 21, 2013 9:30:39 AM via Website

Hey,

Thanks..I did some work and got it to work.
Please refer to the code below. Following were some of the learnings:
  1. getChildCount() method returns only visible items in list. Since i had to check all checkboxes on "Select All" , i iterated my code using the getCount() method
  2. In order to check/un check the item i called the following method on list view : setItemChecked(position, true);

Appreciate your inputs and help on this .... :)
Will use this feature when I update my app on google play :
play.google.com/store/apps/details?id=country.code.contact.updater.utility

1ListView dialogListView = ((AlertDialog)dialog).getListView();
2 if (isChecked) {
3 if(which==0)
4 {
5 if(dialogListView!=null)
6 {
7 Log.i(tagName, "Dialog List Item Count"+dialogListView.getCount());
8 for (int position = 0; position < dialogListView.getCount(); position++) {
9 if(position>0)
10 {
11 Log.i(tagName, "Iterating for Adding Positions : "+position);
12 // Check items, disable and make them unclickable
13 dialogListView.setItemChecked(position, true);
14 }
15 }
16 }
17 }
18 }