David Seroussi David Seroussi - 4 months ago 32
Android Question

FloatingActionButton above KeyBoard without "adjustResize"

In my app I am using a

MapView
, an
EditText
and a
FloatingActionButton
(fab), and I want the fab to move above the keyboard when the
EditText
is clicked. I found that setting
android:windowSoftInputMode="adjustResize"
in the
manifest
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 :

<CoordinatorLayout>

<MapView/>

<RelativeLayout>

<EditText/>

other views ...

</RelativeLayout>

<FloatingActionButton/>

other views ...

</CoordinatorLayout>


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

Answer

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() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();

            root.getWindowVisibleDisplayFrame(r);

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

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

            if(!bottomSheetIsVisible){
                fabSearch.setTranslationY(-(heightDiff + suggestionsBarHeight));
            }

        }
    });
}
Comments