The android.app AlertDialog.Builder setMessage method is used to set the message displayed in an alert dialog box in an Android application. This method can be used to display any message or information to the user.
Example 1:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setMessage("Are you sure you want to delete this file?"); alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // Code for deleting the file } }); alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // Do nothing } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show();
In this example, setMessage is used to set the message "Are you sure you want to delete this file?" in an alert dialog box. The dialog box also contains two buttons, 'Yes' and 'No'. The positive button is used to delete the file, while the negative button is used to cancel the operation.
Package Library: android.app
Example 2:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setMessage("Please enter your name:"); final EditText input = new EditText(this); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); input.setLayoutParams(lp); alertDialogBuilder.setView(input); alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { String name = input.getText().toString(); // Code for using the name } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // Do nothing } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show();
In this example, setMessage is used to set the message "Please enter your name:" in an alert dialog box. The dialog box contains an EditText view where the user can enter their name. The positive button is used to retrieve the entered name and perform some operation on it.
Package Library: android.app
Java AlertDialog.Builder.setMessage - 30 examples found. These are the top rated real world Java examples of android.app.AlertDialog.Builder.setMessage extracted from open source projects. You can rate examples to help us improve the quality of examples.