The android.app.AlertDialog.Builder setNeutralButton method is used to set the neutral button in the dialog box. It takes three parameters: the button text, a dialog interface listener for the button, and an optional handler.
Example 1:
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Title of the AlertDialog") .setMessage("Message to be displayed in the dialog box") .setNeutralButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do something when the button is clicked } }) .show();
In this example, we create an AlertDialog.Builder object and set the title and message to be displayed in the dialog box. We use the setNeutralButton method to set the button text to "OK" and assign a click listener to it.
Example 2:
import androidx.appcompat.app.AlertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Title of the AlertDialog") .setMessage("Message to be displayed in the dialog box") .setNeutralButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do something when the button is clicked } }) .show();
In this example, we use the androidx.appcompat.app library to create an AlertDialog.Builder object with the same properties as before, and set the neutral button with the setNeutralButton method.
Java AlertDialog.Builder.setNeutralButton - 30 examples found. These are the top rated real world Java examples of android.app.AlertDialog.Builder.setNeutralButton extracted from open source projects. You can rate examples to help us improve the quality of examples.