Android AlertDialog Single Button
157,382
Solution 1
Couldn't that just be done by only using a positive button?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
Solution 2
You could use this:
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setTitle("Title");
builder1.setMessage("my message");
builder1.setCancelable(true);
builder1.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
Solution 3
Another approach
Builder alert = new AlertDialog.Builder(ActivityName.this);
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setPositiveButton("OK",null);
alert.show();
Bonus
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);
builder.setMessage("Message dialog with three buttons");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
builder.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
Now it is up to you to use one,two or three buttons..
Solution 4
This is the closer I could get to the one liner this should be if the Android API was any smart:
new AlertDialog.Builder(this)
.setMessage(msg)
.setPositiveButton("OK", null)
.show();
Solution 5
For code reuse, You can make it in a method like this
public static Dialog getDialog(Context context,String title, String message, DialogType typeButtons ) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title)
.setMessage(message)
.setCancelable(false);
if (typeButtons == DialogType.SINGLE_BUTTON) {
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
}
AlertDialog alert = builder.create();
return alert;
}
public enum DialogType {
SINGLE_BUTTON
}
//Other code reuse issues like using interfaces for providing feedback will also be excellent.
Related videos on Youtube
Author by
Elec0
Updated on April 27, 2020Comments
-
Elec0 about 4 years
I'd like to have an AlertDialog builder that only has one button that says OK or Done or something, instead of the default yes and no. Can that be done with the standard AlertDialog, or would I have to use something else?
-
MSaudi about 9 years@MarianPaździoch why you think that ?
-
SMBiggs over 8 yearsWorks great, as long as you don't mind that single button right justified. :(
-
aberaud almost 8 years@ScottBiggs Button alignment in dialogs are decided by the system and could change between devices, versions, and languages (RTL etc.). The App shouldn't attempt to set the alignment but the intent (positive, negative etc.)
-
Jaimin Modi about 7 yearswhat if i want to add four buttons ?
-
IgorGanapolsky almost 7 yearsWouldn't that cause the one button to be right-justified?
-
JackLametta almost 7 yearsAwesome! Thanks!
-
Utkarsh Vishnoi over 6 yearsI don't want the button to perform any action, it should simply just dismiss the alertbox, Is it a good practice to make an empty onClick listener or should i use a different approach.
-
Joshua Pinter almost 6 yearsThis is okay if it's just for a "confirmation dialog" with a single button with no listener. Anything more than that and it's not worth wrapping in a helper like this because you'll need to handle each button press.
-
Muhammad Younus about 5 yearsSimple and easy.Thanks