Skip to content

We all know how hard it is to manage keyboard changes on Android. Sometimes we need to do something when the keyboard is open or closed. How do we do it? There is no easy way. This library is designed to make it easier to monitor keyboard changes.

License

Notifications You must be signed in to change notification settings

MayconCardoso/KeyboardVisibilityMonitor

Repository files navigation

Keyboard Visibility Monitor

We all know how hard it is to manage keyboard changes on Android. Sometimes we need to do something when the keyboard is open or closed. How do we do it? There is no easy way.

This library is designed to make it easier to monitor keyboard changes. So, in order to keep things compatible with Java and Kotlin the library has been split into two: core and ktx.

Download

implementation 'com.mctech.library.keyboard:visibilitymonitor:1.0.6'
implementation 'com.mctech.library.keyboard:visibilitymonitor-ktx:1.0.6'

Show me the code :)

All you have to do is create an instance of KeyboardVisibilityMonitor which is a Lifecycle-Aware Component inside an activity or fragment.

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    KeyboardVisibilityMonitor( lifecycleOwner = this, activity = this){ change ->
        // Your code here
    }
}

It will deliver you an instance of KeyboardChange every time the keyboard state changes.

data class KeyboardChange(
    val isOpened: Boolean,
    val currentKeyboardHeight: Int
)

Custom Settings

You can change the keyboard notifier policy by creating an instance of KeyboardObservableSettings

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    val settings = KeyboardObservableSettings(
        notifyWhenScreenHasOpenedAtFirstTime = false,
        notifyOnlyWhenStateChange = true
    )

    KeyboardVisibilityMonitor( 
        lifecycleOwner = this, 
        activity = this, 
        keyboardObservableSettings = settings
    ){ change ->
        // Your code here
    }
}

Kotlin extentions

Of course, the code above could be better and without that all boilerplate. Just make sure you have implemented the 'com.mctech.library.keyboard:visibilitymonitor-ktx:1.0.6' on your build.gradle dependencies block. For more information about all available extentions click here

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    observeKeyboardChanges { change ->
        // Your code here
    }
    
    // Or you can call the same method with the settings mentioned above.
    val settings = KeyboardObservableSettings(
        notifyWhenScreenHasOpenedAtFirstTime = false,
        notifyOnlyWhenStateChange = true
    )

    observeKeyboardChanges(settings) { change ->
        // Your code here
    }
}

If you want to observe one specific event changes, use one of these methods below

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    observeKeyboardWhenClosed {

    }

    observeKeyboardWhenOpened {

    }
}

Thank you

If this library has helped you, please give me that friendly star :)

About

We all know how hard it is to manage keyboard changes on Android. Sometimes we need to do something when the keyboard is open or closed. How do we do it? There is no easy way. This library is designed to make it easier to monitor keyboard changes.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages