0

In my app I use this method to detect the keyboard on screen. It works perfectly on api 28+ But once I go below api 28 it starts to cause crashes.

  private fun isKeyboardOpen() {
   window?.decorView?.setOnApplyWindowInsetsListener { v, insets ->
       val systemWindowInsets = insets.inset(0,0,0,200)
        imeVisible = ViewCompat.getRootWindowInsets(requireView())?.getInsets(ime())
        if(imeVisible != null){
            if(imeVisible?.bottom!! > 0){
                ivWeatherIcon1.visibility = View.INVISIBLE
            } else {
                ivWeatherIcon1.visibility = View.VISIBLE
            }
        }
       systemWindowInsets
    }
}




E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app, PID: 8062
java.lang.NoSuchMethodError: No virtual method inset(IIII)Landroid/view/WindowInsets; in class Landroid/view/WindowInsets; or its super classes (declaration of 'android.view.WindowInsets' appears in /system/framework/framework.jar!classes2.dex)
    at com.app.framework.presentation.workouts.reframe.Workout$isKeyboardOpen$1.onApplyWindowInsets(WorkoutMoodReframe.kt:881)
    at android.view.View.dispatchApplyWindowInsets(View.java:9225)
    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:6929)
    at android.view.ViewRootImpl.dispatchApplyInsets(ViewRootImpl.java:1568)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1812)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    at android.view.Choreographer.doFrame(Choreographer.java:658)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Is there a way to make this method work back to api 23?

AndroidDev123
  • 280
  • 3
  • 24

1 Answers1

0

Obviously, keyboard appearance support does not support api <28 yet, so until backward compatibility for getInsets (ime ()) is released - you can use the following code:

private fun isKeyboardOpen() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // your code above
        } else {
            // code from source below
        }
    }

How to check visibility of software keyboard in Android?

Alesh17
  • 366
  • 1
  • 7