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

后端 未结 7 1914
南方客
南方客 2021-01-07 23:11

I\'m new with android.

Currently I want to show an AlertDialog box with \'OK\' & \'Cancel\' buttons.

The default is PositiveButton: Left, Ne

相关标签:
7条回答
  • 2021-01-07 23:37
    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.

    0 讨论(0)
  • 2021-01-07 23:37

    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);
    
    0 讨论(0)
  • 2021-01-07 23: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..

    0 讨论(0)
  • 2021-01-07 23:43

    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

    0 讨论(0)
  • 2021-01-07 23:52

    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.

    0 讨论(0)
  • 2021-01-07 23:52

    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);
    
    0 讨论(0)
提交回复
热议问题