Skip to content

weikeet/SoftKeyboardWatcher

Repository files navigation

SoftKeyboardWatcher

License maven-central

Android API kotlin

Getting started

In your build.gradle:

dependencies {
    implementation 'com.weicools:keyboard-watcher:1.0.1'
}

Usage

  1. set window SoftInputMode to adjustNothing

    Activity#onCreate
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
    
    AndroidManifest.xml activity tag
    android:windowSoftInputMode="adjustNothing"
    
    application/activity theme
    <item name="android:windowSoftInputMode">adjustNothing</item>
  2. setDecorFitsSystemWindows to false

    WindowCompat.setDecorFitsSystemWindows(window, false)
  3. watch keyboard state

    val keyboardWatcher = SoftKeyboardWatcher(requireActivity().window)
    keyboardWatcher.startWatch(requireActivity(), viewLifecycleOwner) { imeHeight, navigationBarsHeight, animated ->
        val translation = -max(imeHeight - navigationBarsHeight, 0).toFloat()
        if (animated) {
            // Android 11+, Window insets animation callback
            // set views padding/margin/translationY
            buttonSend.translationY = translation
            textInputLayout.translationY = translation
        } else {
            // set views padding/margin/translationY with animation or without animation
            // do ...
        }
    }

Samples

License

    Copyright (c) 2021. Weiwei

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

Releases

No releases published

Packages

No packages published