Aug 28, 2014

AlertDialog of Android


In android,  we can make  simple dialog using AlertDialog.

I make simple button program for alertdialog test.


This is program's layout for alertdialog test.

Simple!! I make 3 buttons.

First button is one button alertdialog.

Second button is two buttons alertdialog.

Third button is three buttons alertdialog.



 If you click to first button(one button alertdialog test), you can see this dialog.



 If you click to second button(two buttons alertdialog test), you can see this dialog.



 If you click to third button(three buttons alertdialog test), you can see this dialog.

This is JAVA code 1 button.


  1. AlertDialog.Builder ad=new AlertDialog.Builder(this);
  2. ad.setTitle("1 button").setMessage("This is one button dialog.").setNeutralButton("check",new DialogInterface.OnClickListener() {
  3.     public void onClick(DialogInterface dialog, int which) {
  4.         Toast.makeText(getApplicationContext()"Test end!!", Toast.LENGTH_SHORT).show();
  5.         }
  6.     }).create().show();

setTitle is alertdialog's title text.

setMessage is alertdialog's message text.

setNutralButtun is alertdialog's button.

button's method is

setPositiveButton(CharSequence text, OnClickListener listener)
setNegativeButton(CharSequence text, OnClickListener listener)
setNeutralButton(CharSequence text, OnClickListener listener)
CharSequence text is button's name.

OnClickListener listener is alertdialog button's listener
create() is dialog create.

show() is dialog show.

This is 2buttons

  1.             ad.setTitle("2 button").setMessage("This is two button dialog.").setPositiveButton("OK",newDialogInterface.OnClickListener() {
  2.                
  3.                 public void onClick(DialogInterface dialog, int which) {
  4.                     Toast.makeText(getApplicationContext()"You Click to OK!!", Toast.LENGTH_SHORT).show();
  5.                    
  6.                 }
  7.             }).setNegativeButton("NO"new DialogInterface.OnClickListener() {
  8.                
  9.                 @Override
  10.                 public void onClick(DialogInterface dialog, int which) {
  11.                     Toast.makeText(getApplicationContext()"You Click to No!!", Toast.LENGTH_SHORT).show();
  12.                    
  13.                 }
  14.             }).create().show();

And 3buttons

  1.             ad.setTitle("3 button").setMessage("This is three button dialog.").setPositiveButton("YES",newDialogInterface.OnClickListener() {
  2.                
  3.                 public void onClick(DialogInterface dialog, int which) {
  4.                     Toast.makeText(getApplicationContext()"You Click to YES!!", Toast.LENGTH_SHORT).show();
  5.                    
  6.                 }
  7.             }).setNegativeButton("NO"new DialogInterface.OnClickListener() {
  8.                
  9.                 @Override
  10.                 public void onClick(DialogInterface dialog, int which) {
  11.                     Toast.makeText(getApplicationContext()"You Click to No!!", Toast.LENGTH_SHORT).show();
  12.                    
  13.                 }
  14.             }).setNeutralButton("cancel"new DialogInterface.OnClickListener() {
  15.                
  16.                 @Override
  17.                 public void onClick(DialogInterface dialog, int which) {
  18.                     Toast.makeText(getApplicationContext()"You Click to CANCEL!!", Toast.LENGTH_SHORT).show();
  19.                    
  20.                 }
  21.            
  22.             }).create().show();


No comments:

Post a Comment