Android: how to prevent jumping/flashing of views when trying to replace keyboard with a view

762 views Asked by At

I hide the keyboard. If I immediately make my view visible, there is a flash of the view above the keyboard, and then it goes down, on the place of the keyboard.

If I wait for the keyboard to hide/show and resize my layout (with onSizeChanged of my global layout), and then try to make my view visible, in many cases the system does not re-estimate my layouts correctly (maybe because I'm making a view visible in some weird part of the layout changes lifecycle?) So in many cases I just end up with no view shown. Sometimes there is weird space above the keyboard on the place of the view that should be hidden. If I do post(Runnable) and then call requestLayout() or make the view visible in the next frame, it is not good, because the flashing is there again, of course.

So basically I want to know the exact frame when the keyboard will resize my views, so that I can make them visible/invisible and prevent the flashing of different view structure/sizes. If I wait for onSizeChanged, it seems that it is too late.

I really hoped that the system will behave correctly and re-estimate my views sizes and position them in the right places, but if that is done in onSizeChanged, it doesn't work correctly.

1

There are 1 answers

0
Danail On

I ended up doing the following:

  1. disable resizing the main screen between hiding parts of it and waiting for the keyboard to show up
  2. enable resizing (and triggering resizing) once the keyboard was shown.
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (resizingEnabled) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    } else {
        setMeasuredDimension(getWidth(), getHeight());
    }
}