Reputation: 3868
I have implemented ActionBar Tab in my app. But I am facing one issue during tab change. My tabs contains mainly webview, but one tab contains edit text. when i click on edit text keyboard appears, and with keyboard appearing if I am changing the tab, keyboard is not disappearing. I tried few of the simple solution like hiding it explicitly, but no success.
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(fragment.getView().getApplicationWindowToken(), 0);
this i am calling in onTabSelected() of class that implements ActionBar.TabListener. I don't know how to solve this problem , neither getting relevant information.
Thanks in advance. Any help will appreciated.
Update and Answer
Eric answer somewhat gave me a push and helped me achieve the answer, so i am marking his answer as correct with my change. ie I have added the eric's code in my onTabUnselected
but not in tabSelected, as when i was trying to get view at that moment view was not created thus was getting view as null. so my final code was
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
View target = initialisedFragment.getView().findFocus();
if (target != null)
{
InputMethodManager mgr = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(target.getWindowToken(), 0);
}
}
Upvotes: 2
Views: 2863
Reputation: 193
I use the following, this option to capture the current view in which the device is working :
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);
}
}
Upvotes: 0
Reputation: 67502
I don't believe you can just pick a View
and use it as the window token. You have to find the field that is currently showing the keyboard.
This is a port of a method I've used before, it's worth a try:
View target = fragment.getView().findFocus();
if (target != null) {
InputMethodManager imm = (InputMethodManager) target.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
}
If that doesn't work, there's lots of other methods that have been reported to work.
Upvotes: 2