Thursday, January 3, 2013

Adding Check Boxes In Alert Dialog

We can also add check boxes in Alert Dialog and we can check multiple, it is used when we want mutiple option to be selected.

Handling the Click Events on CheckBoxes:

when a Checkbox is clocked means it is checked or unchecked obClick() method is called and the index of the selected item is passed, we can use this index to get the item which is checked




Create an Object of AlertDialog

AlertDialog dialog; 

//following code will be in your activity.java file

final CharSequence[] items = {" Easy "," Medium "," Hard "," Very Hard "};
                // arraylist to keep the selected items
                final ArrayList seletedItems=new ArrayList();
              
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Select The Difficulty Level");
                builder.setMultiChoiceItems(items, null,
                        new DialogInterface.OnMultiChoiceClickListener() {

                 // indexSelected contains the index of item (of which checkbox checked)
                 @Override
                 public void onClick(DialogInterface dialog, int indexSelected,
                         boolean isChecked) {
                     if (isChecked) {
                         // If the user checked the item, add it to the selected items

                         // write your code when user checked the checkbox
                         seletedItems.add(indexSelected);
                     } else if (seletedItems.contains(indexSelected)) {
                         // Else, if the item is already in the array, remove it 

                         // write your code when user Uchecked the checkbox
                         seletedItems.remove(Integer.valueOf(indexSelected));
                     }
                 }
             })
              // Set the action buttons
             .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int id) {
                     //  Your code when user clicked on OK
                     //  You can write the code  to save the selected item here

                   
                 }
             })
             .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int id) {
                    //  Your code when user clicked on Cancel
                  
                 }
             });
      
                dialog = builder.create();//
AlertDialog dialog; create like this outside onClick
                dialog.show();
        }




16 comments:

  1. hi, i'm trying to make a dialog box with checkboxes but i'd like to save the boxes i have checked. Do you have an exemple code for that ?

    ReplyDelete
    Replies
    1. Hi Niiaou
      You can save the check box state using SharedPreffrences. SharedPreffrences is the easiest way to store int, string, boolean values.

      You can learn about SharedPreffrences in this post http://learnandroideasily.blogspot.in/search/label/SharedPreferences.

      In the above postI have explained SharedPreferences with example.
      If you need more help, please let me know.

      Delete
  2. hi,
    I'm trying your tutorial with checkboxes and i was wondering why are you using "Integer.valueOf(indexSelected)" instead of "indexSelected" aren't they both integer ?

    I have tried to use your tutorial for the sharedpreferences and it seem kinda hard, i don't understand how i can complete the value of the checkbox from the "setPositiveButton function"

    ReplyDelete
    Replies
    1. yes Niiaou there is no need of Integer.valueOf(indexSelected), we can write only indexSelected , both are same. thanks for pointing out.

      And for your 2nd question you can try following

      @Override
      public void onClick(DialogInterface dialog, int indexSelected,
      boolean isChecked) {
      if (isChecked) {
      // If the user checked the item, add it to the selected items
      seletedItems.add(indexSelected);
      sharedPrefferenveObject.putInt("CHECKBOXSTATE",1);// like that
      } else if (seletedItems.contains(indexSelected)) {
      // Else, if the item is already in the array, remove it
      seletedItems.remove(Integer.valueOf(indexSelected));
      sharedPrefferenveObject.putInt("CHECKBOXSTATE",0)
      }

      Delete
  3. on my listview i have 2 buttons which opened one checkbox each, do i have to make a sharedPreferencesObject in each function which launch a checkbox ?

    so to sum it up I have to make a sharedPreferenceObject for each object i want to saved the states ?

    Also for my listview, how can i save the rows ?

    Thx for your help.

    ReplyDelete
    Replies
    1. Firstly I did not got your problem clearly.
      If you have checkbox in list view and want to save the state of each checkbox of listView then SharedPreffrence will not work here. You can use an ArrayList of boolean type and can store the state of each checkbox at particular indexes in arraylist.

      Please let me know for more help.
      I will write a blog "ListView With CheckBoxes" very soon and explain this, Hope it will resolve your problem.

      Delete
  4. Hi!
    Thank you for the tutorial/code!
    I tried to put the checkboxes in an existig dialog but it doesn't work - no checkboxes visible at all.
    Then I deleted the setMessage(...) method from the builder and now it works. :)

    ReplyDelete
  5. Thanks for your example.
    I'd like to know if there's a way to enable/disable checkbox in the dialog at the beginning?(by the time dialog created)

    ReplyDelete
  6. Hi
    Thank you for the code...
    how to uncheck the checkbox on the cancel button using multichoiceItems..Plz help me..

    ReplyDelete
  7. thank you so much! it was very helpfull

    ReplyDelete
  8. hey, if i've many textview in an item of the listview then how can i get the value of the textview on clicking the checkbox of that item?

    ReplyDelete
  9. hi,
    this is helpful!
    I have 1 question, how to disable the OK and CANCEL button if the checkbox is unchecked and enable when checkbox checked?

    ReplyDelete
  10. Hi, As I do so to be in the dialog window, selecting one of the options, each option send me a separate window.
    Thank you.

    ReplyDelete
  11. hi, how can i setText with checked value?

    ReplyDelete