Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set focus to android alert dialog negative button?

I have written code to setFocus to ALert Dialog negative button by using requestFocus(). But the button color will not change.I can able to set background image to that button manually.But i need that one directly from native.How to give focus to second button in ALlert Dialog?

Iam sending the code snippet for understanding

alertbox.show();
alertbox.getButton(AlertDialog.BUTTON_NEGATIVE).requestFocus();

Even I tried with

alertbox.show();
alertbox.getButton(AlertDialog.BUTTON_NEGATIVE).requestFocus(View.FOCUS_FORWARD)

Please any one can responde on this query?

Regards, Android Developer

like image 434
ADIT Avatar asked Aug 29 '11 05:08

ADIT


People also ask

What is alert dialog in Android?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.

What is a DialogFragment?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


1 Answers

Just setOnShowListener() to AlertDialog, and set focus on the negative button.

    alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){

        @Override
        public void onShow(DialogInterface dialog) {

            Button negative = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
            negative.setFocusable(true);
            negative.setFocusableInTouchMode(true);
            negative.requestFocus();
        }
    });
    alertDialog.show();
like image 68
timyau Avatar answered Oct 23 '22 18:10

timyau