1

When I press 'Return' key in EditText, it is making new line by making it bigger. How to make EditText lose focus when 'Return' key is pressed? In other words, how to make keyboard disappear when 'Return' is pressed?

nano_nano
  • 12,351
  • 8
  • 55
  • 83
Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109

3 Answers3

5

Use following short of code to get event of Return and disappear keyboard.

(EditText) findViewById(R.id.editText1)).setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == 66) {
            hideKeyboard(v);
            return true; //this is required to stop sending key event to parent
        }
        return false;
    }
});

In above code 66 is code for Return You can also use KeyEvent.KEYCODE_ENTER as preferred by @JJPA.

Following is code to hide keyboard explicitly.

private void hideKeyboard(View view) {
    InputMethodManager manager = (InputMethodManager) view.getContext()
            .getSystemService(INPUT_METHOD_SERVICE);
    if (manager != null)
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • This is hiding keyboard but could not prevent making new line. How to deal with that? – Joe Rakhimov Aug 12 '13 at 14:15
  • @JoeRichard, I have edited my answer with `return true;` (upper half code in answer). This will prevent new line. :) – Chintan Rathod Aug 12 '13 at 14:16
  • @JoeRichard, I don't have any problem with my code. I am remove characters by using "BackSpace". May be you did some changes in code or other which reflects that? Please check again. – Chintan Rathod Aug 13 '13 at 06:52
3

Avoid displaying the return key if you don't want user to press it.

What you can do is, make the EditText singleLine and play around a little with android:nextFocus* to implement a more user friendly IME navigation. For your problem, you can simply modify your EditText from xml like

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edit"
        android:singleLine="true"
/>
Aman Gautam
  • 3,549
  • 2
  • 21
  • 25
0

You need to use TextWatcher so you can catch the \n character and modify text so there is no new lines, heres the magic:

mFirstEditText.addTextChangedListener( new TextWatcher() {

    private boolean mEnterOccurred;

    @Override
    public void onTextChanged(CharSequence text, int start, int before, int count) {
        String textStr = text.toString();
        if(textStr.contains("\n")) {
            // Wipe off the \n
            textStr = textStr.replace("\n", "");
            mEnterOccurred = true;
            mFirstEditText.setText(textStr);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence text, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        if(mEnterOccurred) {
            // Change focus to another EditText
            mSecondEditText.requestFocus();
            // Change flag to default
            mEnterOccurred = false;
        }
    }
});
vilpe89
  • 4,656
  • 1
  • 29
  • 36