20

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

user1866128
  • 281
  • 1
  • 3
  • 8

7 Answers7

39

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..

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • 3
    That's the only correct and helpful answer since those workarounds posted above don't fix the problem for api 11 and above users, it just causes confusion ;] ... – reVerse Nov 30 '12 at 13:10
3
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.

Caner
  • 57,267
  • 35
  • 174
  • 180
  • Is there any chance/trouble if Negativebutton cause a bad sound when pressing OK, if We change text "OK" to NegativeButton & "Cancel" to PositiveButton. – user1866128 Nov 30 '12 at 12:10
  • It is possible but I have never seen such a thing. – Caner Nov 30 '12 at 12:13
1

A really simple way to shift the buttons of the AlertDialog to the right hand side is to hide the leftSpacer, a LinearLayout within the core XML which handles the default layout.

// Fetch the PositiveButton
final Button       lPositiveButton = lDialog.getButton(AlertDialog.BUTTON_POSITIVE);
// Fetch the LinearLayout.
final LinearLayout lParent         = (LinearLayout) lPositiveButton.getParent();
// Ensure the Parent of the Buttons aligns it's contents to the right.
lParent.setGravity(Gravity.RIGHT);
// Hide the LeftSpacer. (Strict dependence on the order of the layout!)
lParent.getChildAt(1).setVisibility(View.GONE);
Mapsy
  • 4,192
  • 1
  • 37
  • 43
0

This is not the most elegant of ways but it will do what you want

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
                        dialog.cancel();
                    }
                })
                .setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //TOdo
                    }
                })


            diaglog = builder.create();

Just make the Cancel button as positive and the Ok button as negative.

Ankush
  • 6,767
  • 8
  • 30
  • 42
  • Is there any chance/trouble if Negativebutton cause a bad sound when pressing OK, if We change text "OK" to NegativeButton & "Cancel" to PositiveButton. – user1866128 Nov 30 '12 at 12:12
0

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

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();
                        }
                    });


dialog = builder.create();
Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53
0

check this https://github.com/hslls/order-alert-buttons

dependencies {
    compile 'com.github.hslls:order-alert-buttons:1.0.0'
}


AlertDialogParams params = new AlertDialogParams();
params.mTitle = "title";
params.mMessage = "message";
params.mPositiveText = "Ok";
params.mNegativeText = "Cancel";
params.mCancelable = true;
params.mAlign = BUTTON_ALIGN.POSITIVE_BUTTON_LEFT;  // fix button position
params.mClickListener = new AlertDialogClickListener() {
    @Override
    public void onPositiveClicked() {

    }

    @Override
    public void onNegativeClicked() {

    }
};

AlertDialog dialog = AlertDialogBuilder.createAlertDialog(this, params);
mapleleaf
  • 1
  • 1
0

I have figured out that there is a space layout between the neutral button and -ve/+ve buttons with the place "1" in the buttonBarLayout 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 the buttonBarLayout) also we better to use method onShowListner better that doing that after showing the dialog by:

 alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            Button neutralButton = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
            LinearLayout view = (LinearLayout) neutralButtonOrAnyOtherBtnFromThe3Btns.getParent();
            Space space= (Space) view.getChildAt(1);
        }
 });

then the rest is your design, wish that helps

Dasser Basyouni
  • 3,142
  • 5
  • 26
  • 50