I have a ViewPager + ActionBar with tabs. I want to make the soft keyboard hide when I \"swipe\" to another tab but I can\'t figure out how.
I\'ve passed in my Activity
In my case for fragment activity following is working fine.
((CommentActivity)getActivity()).pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
final InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtComment.getWindowToken(), 0);
}
@Override
public void onPageScrolled(int position, float offset, int offsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
UPDATE: This was my fragment activity from which two fragments(CommentFragment,AnotherFragment) are opening in a ViewPager
public class CommentActivity extends FragmentActivity {
ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pager = (ViewPager) findViewById(R.id.viewPager);
.......
.......
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return CommentFragment.newInstance();
default: return AnotherFragment.newInstance();
}
}
@Override
public int getCount() {
return 2;
}
}
}
If keypad is appear for an EditText in CommentFragment and want to switch to AnotherFragment and want to hide keypad at the time of switch
public class CommentFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
.........
.........
.........
((CommentActivity)getActivity()).pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
final InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtComment.getWindowToken(), 0);
}
@Override
public void onPageScrolled(int position, float offset, int offsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}