Giter Club home page Giter Club logo

keyboardheightprovider's Introduction

Keyboard Height Provider

Getting keyboard height in android it's a pain in the ass. For activities where soft input mode is adjustResize, you can set up an OnGlobalLayoutListener and measure how the activity window is resized, to make room for the keyboard.

For other modes of soft input mode, you're out of luck. As the android brick-heads refuse to solve this problem even after 28 iterations of SDK development, someone came with a cool solution:

  • create a pop-up window and observe it's global layout changes
  • set it's soft input behaviour as adjustResize
  • attach this popup window to your activity and let it report the keyboard height, based on it's layout changes.

Set up

Add the following dependency to your project:

repositories { maven { url "https://jitpack.io" } }

    implementation 'com.github.liuy97:KeyboardHeightProvider:v1.0.1'

Usage

  1. Keep a reference to KeyboardHeightProvider in your activity.

        private var keyboardHeightProvider: KeyboardHeightProvider? = null
  2. Create a KeyboardListener

        private fun getKeyboardListener() = object : KeyboardHeightProvider.KeyboardListener {
            override fun onHeightChanged(height: Int) {
                sizeText.text = "$height"
            }
        }
  3. Create the height provider after your activity has been created and register the listener.

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            keyboardHeightProvider = KeyboardHeightProvider(this)
            keyboardHeightProvider?.addKeyboardListener(getKeyboardListener())
        }
  4. Override activity lifecycle and make sure you notify the KeyboardHeightProvider

        override fun onResume() {
            super.onResume()
            keyboardHeightProvider?.onResume()
        }
    
        override fun onPause() {
            super.onPause()
            keyboardHeightProvider?.onPause()
        }

Also the KeyboardInfo object provides information about the current state of the keyboard, and it's cached height.

Credits

All credits for this project goes to Siebe Brouwer. He had a great idea of using an android PopupWindow in order to extract the keyboard height in activities where you don't want to use the adjustResize behaviour.

keyboardheightprovider's People

Contributors

crysis21 avatar mouzhik avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.