How to show Alert dialog box in Android

In this article we will discuss about how to create dialog box in android. Create dialog box is very easy. I will discussing about difference type of dialogue with one button "OK " button, two buttons "Yes No " buttons and three buttons " Yes No Cancel " buttons.

  1. File->New ->Android Project

  In activity_main.xml file I created three buttons.

 follow this code 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.flashlight.MainActivity" >
    <Button
        android:id="@+id/btnone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="88dp"
        android:text="One Alert Dialog" />
    <Button
        android:id="@+id/btntwo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Two Alert Dialog" />
    <Button
        android:id="@+id/btnthree"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Three Alert Dialog" />
</LinearLayout>
  In MainActivity.java I initialized all button follow this code

Button btn, btn2, btn3;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  btn = (Button) findViewById(R.id.btnone);
  btn2 = (Button) findViewById(R.id.btntwo);
  btn3 = (Button) findViewById(R.id.btnthree);
  btn.setOnClickListener(this);
  btn2.setOnClickListener(this);
  btn3.setOnClickListener(this);
 } 
I need to implement OnClickListener interface and then override method

public void onClick(View v) {
}
 In this we will start with One dialog box. Follow this code with create a simple one dialog box with "OK" button. following the code setTitle() method is used for set title of dialog box. setMessage() for set message of dialog box, and next method call setIcon() used for set icon to alert dialog box.

 AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
  .create();    
  alertDialog.setTitle("One Alert Dialog!");
  alertDialog.setIcon(R.drawable.ic_launcher);  
  alertDialog.setMessage("welcome to androidshare.info");
  alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
 @Override  
  public void onClick(DialogInterface dialog, int which){
      Toast.makeText(getApplicationContext(), "One ",Toast.LENGTH_SHORT).show();
   }
   });
  alertDialog.show();

Out put this program will show like this.




Now with Alert Dialog two buttons

follow this code will create alert dialog two buttons. setPositionButton()  used to create positive button in alert dialog button and setNagativeButton() used to invoke native button to alert dialog button. follow this code

AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(MainActivity.this);
      alertDialog2.setTitle("Two Alert Dialog!");
      alertDialog2.setIcon(R.drawable.ic_launcher);
      alertDialog2.setMessage("Are you want to delete?");
      alertDialog2.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        @Override
    public void onClick(DialogInterface dialog, int which) {
     // TODO Auto-generated method stub
          Toast.makeText(getApplicationContext(), "Deleted...",Toast.LENGTH_SHORT).show();
    }
   });
      alertDialog2.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        @Override
    public void onClick(DialogInterface dialog, int which) {
     // TODO Auto-generated method stub
          dialog.dismiss();
     Toast.makeText(getApplicationContext(), "Cancel",Toast.LENGTH_SHORT).show();
         }
   });
      AlertDialog alertDialog = alertDialog2.create();
   alertDialog.show();



After run this application you will see



Next is Three Alert Dialog box

The method setNeutralButton() used for create a neutral cancel button

AlertDialog.Builder aBuilder = new AlertDialog.Builder(MainActivity.this);
      aBuilder.setTitle("Three Alert Dialogbox");
      aBuilder.setIcon(R.drawable.ic_launcher);
      aBuilder.setMessage("Do you want to save this file?");
      aBuilder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        @Override
    public void onClick(DialogInterface dialog, int which) {
     // TODO Auto-generated method stub
          Toast.makeText(getApplicationContext(), "Clicked YES",Toast.LENGTH_SHORT).show();
    }
   });
      aBuilder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        @Override
    public void onClick(DialogInterface dialog, int which) {
     // TODO Auto-generated method stub
          dialog.dismiss();
     Toast.makeText(getApplicationContext(), "Clicked NO",Toast.LENGTH_SHORT).show();
         }
   });
      aBuilder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
    public void onClick(DialogInterface dialog, int which) {
          Toast.makeText(getApplicationContext(),"Clicked on Cancel",Toast.LENGTH_LONG).show();
     dialog.dismiss();
    }
   });
      AlertDialog alertDialog = aBuilder.create();
   alertDialog.show();
  }

After run this application




Enjoy

Post a Comment

0 Comments