2

I am using an AlertDialog to ask the user to enter a numerical value on a long press of a view. This uses the Android soft-keyboard. For a better user experience I would like the keyboard "Enter" button to programmatically click the Alert Dialog's positive button and run it's onClick. This is really awkward because I can't find any reference to the positive button in the dialog objects. Code to illustrate:

    customStakeView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("Custom Stake");
            customStakeSet = false;

            // Set up the input
            final EditText input = new EditText(context);
            input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            // Specify the type of input expected;
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            input.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                    if(keyEvent.getAction() == KeyEvent.ACTION_DOWN){
                        if(!input.getText().toString().equals("")){
                            switch (keyCode){
                                case KeyEvent.KEYCODE_ENTER:
                                    //Positive Button Outcome
                            }
                        }
                    }
                    return false;
                }
            });
            builder.setView(input);

            // Set up the buttons
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String newStake = input.getText().toString();
                    if (!newStake.equals("")) {
                        newStake = newStake.replaceAll("[^\\d.]", "");  //strip down to currency format
                        customStake = new Stake(newStake);
                        customStakeSet = true;

                        deselectAll();
                        selectCustomStake();
                        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(input.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();

            return true;
        }
    });

I've caught the KeyEvent, and if this was a button added via XML, or even defined with a variable, I would easily be able to do

button.performClick();

but AlertDialog doesn't seem to have such a reference

EDIT:

Updated code as per suggestion

1 Answer 1

5

From documentation, use getButton(whichButton)

Gets one of the buttons used in the dialog. Returns null if the specified button does not exist or the dialog has not yet been fully created (for example, via show() or create()).

whichButton can be BUTTON_POSITIVE or any other button you have specified.

Below is a screenshot of it.

screen-shot

You aren't catching AlertDialog returned by .create() method. getButton() is not available to builder, but to AlertDialog object.

builder.setPositiveButton(...);

// you're missing this
final AlertDialog alertDialog = builder.create();

// then, use it like this
alertDialog.getButton(DialogInterface.Button_POSITIVE).performClick();
6
  • Strange... I can't seem to call getButton on the alert dialog object "builder". Am I missing something here? Jan 10, 2018 at 11:12
  • You have to build the dialog first. Call builder.create(). Then, use this to get button from created AlertDialog. Read my answer, it says button will be null if dialog is not created. Jan 10, 2018 at 11:14
  • Can't seem to get this to work. Calling builder.create() doesn't give me any new getters for AlertDialog. Jan 10, 2018 at 11:28
  • I added a screenshot to my answer. Jan 10, 2018 at 11:33
  • Your logic (and the documentation) seems to be correct but somehow the getButton method doesn't seem available. I added a new screenshot. I don't see any major differences between the code examples Jan 10, 2018 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.