Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Hide Keyboard Not Working - cannot hide soft keyboard

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...

like image 529
stormin986 Avatar asked May 09 '10 00:05

stormin986


People also ask

How do I get rid of soft keyboard on Android?

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 dismiss my keyboard on Android?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.


2 Answers

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.

like image 120
stormin986 Avatar answered Oct 19 '22 05:10

stormin986


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">
like image 26
Joseph Selvaraj Avatar answered Oct 19 '22 07:10

Joseph Selvaraj