Monday, 29 April 2013

Alert Dialog in Android


This Custom dialog used for Display information or alert to user.

public void dialog_message(String msg)
       {
        final AlertDialog alertDialog = new  AlertDialog.Builder(context.this).create();
              alertDialog.setTitle("Title");
              alertDialog.setIcon(R.drawable.close);
              alertDialog.setMessage(msg);

              alertDialog.setButton("Yes"new DialogInterface.OnClickListener()
              {
                     public void onClick(DialogInterface dialog, int which)
                     {
                           System.exit(0);    
                     }
              });

              alertDialog.setButton2("No"new DialogInterface.OnClickListener()
              {
                     public void onClick(DialogInterface dialog, int which)
                     {    
                           alertDialog.dismiss();
                     }
              });

              alertDialog.show();
       }

2. Single dialog with multiple usage: 
   - This dialog is not used while you are using Date and Time dialog because it has same method (onCreateDialog). As a result below 3rd option is feasible for custom dialog.
     -Dialog id:
                                     final static int DIALOG_Measurement=100;
                     final static int DIALOG_NumberFormate=101;

     -Call dialog like on Button Tap Event:

                     removeDialog(DIALOG_Measurement);
                     showDialog(DIALOG_Measurement);

                     removeDialog(DIALOG_NumberFormate);
                     showDialog(DIALOG_NumberFormate);

Method:
protected Dialog onCreateDialog(int id) {
              AlertDialog dialog = null;
              AlertDialog.Builder builder = null;
              switch(id)
              {
              case DIALOG_Measurement:

                     final CharSequence[] items = {"Imperial","Metric"};
                     builder = new AlertDialog.Builder(this);
                     builder.setTitle("Select Options");
                     builder.setItems(items, new DialogInterface.OnClickListener()
                     {
                           public void onClick(DialogInterface dialog, int item) {

                                  String name=items[item].toString();
                                  TextView txt=(TextView) findViewById(R.id.txt_meas_name);
                                  txt.setText(name);               

                           }                   

                     });

                     builder.setNegativeButton("Cancel"new DialogInterface.OnClickListener()
                     {
                           public void onClick(DialogInterface dialog, int id) {


                                  dialog.cancel();
                           }
                     });
                     dialog = builder.create();
                     break;

              case DIALOG_NumberFormate:
                     final CharSequence[] items1 = {"1000","1,000","1000.00"};
                     builder = new AlertDialog.Builder(this);
                     builder.setTitle("Select Options");
                     builder.setItems(items1, new DialogInterface.OnClickListener()
                     {
                           public void onClick(DialogInterface dialog, int item) {

                                  String name=items1[item].toString();
                                  TextView txt=(TextView) findViewById(R.id.txt_NumFormate_Value);
                                  txt.setText(name);               
                           }

                     });          
                     builder.setNegativeButton("Cancel"new DialogInterface.OnClickListener()
                     {
                           public void onClick(DialogInterface dialog, int id) {


                                  dialog.cancel();
                           }
                     });
                     dialog = builder.create();
                     break;
              }
              return dialog;

       }


2. Custom dialog for user selection:


       private static final int PAIDWITH_CODE=101                     
       String[] temp = null;
       String Paidwith[]={"Visa","MC"};
       temp=Paidwith;  
       showSimplePopUp(PAIDWITH_CODE,"Select Paid with");

 Method:
       private void showSimplePopUp(final int id,String title)
       {
              CharSequence[] itemsPhoto = temp;
              AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
              helpBuilder.setTitle(title);
              //helpBuilder.setMessage("This is a Simple Pop Up");
              helpBuilder.setItems(itemsPhoto, new DialogInterface.OnClickListener()
              {

                     public void onClick(DialogInterface dialog, int item)
                     {
                           if (id == PAIDWITH_CODE)
                           {  
                                  String paidwith=temp[item].toString();
                                  txt_paidwith.setText(paidwith);
                           }   
                     }
              });

              // Remember, create doesn't show the dialog
              AlertDialog helpDialog = helpBuilder.create();
              helpDialog.show();
       }

No comments:

Post a Comment