Android Dialog

Web Hosting
In this tutorial, I will show you how to create a dialogs in android. Here's a sample code below.

Alert Dialog Example

 AlertDialog alertDialog1 = new AlertDialog.Builder(
                    MainActivity.this).create();

            // Setting Dialog Title
            alertDialog1.setTitle("Alert Dialog");

            // Setting Dialog Message
            alertDialog1.setMessage("Welcome to gaudicos.blogspot.com");

            // Setting Icon to Dialog
            alertDialog1.setIcon(R.drawable.ic_launcher);

            // Setting OK Button
            alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to execute after dialog
                    // closed
                    Toast.makeText(getApplicationContext(),
                            "You clicked on OK", Toast.LENGTH_SHORT).show();
                }
            });

            // Showing Alert Message

            alertDialog1.show();

Output: 




Confirm Dialog Example

AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(
      MainActivity.this);
 
// Setting Dialog Title
alertDialog2.setTitle("Confirm Delete...");
 
// Setting Dialog Message
alertDialog2.setMessage("Are you sure you want delete this file?");
 
// Setting Icon to Dialog
alertDialog2.setIcon(R.drawable.ic_launcher);
 
// Setting Positive "Yes" Btn
alertDialog2.setPositiveButton("YES",
       new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
               // Write your code here to execute after dialog
               Toast.makeText(getApplicationContext(),
                       "You clicked on YES", Toast.LENGTH_SHORT)
                       .show();
           }
       });
// Setting Negative "NO" Btn
alertDialog2.setNegativeButton("NO",
       new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
               // Write your code here to execute after dialog
               Toast.makeText(getApplicationContext(),
                       "You clicked on NO", Toast.LENGTH_SHORT)
                       .show();
               dialog.cancel();
           }
       });
 
// Showing Alert Dialog

alertDialog2.show();

Output:






YesNoCancel Dialog Example


// Creating alert Dialog with three Buttons
 
AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(
       MainActivity.this);
 
// Setting Dialog Title
alertDialog3.setTitle("Save File...");
 
// Setting Dialog Message
alertDialog3.setMessage("Do you want to save this file?");
 
// Setting Icon to Dialog
alertDialog3.setIcon(R.drawable.ic_launcher);
 
// Setting Positive Yes Button
alertDialog3.setPositiveButton("YES",
       new DialogInterface.OnClickListener() {
 
           public void onClick(DialogInterface dialog, int which) {
               // User pressed Cancel button. Write Logic Here
               Toast.makeText(getApplicationContext(),
                       "You clicked on YES", Toast.LENGTH_SHORT)
                       .show();
           }
       });
// Setting Positive Yes Btn
alertDialog3.setNeutralButton("NO",
       new DialogInterface.OnClickListener() {
 
           public void onClick(DialogInterface dialog, int which) {
               // User pressed No button. Write Logic Here
               Toast.makeText(getApplicationContext(),
                       "You clicked on NO", Toast.LENGTH_SHORT)
                       .show();
           }
       });
// Setting Positive "Cancel" Btn
alertDialog3.setNegativeButton("Cancel",
       new DialogInterface.OnClickListener() {
 
           public void onClick(DialogInterface dialog, int which) {
               // User pressed Cancel button. Write Logic Here
               Toast.makeText(getApplicationContext(),
                       "You clicked on Cancel", Toast.LENGTH_SHORT)
                       .show();
           }
       });
// Showing Alert Dialog

alertDialog3.show();

Output:




And lastly the Customized Dialog. See code below!

final Dialog dialog = new Dialog(context);

//Getting the screen resolution
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.date);
if((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_LARGE) ==
Configuration.SCREENLAYOUT_SIZE_LARGE){
   dialog.getWindow().setLayout(350, 290);
}else{
}
//end getting the screen resolution
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ml_logo);
//dialog.getWindow().setBackgroundDrawableResource(R.drawable.bg_header);
dialog.setTitle("Select Date From:");

Button btn_datefrom = (Button) dialog.findViewById(R.id.back);
Button btn_cancel = (Button) dialog.findViewById(R.id.cancel);
btn_datefrom.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DatePicker dp_date = (DatePicker) dialog.findViewById(R.id.dp_date);

int   day_from  = dp_date.getDayOfMonth();
int   month_from= dp_date.getMonth()+1;
int   year_from = dp_date.getYear();

String date_from = Integer.toString(month_from)+" "+Integer.toString(day_from)+", "+Integer.toString(year_from);
String cdate_from = Integer.toString(year_from)+Integer.toString(month_from)+Integer.toString(day_from);
SavePreferences("compare_from", cdate_from);
SimpleDateFormat dateFormat = new SimpleDateFormat(
           "MM dd, yyyy");
   Date myDate = null;
  
       try {
myDate = dateFormat.parse(date_from);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

   SimpleDateFormat timeFormat = new SimpleDateFormat("MMM dd, yyyy");
   String fdate_from = timeFormat.format(myDate); 
dialog.dismiss();
SavePreferences("date_from", fdate_from);
et_datefrom.setText(LoadPreferences("date_from", ""));
}
});

btn_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

dialog.show();


Web Hosting
That's it. Hope this simple tutorial may help you. Happy Coding...

No comments:

Post a Comment