Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android set hidden the keyboard on press enter (in a EditText)

When my user press Enter on the virtual android "user validate entry!" keyboard my keyboard stay visible! (Why?)

Here my Java code...

private void initTextField() {     entryUser = (EditText) findViewById(R.id.studentEntrySalary);     entryUser.setOnKeyListener(new OnKeyListener() {         public boolean onKey(View v, int keyCode, KeyEvent event) {             if (event.getAction() == KeyEvent.ACTION_DOWN) {                 switch (keyCode) {                     case KeyEvent.KEYCODE_DPAD_CENTER:                     case KeyEvent.KEYCODE_ENTER:                         userValidateEntry();                         return true;                 }             }            return true;         }     }); }  private void userValidateEntry() {     System.out.println("user validate entry!"); } 

... here my View

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">             <EditText android:id="@+id/studentEntrySalary" android:text="Foo" android:layout_width="wrap_content" android:layout_height="wrap_content" />  </LinearLayout> 

Maybe something wrong on my virtual device?

like image 718
Martin Magakian Avatar asked Mar 12 '10 17:03

Martin Magakian


People also ask

How do I hide the soft keyboard on android after clicking outside EditText?

and put the following code in the onTouch method. InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);

How do I hide the keyboard on my android?

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 you hide the keyboard when 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. To bring the keyboard back into view, tap the typing area.


2 Answers

This should do it:

yourEditTextHere.setOnEditorActionListener(new OnEditorActionListener() {          @Override         public boolean onEditorAction(TextView v, int actionId,                 KeyEvent event) {             if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {                 InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);                  // NOTE: In the author's example, he uses an identifier                 // called searchBar. If setting this code on your EditText                 // then use v.getWindowToken() as a reference to your                  // EditText is passed into this callback as a TextView                  in.hideSoftInputFromWindow(searchBar                         .getApplicationWindowToken(),                         InputMethodManager.HIDE_NOT_ALWAYS);                userValidateEntry();                // Must return true here to consume event                return true;              }             return false;         }     }); 
like image 127
jqpubliq Avatar answered Oct 22 '22 17:10

jqpubliq


Keep the singleLine="true" and add imeOptions="actionDone" to the EditText. Then in the OnEditorActionListener check if actionId == EditorInfo.IME_ACTION_DONE, like so (but change it to your implementation):

if (actionId == EditorInfo.IME_ACTION_DONE) {                  if ((username.getText().toString().length() > 0)                         && (password.getText().toString().length() > 0)) {                     // Perform action on key press                     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);                     imm.hideSoftInputFromWindow(username.getWindowToken(),                             0);                     doLogin();                 }             } 
like image 28
Micha Okkerman Avatar answered Oct 22 '22 17:10

Micha Okkerman