I\'ve searched half a dozen other answers on SO, but haven\'t found one that works. All I\'m trying to do is dismiss the soft keyboard when the user presses the enter butto
Below code works for me.
IN XML:
android:imeOptions="actionDone"
android:inputType="textCapWords"
IN JAVA CLASS:
edit_text.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
if ((actionId==EditorInfo.IME_ACTION_DONE ) )
{
//Toast.makeText(getActivity(), "call",45).show();
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
//or try following:
//InputMethodManager imm = (InputMethodManager)getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(auto_data.getWindowToken(), 0);
return true;
}
return false;
}
});
This is what worked for me. Based on this layout xml:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/ip_override_text_input_sat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:hint="IP Address" />
</com.google.android.material.textfield.TextInputLayout>
I needed to do this for the keyboard to dismiss and the text into to lose focus and hide the cursor.
findViewById<TextView>(R.id.ip_override_text_input_sat).setOnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_ENTER && currentFocus != null) {
val inputManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(
this.currentFocus!!.windowToken,
HIDE_NOT_ALWAYS
)
currentFocus!!.clearFocus()
return@setOnKeyListener true
}
return@setOnKeyListener false
}
Hope this helps someone doing the same
EditText.xml
<EditText
android:id="@+id/edit_text_searh_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:singleLine="true"
android:lines="1"
android:hint="Ingresa palabras claves"
android:imeActionLabel="Search"
android:background="@drawable/rounded_edit_text_search"
android:drawableRight="@android:drawable/ic_menu_search"/>
Fragment.java
final EditText edit_text_searh = (EditText) v.findViewById(R.id.edit_text_searh_home);
edit_text_searh.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit_text_searh.getWindowToken(), 0);
Toast.makeText(getContext(),edit_text_searh.getText(),Toast.LENGTH_SHORT).show();
return true;
}
});