Android: Avoid Jerk When Hiding Soft Keyboard

486 views Asked by At

I am working with android layout in which when I am hiding Proceed to Payment Button as well as Bottom View when Key board is showing, similarly when user press back or done Button in Keyboard keyboard will hide now mean time I am showing save address Button also Bottom View, To check the visibility of key board I am using following solluation and its working fine but the Problem is when KeyBoard hide it take jerk as layout recize

softKeyboardStateWatcher = new SoftKeyboardStateWatcher(mBinding.activityRoot);

        softKeyboardStateWatcher.addSoftKeyboardStateListener(new SoftKeyboardStateWatcher.SoftKeyboardStateListener() {

            @Override
            public void onSoftKeyboardOpened(int keyboardHeightInPx) {
                mBinding.actionProceedToPayment.setVisibility(View.GONE);
                mHomeActivityImplementation.handleBottomTabs(false);
            }

            @Override
            public void onSoftKeyboardClosed() {
                mBinding.actionProceedToPayment.setVisibility(View.VISIBLE);
                mHomeActivityImplementation.handleBottomTabs(true);
            }
        });

As showing in above code when keyboard show layout then a glitch will occur as layout is resizing,to avoid this I use onPostDelay() as shown below

         @Override
            public void onSoftKeyboardClosed() {
                mBinding.activityRoot.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mBinding.actionProceedToPayment.setVisibility(View.VISIBLE);
                        mHomeActivityImplementation.handleBottomTabs(true);
                    }
                }, 100);
            } 

but still facing same problem. I also apply CountDownTimer() but problem not solved and Is there any method to avoid this jerk ?

2

There are 2 answers

0
Gabe Sechan On

First off- that library you're using, SoftKeyboardStateWatcher is not reliable. The functionality you need isn't exposed in the Android framework. Its making a guess, and there's many situations where it will be wrong and say a keyboard exists when it doesn't, and vice versa. So any code that relies on that class will be buggy. My suggestion would be not to provide this type of functionality at all, as how well it works will depend on device, OS version, keyboard used, etc.

Secondly, I don't think jerk is avoidable using that library. Its guess at when a keyboard is shown won't inform you until well after the keyboard expansion is done. So you're going to be waiting until the keyboard changes, then making additional changes. That will always cause jerk. The only thing you can do is animate the buttons in rather than just setting the visibility.

0
Zubair Akber On

The jerk is because the layout is rendering and adjusting its height when the keyboard is hiding or showing and it all depends on the device as we have different devices with different processor and GPU with different clock speed and frame rate respectively, you will find different behavior on different devices, on some devices you may have no jerk and on some devices you have a minor jerk and this all depend on the processor and GPU you have in your device.

You can add animation to your views that you are showing and hiding during keyboard hiding and showing, this will smooth the jerk a little bit.