marshallslee
marshallslee

Reputation: 655

Android: Keeping the soft keyboard open even on clicking the done button

enter image description here

Here, by clicking the done button, the soft keyboard automatically shuts down, but I want to keep it open.

Here's the current code inside the onCreate() method.

final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

etPIN.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
        if((keyEvent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
            String pin1 = etPIN.getText().toString();
            Toast.makeText(PINActivity.this, pin1, Toast.LENGTH_SHORT).show();

            tvPINGuide.setText(getString(R.string.confirm_pin));
            etPIN.setText("");
        }
        return false;
    }
});

Upvotes: 1

Views: 3689

Answers (5)

Paresh
Paresh

Reputation: 6857

If you return true from your overrided method onEditorAction, system does not going to handle the action again. So, In this case you should return true to not hide keyboard when action is EditorInfo.IME_ACTION_DONE.

Here use this code:

edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
      @Override
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
          if (actionId == EditorInfo.IME_ACTION_DONE) {

          }
          return true;
      }
 });

Upvotes: 3

Athul
Athul

Reputation: 821

Try this code

if your return true from your onEditorAction method, action is not going to be handled again. In this case you can return true to not hide keyboard when action is EditorInfo.IME_ACTION_DONE.

    editText = (EditText) findViewById(R.id.edit_text);
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                // do your stuff here
if (editText.getText().toString().equals(PIN)) // they entered correct
       {
           // log them in
           return false; // close the keyboard
       }
       else
       {
           Toast.makeText(Main.this, "Incorrect.", Toast.LENGTH_SHORT).show();
           return true; // keep the keyboard up
       }
            }
            return false;
        }
    });

Upvotes: 0

Vishal Chhodwani
Vishal Chhodwani

Reputation: 2577

Try this one

It will help you definitely

etPIN.setOnKeyListener(new View.OnKeyListener() 
{
        @Override
        public boolean onKey(View view, int keyCode, KeyEvent keyEvent)
        {
            if((keyEvent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
            {
                String pin1 = etPIN.getText().toString();
                Toast.makeText(PINActivity.this, pin1, Toast.LENGTH_SHORT).show();

                tvPINGuide.setText(getString(R.string.confirm_pin));
                etPIN.setText("");
            }
            if (keyEvent.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION) // when done button pressed
            {
                // it will open your keyboard again here
                InputMethodManager inputMethodManager =  (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInputFromWindow(etPIN.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
                etPIN.requestFocus();
            }
            return false;
        }
    });

Upvotes: 0

Quick learner
Quick learner

Reputation: 11457

Try this , its simple

etPIN.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                  //do your stuff here
                }
                return true;
            }
        });

Upvotes: 0

Shankara Narayana
Shankara Narayana

Reputation: 765

Just insert this snippet inside the overrided onKey method when the done key is detected

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);

Source https://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

Upvotes: 0

Related Questions