Getting Keyboard events in Android

Currently in Android (as of Android 7.1.2), there is no out of the box option to get keyboard events. When we came across a scenario where this was needed, we had to come up with our own solution.

Our Problem

Our design included a main view with a ScrollView inside and some buttons below the scroll view.

This ScrollView contained a textbox for user editing. On entering text, the soft keyboard would open with the buttons below the scrollview being visible on top of the keyboard. This was taking up precious screen space.

Android_Keyboard _EventTo tackle this issue, we decided to hide the button when the Soft keyboard was shown. Unfortunately there was no easy way to find if softKeyboard is shown or hidden.

The Solution

After searching multiple sources, we found one particular method that we could modify to fit for our use case.

We decided to use the addOnGlobalLayoutListener to the root view and monitor for changes in root view’s height has shrunk from its original height meaning that the soft keyboard was opened.

If the height of the app has reduced from its maximum height then the keyboard must have been opened else the keyboard is hidden.

Make sure there are no intensive tasks onKeyboardVisibilityChanged method as it can be called multiple times.



static int mAppHeight;

static int currentOrientation = -1;


public static void setKeyboardVisibilityListener(final Activity activity, final  KeyboardVisibilityListener keyboardVisibilityListener) {

final View contentView = activity.findViewById(;

contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

private int mPreviousHeight;


public void onGlobalLayout() {

int newHeight = contentView.getHeight();

if (newHeight == mPreviousHeight)


mPreviousHeight = newHeight;

if (activity.getResources().getConfiguration().orientation != currentOrientation) {

currentOrientation = activity.getResources().getConfiguration().orientation;

mAppHeight =0;


if (newHeight >= mAppHeight) {

mAppHeight = newHeight;


if (newHeight != 0) {

if (mAppHeight > newHeight) {

// Height decreased: keyboard was shown


} else


// Height increased: keyboard was hidden








public interface KeyboardVisibilityListener {

void onKeyboardVisibilityChanged(boolean keyboardVisible);


//to call from the view controller set keyboard listener setKeyboardVisibilityListener with the activity and listener parameter. Utils is the class name where we have written our code.

Utils.setKeyboardVisibilityListener(activity, this);

//Override the onKeyboardVisibilityChanged method to perform your changes


public void onKeyboardVisibilityChanged(boolean keyboardVisible) {

if (keyboardVisible) {

//hide the button if keyboard is shown


}else {

//show the button if keyboard is hidden




Limitations of the solution

This solution uses global layout listener which means this code block will be called whenever the screen is updated. Hence make sure you don’t do any resource intensive task in this block.


Modified code from following answers

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>