Showing posts with label Alert Dialog. Show all posts
Showing posts with label Alert Dialog. Show all posts

Sunday 2 November 2014

Android Tutorial 19 : 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();
        }

Android Tutorial 18 : Adding Radio Buttons In Alert Dialog

We can add a List and Radio Buttons in Dialog so that user can make choice.

For this we need an array of CharSequences items to use in the list.






AlertDialog levelDialog;

// Strings to Show In Dialog with Radio Buttons
final CharSequence[] items = {" Easy "," Medium "," Hard "," Very Hard "};
            
                // Creating and Building the Dialog 
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Select The Difficulty Level");
                builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                   
                    
                    switch(item)
                    {
                        case 0:
                                // Your code when first option seletced
                                 break;
                        case 1:
                                // Your code when 2nd  option seletced
                                
                                break;
                        case 2:
                               // Your code when 3rd option seletced
                                break;
                        case 3:
                                 // Your code when 4th  option seletced            
                                break;
                        
                    }
                    levelDialog.dismiss();    
                    }
                });
                levelDialog = builder.create();
                levelDialog.show();

Android Tutorial 17 : Android Alert Dialog Example

What are  Alert Dialogs

An Alert Dialog is used to show some information or to prompts the user to make.
Dialogs can also be used to take input from users, such dialogs are called Customized Dialogs

Buiding  Alert Dialogs:    

 AlertDialog class is used to create Alert Dialog.

Three things are required to create a dialog.
1:Title : The title of the dialog
2: Message: The message to be given in dialog.
3: Image(Optional): Image to displayed in Title Bar


We can also add buttons in Alert Dialog.

Code to create Alert Dialog

AlertDialog dialog;
                dialog = new AlertDialog.Builder(this).create();
                dialog.setTitle("Close");
                dialog.setIcon(android.R.drawable.ic_dialog_info);
                dialog.setMessage("Want to close this App").


dialog.show();


Adding Buttons to Dialog:


dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which)
            {
                   
                // Your Code
            }   
        });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which)
            {
                   
                  // Your Code
            }   
        });
        dialog.show();


Dismissing A dialog 


dialog.dismiss();