Search code examples
androidkeyboardeditview

Open Keyboard for another EditView when click Enter


I would like to open the keyboard for another EditView (than the one I am using) when I click Enter on it (when I am in the first EditView).

The scenario should be like this : I am writing something in the keyboard for the 1st EditView, I have finished so I press "Enter" and it opens the keyboard for the second EditView.

I tried different codes but no success, if somebody could help me please.


Solution

  • I have found by searching with 'setNextFocusDownld' like Kasper suggested, using this answer : Android Softkey's next button not taking focus to spinner

    The code I used is:

    mEditText.setNextFocusDownId(R.id.textView2b);
            mEditText.setOnKeyListener(new OnKeyListener() {
    
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // If the event is a key-down event on the "enter" button
                    if ((event.getAction() == KeyEvent.ACTION_DOWN)
                            && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                        // Perform action on Enter key press
                        mEditText.clearFocus();
                        mEditText2.requestFocus();
                        return true;
                    }
                    return false;
                }
            });
            mEditText2.setOnKeyListener(new OnKeyListener() {
    
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // If the event is a key-down event on the "enter" button
                    if ((event.getAction() == KeyEvent.ACTION_DOWN)
                            && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                        // Perform action on Enter key press
                        mEditText2.clearFocus();
                        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(mEditText2.getWindowToken(), 0);
                        return true;
                    }
                    return false;
                }
            });