I use this code in my android project:
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
But, Eclipse says that setButton()
is deprecated. Please, help me with an alternative solution. Thanks!
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("ALERTTILESTRING")
.setMessage("alertNameString")
.setCancelable(false)
.setNegativeButton("Close",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
I hope the above code is a good one in which dialog works very well
setButton() isn't what is deprecated, but that function + argument combination. There is still setButton(), but you need to give an ID for the button as the first argument for setButton():
alertDialog.setButton(0, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
<do something>;
}
});
This is useful if you want to give all your buttons the same OnClickListener:
class alertDialogOnClickListener implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
switch(which){
case 1:
<do something>;
break;
case 2:
<do something>;
break;
}
}
}
alertDialog.setButton(1, "OK", new alertDialogOnClickListener());
alertDialog.setButton(2, "Cancel", new alertDialogOnClickListener());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With