Closing the keyboard in the ViewPager

I have used three tabs using a ViewPager. In correct tab layout, I have one edit text widget, when users click on it, the keyboard appears. Unless the user manually closes the keyboard and scrolls to the middle or left tab, the keyboard remains on the screen. So how do I avoid the keyboard on the other two tabs where I don't need it.

Edit: Here's my code.

    class ViewPagerActivity extends Activity   {



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(1);
    TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.titles);
    titleIndicator.setViewPager(myPager);


      } }

      

Second file: MyPagerAdapter.java

  class MyPagerAdapter extends PagerAdapter implements OnClickListener,
    OnLongClickListener, AdapterView.OnItemSelectedListener {

public Object instantiateItem(View collection, int position) {

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resId = 0;
    View view;
    switch (position) {

    case 0:
        resId = R.layout.left;
        view = inflater.inflate(resId, null);
        break;
    case 1:


        resId = R.layout.main_c;

        view = inflater.inflate(resId, null);


        btn_no1 = (Button) view.findViewById(R.id.one);
        btn_no1.setOnClickListener(this);
                    ((ViewPager) collection).addView(view, 0);
        return view;
           case 2:
        resId = R.layout.right;

        view = inflater.inflate(resId, null);

        main_spinner = (Spinner) view.findViewById(R.id.spinner1);
        main_spinner.setOnItemSelectedListener(this);

        from_spinner = (Spinner) view.findViewById(R.id.spinner2);
        from_spinner.setOnItemSelectedListener(this);

        to_spinner = (Spinner) view.findViewById(R.id.spinner3);
        to_spinner.setOnItemSelectedListener(this);

        swap = (ImageButton) view.findViewById(R.id.swap_spinner);
        swap.setOnClickListener(this);
        ((ViewPager) collection).addView(view, 0);

        return view;


    }

    view = inflater.inflate(resId, null);

    ((ViewPager) collection).addView(view, 0);

    return view;
}

      

And the TitlePageIndicator looks like this: TitlePageIndicator

+3


source to share


3 answers


In my opinion, this is not a serious problem. The user opens the keyboard to enter text and hide it when no longer needed.

Having a reference to your EditText object, this should hide the keyboard:



InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);

      

Call the snippet above when exiting the right tab.

+2


source


Try it.

First listen for the ViewPager.OnPageChangeListener event. Then check if the soft keyboard is visible, if it shows on the page where it is not needed, hide it using this code:



InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
    imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}

      

Hope it helps. :)

+1


source


If it helps anyone, I went out with this:

Utils class

public void hideKeyboard(Activity activity) {
    if (activity == null || activity.getCurrentFocus() == null || activity.getCurrentFocus().getWindowToken() == null) return;

    InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

public void hideKeyboard(Fragment fragment) {
    if (fragment == null || fragment.getActivity() == null || fragment.getActivity().getCurrentFocus() == null || fragment.getActivity().getCurrentFocus().getWindowToken() == null) return;

    InputMethodManager inputManager = (InputMethodManager) fragment.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(fragment.getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

      

and inside the MyPagerAdapter class

 @Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    super.setPrimaryItem(container, position, object);

    //assuming fragment in position 2 should hide the keyboard
    if (position == 2) {
        new SystemGlobal().hideKeyboard((Fragment) object);
    } 
}

      

+1


source







All Articles