1

I'm using ViewPager to display two fragments as pages in my application. In page 1, there are EditText fields. I want to programmatically dismiss the keyboard associated with any of those textfields (in Page 1), when I scroll to Page2 (or select Tab2). I've written the following code for that:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {


        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            View currentView = getCurrentFocus();
            if (currentView != null) {

                InputMethodManager imm = (InputMethodManager)  currentView.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(currentView.getWindowToken(), 0);
            }

        } });

If the keyboard is visible while in Page 1, tapping on the second tab will result in Page 2 displayed in a 'scrolled down' state. This makes the Toolbar goes up, over the status bar making the Toolbar title almost invisible to read. Please check the screenshots below (1st image is Page1 and second is Page2):

Page1 Page2

How to rectify this?

DroidHeaven
  • 2,414
  • 3
  • 25
  • 31

1 Answers1

0

try this it works

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                switch ((actionId))
                {
                    case EditorInfo.IME_NULL:break;
                }
                return false;
            }
        });

and call editText.onEditorAction(EditorInfo.IME_NULL);

Farido mastr
  • 464
  • 5
  • 12
  • Sorry. The problem isn't that the keyboard is not dismissing. I can dismiss keyboard with the code I already posted in the question. The problem is the scrolling behaviour I mentioned. – DroidHeaven May 10 '17 at 08:49