user1866128
user1866128

Reputation: 281

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

Upvotes: 20

Views: 23293

Answers (7)

Dasser Basyouni
Dasser Basyouni

Reputation: 3252

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

Upvotes: 0

Mapsy
Mapsy

Reputation: 4262

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

Upvotes: 1

mapleleaf
mapleleaf

Reputation: 1

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

Upvotes: 0

Caner
Caner

Reputation: 59168

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.

Upvotes: 3

Mohd Saleem
Mohd Saleem

Reputation: 94

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

Upvotes: 0

Krishnabhadra
Krishnabhadra

Reputation: 34275

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

Upvotes: 39

Ankush
Ankush

Reputation: 6927

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.

Upvotes: 0

Related Questions