0

So, I have an EditText just below the custom toolbar I created. And under the edittext, there is a listview. However, when soft keyboard appears, users can't see the list as it gets blocked by the keyboard. How can I move the EditText to the top of the screen pushing the toolbar out of the screen when soft keyboard appears I'm using NoActionBar theme Any help will be appreciated.

EDIT: I have already tried

android:windowSoftInputMode="adjustPan"

in the Manifests.xml

Thanks!.

EDIT: I'm using this code to detect hide the toolbar when keyboard shows up.

        numbersActivityRoot = (CoordinatorLayout) findViewById(R.id.numbers_activity_root_view);
        appBarLayout = (AppBarLayout) findViewById(R.id.appbarLayout);
        numbersActivityRoot.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                //r will be populated with the coordinates of your view that area still visible.
                numbersActivityRoot.getWindowVisibleDisplayFrame(r);

                int heightDiff = numbersActivityRoot.getRootView().getHeight() - (r.bottom - r.top);
                if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                    Log.v("NUMBERS","Hide toolbar");
                    appBarLayout.setExpanded(false);
                } else {
                    Log.v("NUMBERS","Show toolbar");
                    appBarLayout.setExpanded(true);
                }
            }
        }); 

While the log messages are showing correct results for detecting the keyboard, but the toolbar is still not hiding . What am I doing wrong?

Asym
  • 1,836
  • 3
  • 21
  • 36

1 Answers1

2

adjustPan doesn't help if the EditText is fully on screen when the keyboard is up. Personally, I never use adjustPan because it lets the system move your window an unpredictable amount, and I just don't like that at all.

There are two main problems you need to address: Detecting the keyboard is visible, and moving your Toolbar off screen.

For the first part, there is no mechanism provided by the framework to directly be informed of the keyboard being shown/hidden. There are numerous posts on StackOverflow with workarounds (like this one and this one). The basic idea is to compare the height of your content view with the height of the entire phone screen, and if there's a significant difference, you sort of just assume that the soft keyboard is what's occupying the other space.

For the second part, I suggest you use AppBarLayout's ability to scroll/collapse by giving your Toolbar the attribute layout_scrollFlags="scroll" (or maybe "scroll|enterAlways"). When you detect the keyboard change using one of the tricks described in part 1, you can collapse or expand the app bar with appBarLayout.setExpanded(boolean).

Community
  • 1
  • 1
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • Thanks a lot for your answer. I think this is the way to go for hiding the toolbar. However, it's still not working. Can you please take another look at my recent edit and let me know what's wrong? – Asym Dec 04 '16 at 21:05