Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog negative button by default highlighted

I am using alert dialog with two buttons.Issue here is whenever my alert dialog gets displayed its negative button is shown highlighted.And this is happening in this dialog only rest other are working fine.Please suggest some solution.

Here is the code:


AlertDialog.Builder alert = new AlertDialog.Builder(context);

            alert.setTitle("Stop On The Go!");
            alert.setMessage(getResources().getString(R.string.tx_confirm_msg_journey));
            alert.setPositiveButton("Stop",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            stopTask();
                        }
                    });
            alert.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            navigateToHomePage();
                            dialog.cancel();
                        }
                    });

            alert.show();

Screenshot: enter image description here

like image 674
Swapnil Avatar asked Feb 25 '16 07:02

Swapnil


2 Answers

Show below code just change one line. alert.show().getButton(DialogInterface.BUTTON_POSITIVE).requestFocus();

 AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());

    alert.setTitle("Stop On The Go!");
    alert.setMessage(getResources().getString(R.string.tx_confirm_msg_journey));
    alert.setPositiveButton("Stop",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    stopTask();
                }
            });
    alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    navigateToHomePage();
                    dialog.cancel();
                }
            });

    alert.show().getButton(DialogInterface.BUTTON_POSITIVE).requestFocus();
like image 132
Ravi Vaghela Avatar answered Sep 19 '22 10:09

Ravi Vaghela


Just use focus on positive

add this line in your positive button

 alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){

        @Override
        public void onShow(DialogInterface dialog) {

            Button positive= alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            positive.setFocusable(true);
            positive.setFocusableInTouchMode(true);
            positive.requestFocus();
        }
    });
like image 23
Aditya Vyas-Lakhan Avatar answered Sep 22 '22 10:09

Aditya Vyas-Lakhan