Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear focus and remove keyboard on Android?

Tags:

I have a EditText control. If I tap it the softkeyboard will popup however when I press "enter/ok/return" then the EditText control it still has focus and the keyboard up.
How do I close the softkeyboard and remove focus from it?

like image 601
carefacerz Avatar asked Nov 20 '10 21:11

carefacerz


People also ask

How do I turn off keyboard focus on Android?

clearFocus() in its touch event listener. This will clear all the focus on any touched textview. Then I proceed to close the soft keyboard on screen.

How do I dismiss my keyboard on Android?

To dismiss the keyboard, call clearFocus() on the respective element when the button is clicked.

How do I remove edit text from Focus?

It's fine if it worked for you, I solved it with android:focusable="true" android:focusableInTouchMode="true" in the parent RelativeLayout. This answer totally worked for me, removing focus of editText AND closing keyboard.


2 Answers

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0); 
like image 59
Mitul Nakum Avatar answered Nov 02 '22 16:11

Mitul Nakum


In the layout XML file, specify an imeOption on your EditText:

android:imeOptions="actionGo" 

Next, add an action listener to your EditText in the Activity's java file

    mYourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {         public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {             if (actionId == EditorInfo.IME_ACTION_GO) {                 // hide virtual keyboard                 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);                 imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);                 return true;             }             return false;         }     }); 

Where mYourEditText is an EditText object

like image 34
Andrew Avatar answered Nov 02 '22 16:11

Andrew