Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AlertDialog Move PositiveButton to the right and NegativeButton on the left

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

like image 221
user1866128 Avatar asked Nov 30 '12 11:11

user1866128


2 Answers

This might not be a direct answer. But just some information on related topic. From this thread in Google's own forum, Romain guy said..

Going forward the order of positive/negative buttons will be the one used in ICS.

and the convention per OS version is

  • On devices prior to Honeycomb, the button order (left to right) was POSITIVE - NEUTRAL - NEGATIVE.
  • On newer devices using the Holo theme, the button order (left to right) is now NEGATIVE - NEUTRAL - POSITIVE.

If it is a convention, that android/Google wants to follow, isn't it better you follow the same, since your users won't be using your app alone. After all user friendliness is the first thing a developer looks for..

like image 57
Krishnabhadra Avatar answered Nov 18 '22 11:11

Krishnabhadra


AlertDialog.Builder builder = new AlertDialog.Builder(SUtils.getContext());
                builder.setMessage("Confirmation?")
                    .setCancelable(false)
                    .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //TOdo
                        }
                    })
                    .setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //TOdo
                            dialog.cancel();
                        }
                    })


                diaglog = builder.create();

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.

like image 3
Caner Avatar answered Nov 18 '22 13:11

Caner