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