Adding buttons android Dialog

we can add three types of button in the android alert dialog which are as follows:

  • Positive button: this button is used to response such as ok or yes button. To create this button, we have to call the setPositiveButton() method and this method takes two arguments; first the button name and other is the object of the DialogInteface, this interface provides the onClickListener() method which we have to develop.
  • Negative button: this button is used for the negative response such as cancel. To create this button, we have to call the setNegativeButton() method.
  • Neutral button: this button is used to perform the other action. It is like a simple button in android, we can perform any action on this button.

Example of AlertDialog box: The xml layout file is as follows:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert Dialog Example" />

</RelativeLayout>

Here we have used the relative layout and used a textview to display the message like Alert Dialog Example.

The java file is as follows:

package com.example.alertdialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

AlertDialog.Builder builder = new AlertDialog.Builder(this);

//Setting message and buttons and action on clicks
builder.setMessage("Do you want to close this application ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
Toast.makeText(getApplicationContext(), "Hello alert dialog", Toast.LENGTH_LONG).show();
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_LONG).show();
dialog.cancel();
}
});

//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("eitworld");
alert.show();
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

In the above java file first we have used some packages as follows:



In this, the first line is used to define the package name for our android application so that our application can be identified uniquely and other packages are the inbuilt packages.
Here we have used android.app.AlertDialog package is used for dialog box and android.content.DialogInterface is used for the interface purpose and last package is used to generate the toast.
After that we have created the object of the AlertDialog.Builder class.



Now we have to set the message and the button for the dialog box as follows:



Here we have set the message using the setMessage() method and this method takes a parameter of type string to show some message. Here setCancelable(false) method is used to create the cancel button on the dialog box and setPositiveButton() method is used to display the message as first argument as yes and one anonymous method is used to define the action when click on the yes button. In this method, when we click on the yes button then on message will be display in the form of toast that will show the message such as “Hello Alert dialog” and after that we have used finish(); method which used to close the current activity or come out from the application.
After that we have used the .setNegativeButton() method that will take one argument as the “No” and in this one anonymous method is used to define the functionality when we click on this button. Here we have used the toast to show the message when we will click on the cancel button. After that we have used the dialog.cancel() method which is used to cancel the dialog and return to the current activity of the application.
When this application launch then the alert dialog will be displayed.
The output is as follows:



If we click on No button then dialog box will disappear and if click on the Yes button then application will close as follows:



When click on No button