Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Soft Keyboard on Done Keypress in Android?

I'm struggling with the done button on the soft keyboard. I can't get the soft keyboard Done key press to hide the keyboard. From another button, it works perfectly with

imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);

but the onKeyListener does not function the way I want. When I hit the editText, the soft keyboard shows up and its content is cleared from characters.

Thanks for listening!

The main.xml:

<EditText      android:id="@+id/answer"      android:layout_gravity="center_horizontal" android:textSize="36px"     android:inputType="phone"     android:minWidth="60dp" android:maxWidth="60dp" /> 

The Java file:

private EditText editText; //... editText = (EditText)findViewById(R.id.answer); editText.setOnClickListener(onKeyboard); editText.setOnKeyListener(onSoftKeyboardDonePress); //...  // method not working: private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener()  {     public boolean onKey(View v, int keyCode, KeyEvent event)      {         if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)         {             // code to hide the soft keyboard             imm = (InputMethodManager) getSystemService(                 Context.INPUT_METHOD_SERVICE);             imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);         }         return false;     } };  private View.OnClickListener onKeyboard=new View.OnClickListener()  {     public void onClick(View v)      {         editText.setText("");     } }; 

The working method using a button (in the same java file):

private View.OnClickListener onDone=new View.OnClickListener()  {     public void onClick(View v)      {         //....         // code to hide the soft keyboard         imm = (InputMethodManager) getSystemService(             Context.INPUT_METHOD_SERVICE);         imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);     } }; 

Edit: When I press key no "9" the keyboard hides. That's odd.

like image 212
Benny Skogberg Avatar asked Aug 05 '10 08:08

Benny Skogberg


People also ask

How do I hide my soft keyboard?

Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How do I turn off soft keyboard on Android after clicking outside?

Ok everyone knows that to hide a keyboard you need to implement: InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);

How can I hide my keyboard while typing?

Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden. The back button may be a physical button or on the touch screen.


2 Answers

Use android:imeOptions="actionDone", like that:

<EditText     ...     android:imeOptions="actionDone" /> 
like image 134
alcsan Avatar answered Sep 21 '22 17:09

alcsan


InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.toggleSoftInput(0, 0); 

with context being your activity.

like image 29
Doge Avatar answered Sep 21 '22 17:09

Doge