I'm new with android.
Currently I want to show an AlertDialog
box with 'OK' & 'Cancel' buttons.
The default is PositiveButton: Left, NegativeButton: Right
Can you let me know how can I move the PositiveButton to the right side & NegativeButton to the left side?
Is there any chance/trouble if Negativebutton cause a bad sound when pressing OK, if We change text "OK" to NegativeButton & "Cancel" to PositiveButton.
My Code:
AlertDialog.Builder builder = new AlertDialog.Builder(SUtils.getContext());
builder.setMessage("Confirmation?")
.setCancelable(false)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Todo
dialog.cancel();
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//TOdo
}
})
dialog = builder.create();
Thanks, Angel
But I recommend to go along with the convention unless you have a good reason to change the order. That will make easier for users to use your application.
There is no way to change the diffault setting in android But you can change the text ok to cancle set the functionally accroding this
A really simple way to shift the buttons of the
AlertDialog
to the right hand side is to hide theleftSpacer
, aLinearLayout
within the core XML which handles the default layout.I have figured out that there is a space layout between the
neutral button
and-ve/+ve buttons
with the place "1" in thebuttonBarLayout
in which the buttons.So, at first we need to remove that space and or make it's visibility
GONE
(invisible
will let it still takes a space in thebuttonBarLayout
) also we better to use method onShowListner better that doing that after showing the dialog by:then the rest is your design, wish that helps
check this https://github.com/hslls/order-alert-buttons
This is not the most elegant of ways but it will do what you want
Just make the
Cancel
button as positive and theOk
button as negative.