Force keyboard opening

Asked

Viewed 712 times

2

I have this dialog in my application

public void alertaLocalizar(View v) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.alerta_comum, null);


    Dialog localizar = new Dialog(this);
    localizar.requestWindowFeature(Window.FEATURE_NO_TITLE);
    localizar.setContentView(view);
    localizar.show();
    }

}

And I wanted to force open the keyboard as soon as it was opened, how can I do that?

And taking advantage of the question, I would like to know how to use the keyboard OK button to have the same effect as the Alert OK button and not close the keyboard.

1 answer

3


To force the keyboard open use this code :

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

To hide the keyboard put this code inside the button :

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

Take a look at the documentation of InputMethodManager for better understanding :

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

And complementing with your question about where to put the code,I suggest you create an event to deal with over the keyboard OK button to hide the keyboard through the code I posted.

Example of the Enter button with Keyevent event :

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_ENTER:
          /*Coloque aqui o que irá acontecer caso pressionado o botão*/
          return true;
        }
    return false;
}

Take a look at the Keyevent documentation that will help you do this:

https://developer.android.com/training/keyboard-input/commands.html

  • I’m sorry to be unaware, but where in the code would I have to enter?

  • Thank you @Falion, I got exactly what I wanted, but I just got a little problem, I checked in the documentation of ImputMethodManager and I couldn’t find. I put on the cancel button, to hide the keyboard, this usually happens if the keyboard is open, but when the user clicks on the Back of the device, the keyboard disappears, and clicking Cancel, it reappears. Can you make sure that doesn’t happen? Or better yet, stop the keyboard from disappearing, unless you click cancel?

  • You’re using the Keyevent ? Because if yes, you can put an if/case that will check that only if clicked on Cancel,it will make the keyboard disappear.

  • No, buddy. I’m not using Keyevent. What I did was put on the alert the IMM to force the keyboard to appear as shown the alert, and disappear when clicked Cancel on the alert, what works normal. It turns out that when the keyboard is already closed (the user clicked back and the keyboard hid) and the user clicks Cancel, instead of remaining hidden, it reappears. But I believe it is a Bug since in tests, times reappeared, others remained hidden. So I decided not to use the IMM in this case, since it will not disturb the user in anything. Thank you for your attention!

  • If you have more questions then,can ask friend,because this site is to help those who need. I advise you to always test your codes repeatedly, to be sure where the problem comes from.If I helped you, put my answer as "Useful answer". I’m glad my answer helped you.

  • Thanks, I’m still familiarizing myself with the site, I did this and I will do the next ones too. Sometimes I have some questions that may be quite simple, but I am a web programmer, not Android, hence the need to develop this app and I’m venturing. Thank you for your attention.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.