androidandroid-softkeyboardandroid-coordinatorlayoutcustom-scrolling

coordinator layout custom scroll behavior


I have a layout where i use coordinator layout to collapse appbarlayout to a toolbar when user scroll on the screen.

The default behaviour is quite accurate but problems show up when the user clicks on an edit text field, once the softkeyboard shows up, the screen is pushed up ( i use the flag "adjust resize on my activity) and the appbar layout is off the screen.

What i want to achieve is :

Is this possible?

Best regards.


Solution

  • If you want to collapse AppBarLayout when the Soft Keyboard is shown, you can follow these two steps:

    1- Use this library to detect soft keyboard visibility events: https://github.com/yshrsmz/KeyboardVisibilityEvent

    2- When the soft keyboard opens, use this method to collapse your AppBarLayout:

    appBarLayout.setExpanded(false);
    

    Example:

    KeyboardVisibilityEvent.setEventListener(
        getActivity(),//Or context
        new KeyboardVisibilityEventListener() {
            @Override
            public void onVisibilityChanged(boolean isOpen) {
                if(isOpen){
                    appBarLayout.setExpanded(false);
                }
            }
        });