EDIT : Seems I'm not making myself clear. What I need is a way to hide the soft keyboard whenever i replace the fragment I am in. How do I go about doing this ?
Let me keep this simple. I have an EditText box in Tab Fragment 1.2 which obviously opens op the Soft keyboard when pressed. How do I hide this when the tab is changed? I tried the following in my onTabSelected() which doesn't seem to do anything
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I've tried everything now. None of the suggested solutions I've located so far are helping me in any way.
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.
and put the following code in the onTouch method. InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm. hideSoftInputFromWindow(getCurrentFocus(). getWindowToken(), 0);
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.
programmatically you could use, capturing the view of the active activity on the screen of the device.
public final void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
View focus = getCurrentFocus();
if (focus != null) {
hiddenKeyboard(focus);
}
}
public final void onTabselected(Tab tab, FragmentTransaction fragmentTransaction) {
View focus = getCurrentFocus();
if (focus != null) {
hiddenKeyboard(focus);
}
}
public final void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
View focus = getCurrentFocus();
if (focus != null) {
hiddenKeyboard(focus);
}
}
private void hiddenKeyboard(View v) {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
I had the same issue and placed the following code in my tab fragment just prior to using the FragmentTransaction.replace()
method to change tabs. The onCreate
and onCreateView
methods in each fragment are not triggered after the initial tab selection, so hiding the keyboard can be done before getting to the specific fragment's class. Using mTabHost.getApplicationWindowToken()
rather than editText.getWindowToken()
was a major help. Thanks to whoever found that. Sorry I lost the link.
InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(mTabHost.getApplicationWindowToken(), 0);
.......
fm = getFragmentManager();
....
fm.beginTransaction()
.replace(placeholder, new someFragment(), tabId)
.commit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With