Reputation: 2051
I am using this code to hide the keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
This works well. However, I noticed a bug. If I have initially hidden the keyboard using my phone physical back button, then I called the above method, the keyboard will be shown instead of hidden. In other word, seem like the Android system failed to detect I have hidden the keyboard using back button. Instead of hiding the keyboard, it show the keyboard. How to solve this?
Upvotes: 0
Views: 4938
Reputation: 1230
In your Activity
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
or you can add in Androidmanifest.xml
<activity
android:name=".views.activities.tile_details.TileDetailActivity"
android:screenOrientation="portrait"
android:theme="@style/TileDetails.AppTheme.Light"
android:windowSoftInputMode="stateHidden" />
or
public static void hideSoftKeyboard(View view, Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Upvotes: 0
Reputation: 3520
It's because you are toggling the keyboard. Try this:
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Upvotes: 0
Reputation: 2947
just change this line from
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
to
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
As using toggle change the state on the basis of current state. If its hidden it will show and vice versa.
Upvotes: 4
Reputation: 6108
//hide-keyboard:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
//show-keyboard:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Upvotes: 1