Post a form with the Android virtual keyboard

Updated

The usual way of implementing any kind of an input form, is it on a web page or in an mobile application, is first to add some fields and then some buttons to handle the form submission. So in the basic case, the user first fills the fields and then use the buttons to submit the form.

Android form screenshot

For ages the web browsers have contained a little shortcut to streamline the process, the enter which submits the form without the need of using the a button for the submission. This is also available in Android with the help of TextView.OnEditorActionListener class.

You can add a OnEditorActionListener to a field or fields and execute methods based on input clicks. For example, if you have login form like the one depicted in the picture, you can easily submit it with the following lines of code:

passwordField.setOnEditorActionListener(new TextView.OnEditorActionListener() {

    @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            handleLogin();
        }

        return false;
    }
});

So now when the user presses the Done button after he/she has written text to the password field, the login is automatically submitted withou the need for pressing a separate Login button.