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) {
}
});
}
I don't use onPageSelected()
because the hide-keyboard animation screws with the swiping animation. Instead use onPageScrollStateChanged()
:
@Override
public void onPageScrollStateChanged(int state)
{
if (state == ViewPager.SCROLL_STATE_IDLE)
{
if (mViewPager.getCurrentItem() == 0)
{
// Hide the keyboard.
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(mViewPager.getWindowToken(), 0);
}
}
}
Simple way :
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
Common way:
private void hideVirturalKeyboard(){
View v = getCurrentFocus();
if (v != null && v instanceof EditText) {
InputMethodManager mgr = (InputMethodManager)(v.getContext().getSystemService(Context.INPUT_METHOD_SERVIC
E));
mgr.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
In your activity, you can do the following:
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
final InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
}
@Override
public void onPageScrolled(int position, float offset, int offsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});