Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android detect Done key press for OnScreen Keyboard

Tags:

android

People also ask

How do I know my Android keyboard is displayed?

Android provides no direct way to determine if the keyboard is open, so we have to get a little creative. The View class has a handy method called getWindowVisibleDisplayFrame from which we can retrieve a rectangle which contains the portion of the view visible to the user.

How do I force keyboard to open on Android?

To be able to open it anywhere, you go into the settings for the keyboard and check the box for 'permanent notification'. It will then keep an entry in the notifications which you can tap to bring up the keyboard at any point.


Yes, it is possible:

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
        }
        return false;
    }
});

Note that you will have to import the following libraries:

import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

An Editor Info is most useful class when you have to deal with any type of user input in your Android application. For e.g. in login/registration/search operations we can use it for more accurate keyboard input. An editor info class describes several attributes for text editing object that an input method will be directly communicating with edit text contents.

You can try with IME_ACTION_DONE .

This action performs a Done operation for nothing to input and the IME will be closed.

Using setOnEditorActionListener

EditTextObj.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            /* Write your logic here that will be executed when user taps next button */
            handled = true;
        }
        return handled;
    }
});

Using Butterknife you can do this

@OnEditorAction(R.id.signInPasswordText)
boolean onEditorAction(TextView v, int actionId, KeyEvent event){
    if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
        /* Write your logic here that will be executed when user taps next button */
    }
    return false;
}