Thursday, June 6, 2013

AlertDialog With Checkbox

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();
        }




 

 

Advance Android Topics


                   Customizing Toast In Android 
                   Showing Toast for Longer Time
                   Customizing the Display Time of Toast
                   Using TimePickerDialog and DatePickerDialog In android
                   Animating A Button In Android
                    Populating ListView With DataBase
                    Increasin Size of Checkboxes
                    Android ProgressBar
                    Designing For Different Screen Sizes
                    Handling Keyboard Events 
                    Scheduling Task Using AlarmManger

Customizing Android Views

   
                        Customizing Radio Buttons
                        Customizing Checkboxes In Android 
                        Customizing ProgressBar
                        Customizing Toast In Android 

Android : Introduction


       Eclipse Setup for Android Development

                     Configuring Eclipse for Android Development

          Beginning With Android

                     Creating Your First Android Project
                     Understanding Android Manifest File of your android app


         Working With Layouts

                      Understanding Layouts in Android
                          Working with Linear Layout (With Example)
                                Nested Linear Layout (With Example)
                          Relative Layout In Android
                          Table Layout
                          Frame Layout(With Example)
                         Absolute Layout
                         Grid Layout


       Activity

                     Activity In Android
                     Activity Life Cycle
                     Starting Activity For Result
                     Sending Data from One Activity to Other in Android
                     Returning Result from Activity

     Working With Views

                     Using Buttons and EditText in Android 
                     Using CheckBoxes in Android 
                     Using AutoCompleteTextView in Android
                     Grid View
                     ListView
                     Android ProgressBar
                     Customizing ProgressBar



       Toast

                     Customizing Toast In Android
                     Customizing the Display Time of Toast
                     Customizing Toast At Runtime
                     Adding Image in Toast
                     Showing Toast for Longer Time

     Dialogs In Android

                     Working With Alert Dialog
                     Adding Radio Buttons In Dialog
                     Adding Check Boxes In Dialog
                     Creating Customized Dialogs in Android
                    Adding EditText in Dialog

                   Creating Dialog To Collect User Input

                 DatePicker and TimePickerDialog

                              Using TimePickerDialog and DatePickerDialog In android

    Working With SMS

                  How to Send SMS in Android
                  How To Receive SMS
                  Accessing Inbox In Android

    ListView:

               Populating ListView With DataBase
               Populating ListView with ArrayList

      Menus In Android

                    Creating Option Menu
                    Creating Context Menu In Android

      TelephonyManager

                    Using Telephony Manager In Android

     Working With Incoming Calls

                    How To Handle Incoming Calls in Android
                    How to Forward an Incoming Call In Android
                   CALL States In Android
 

    Miscellaneous

                   Notifications In Android
                   How To Vibrate The Android Phone
                   Sending Email In Android
                  Opening a webpage In Browser
                   How to Access PhoneBook In Android
                   Prompt User Input with an AlertDialog

   Storage:  Storing Data In Android


               Shared Prefferences  In Android

                             SharedPreferences In Android

               Files: File Handling In Android

                              Reading and Writing files to Internal Stoarage
                              Reading and Writing files to SD Card 
                           

                DataBase : Working With Database

                             Working With Database in Android
                             Creating Table In Android
                             Inserting, Deleting and Updating Records In Table in Android
                             How to Create DataBase in Android
                             Accessing Inbox In Android

     Animation In Android:

                  Animating A Button In Android

4 comments:

  1. hello,
    thank you for your tutorial, but i have a question: whenever i open my dialog box again the checkboxes are all unchecked how can i make them keep their state ?

    ReplyDelete
    Replies
    1. Hi
      When user checkes any of the CheckBox, store or save the state of checkBox using SharedPrefference and when user open the Dialog Box fetch the saved/stored values and set the checkBox state using the saved values.

      use the method

      checkBox.setChecked(true) or checkBox.setChecked(false); to set the state of CheckBox

      Pls let me know for more help

      Delete
    2. I am also stuck on this as well. What is "checkBox"? I have used the code you provided above and there is no "checkBox" to use "checkBox.setChecked(true)" on?

      Delete
  2. how can i save the arraylist that hold the value of checked boxes in sharedPrefference. I read your tutorial on SharedPrefference but it doesnt include ArrayList.

    ReplyDelete