David Seroussi David Seroussi - 5 months ago 40
Android Question

FloatingActionButton above KeyBoard without "adjustResize"

In my app I am using a

, an
and a
(fab), and I want the fab to move above the keyboard when the
is clicked. I found that setting
in the
works well with the fab, but it also resizes my map which is in background, and it gives a really ugly effect when it's resizing.

Here is my layout :





other views ...



other views ...


Any idea on how I could get the "adjustResize" effect without using it ? Or maybe how to exclude a view from resizing but while keeping "adjustResize" ?

Thank you in advance


After some researches, I ended up handling the keyBoard by adding a GlobalLayoutListener on the root view of my layout, and drawing a rectangle of the current visible part of this root view.
I also added 25dp to the actual height of the keyboard because some devices have a suggestions bar above the keyboard that seems to be included in the visible part of the window.
The if condition is there to prevent any translation when a bottomSheet is expanded.

private void handleKeyBoardApparition() {
    root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            Rect r = new Rect();


            int heightDiff = root.getBottom() - r.bottom;

            int suggestionsBarHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,25,getActivity().getResources().getDisplayMetrics());

                fabSearch.setTranslationY(-(heightDiff + suggestionsBarHeight));