Abinandhan Selvaraj
Abinandhan Selvaraj

Reputation: 33

Submit an AlertDialog.Builder containing an EditText by pressing enter on the keyboard in android studio

This is my code for an AlertDialog.Builder that has a custom view with an EditText. After entering the value inside the EditText, I want the press of Enter on the keyboard to act the same way as the PositiveButton of the AlertDialog.Builder. I have included the necessary 'imeOptions' part in the XML file. I manage to execute the code when pressing Enter, but the AlertDialog.Builder is still on screen and does not dismiss like when the PositiveButton on the AlertDialog.Builder is clicked.

    //AlertDialog to set weekly income
    incomeAlert = new AlertDialog.Builder(this);
    incomeInflater = this.getLayoutInflater();
    incomeDialogView = incomeInflater.inflate(R.layout.activity_weekincome, null);
    incomeAlert.setView(incomeDialogView);
    et_WeekIncome = incomeDialogView.findViewById(R.id.ls_WeekIncome);
    et_WeekIncome.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                submitIncome();
                return true;
            }
            return false;
        }
    });
    incomeAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            submitIncome();
        }
    });

Thanks in advance for any help.

UPDATE: I managed to dismiss the AlertDialog.Builder by adding another piece of code as shown below

        AlertDialog incomeDialog = incomeAlert.create();
        incomeDialog.show();

Then when needing to dismiss, I use

        incomeDialog.dismiss();

Since dismiss() is not available with AlertDialog.Builder, I had to create the Builder through an AlertDialog. Then I call dismiss() on the AlertDialog.

Thank you all for your input.

Upvotes: 0

Views: 1787

Answers (3)

Clo Knibbe
Clo Knibbe

Reputation: 449

Since dismiss() is not available with AlertDialog.Builder, I had to create the Builder through an AlertDialog. Then I call dismiss() on the AlertDialog.

There is another way to deal with this problem: use the setOnShowListener callback to set the key listener. This gives you access to the dialog's dismiss() method.

incomeAlert.setOnShowListener((DialogInterface d) -> {
    et_WeekIncome.setOnKeyListener((v, keyCode, event) -> {
        if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_ENTER) {
            onClick(dialog, BUTTON_POSITIVE);
            d.dismiss();
            return true;
        }
        return false;
    });

Upvotes: 0

KalanaChinthaka
KalanaChinthaka

Reputation: 321

You can use the above setOnKeyListener this way.

et_WeekIncome.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event)
{
    if (event.getAction() == KeyEvent.ACTION_DOWN)
    {
        switch (keyCode)
        {
          case KeyEvent.KEYCODE_ENTER:
            submitIncome();
            return true;

          default:
            break;
        }
      }return false;
     }
});

Upvotes: 0

Shekhar Suman
Shekhar Suman

Reputation: 151

You can use OnKeyListener with your edit text to handle a specific keypress.

mEditTV.setOnKeyListener(new View.OnKeyListener() {

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            // do action
            return true;
        }
        return false;
    }
});

Upvotes: 2

Related Questions