17

I'm developing on the Droid Incredible (and have tested on a 1.5 AVD Emulator as well), and one of the tabs in my tab widget consists of a listview and a row with an EditText and a Send button (for a chat feature). I am using the following to close the soft keyboard once I click Send, but it's not working. This is identical to code I've found elsewhere that people have upvoted as correct.

See anything I'm missing?

// in Button's onClick():
EditText chatTextBox = (EditText) findViewById(R.id.chat_entry);
// Handle button click ...
chatTextBox.setText("");

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(chatTextBox.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

I also tried changing the flag to 0. No luck. Anyone know what's up??

****EDIT**** Just realized I was originally using hideSoftInputFromInputMethod() instead of hideSoftInputFromWindow(). Changing it didn't make it work though...

live-love
  • 48,840
  • 22
  • 240
  • 204
stormin986
  • 7,672
  • 15
  • 42
  • 54

5 Answers5

25

Changing HIDE_IMPLICIT_ONLY to 0 did it (after I changed tohideSoftInputFromWindow() from of hideSoftInputFromInputMethod()).

However I'm not sure why HIDE_IMPLICIT_ONLY isn't working since I am not explicitly opening the keyboard with a long press on Menu.

stormin986
  • 7,672
  • 15
  • 42
  • 54
  • 1
    If you will show keyboard with flag `SHOW_FORCED`then it indicates that the user has forced the input method open (such as by long-pressing menu) so it should not be closed until they explicitly do so. [See Documentation](http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html#SHOW_FORCED) – AZ_ Jan 09 '15 at 08:06
  • 3
    If you have **Forced** shown keyboard and want to hide then use following code `InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);` – AZ_ Jan 09 '15 at 08:08
18

Another option to prevent it from activity in AndroidManifest.xml file

android:windowSoftInputMode="stateAlwaysHidden" - This method will prevent loading/showing keyboard when the activity is loaded. But when you click the editable component like edittext the keyboard will open. perfect for my requirement.

<activity
            android:name=".Name"
            android:label="@string/app_name" 
            android:windowSoftInputMode="stateAlwaysHidden">
Joseph Selvaraj
  • 2,225
  • 1
  • 21
  • 21
14

1.first bind your edit text token with keyboard and open
i.e inputMethodManager.showSoftInput(_edittext, 0); //here _edittext is instance of view

2.keyboard will get hidden automatically if the edit text goes hidden from the screen

3.edit text is still on screen but you want to hide the keyboard then use below code imm.hideSoftInputFromWindow(_edittext.getWindowToken(), 0); //this won't work if edittext is not on screen or not focused.

Uday Sravan K
  • 1,305
  • 12
  • 18
0

Try doing in onResume/onCreate:

rootView.requestFocus();

rootView is your main view. This will change the focus away from the EditText.

live-love
  • 48,840
  • 22
  • 240
  • 204
0

This works good for me:

fun FragmentActivity.openKeyboard(){
    val imm: InputMethodManager = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0) // do not use SHOW_FORCED here
}
fun FragmentActivity.hideKeyboard(view: View){ // send any view here, for instance page root view
    val imm: InputMethodManager = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(view.windowToken, 0)
}
Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62