Rectangle 27 0

How do I Detect if Software Keyboard is Visible on Android Device?


rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = rootView.getRootView().getHeight() - rootView.getHeight();

                if (heightDiff > 100) { 
                    Log.e("MyActivity", "keyboard opened");
                } else { 
                    Log.e("MyActivity", "keyboard closed");
                }
            }
        });
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/addresses_confirm_root_view"
                android:background="@color/WHITE_CLR">
RelativeLayout rootView = (RelativeLayout) findViewById(R.id.addresses_confirm_root_view);
getViewTreeObserver()

@Karoly basically, he's comparing the window size with your activity's root view size. the appearance of the soft keyboard doesn't affect the size of the main window. so you can still lower the value of 100.

@Karoly i think this may be and 1. No matter. Only this must be less than the real length of keyboard

It's not work on Dialog Fragment

hey mate, could you please tell me where this magic 100 comes from? Why not 101 or 99? Thanks

rootView is just a view pointing to my root view in this case a relative layout:

Note
Rectangle 27 0

How do I Detect if Software Keyboard is Visible on Android Device?


KeyboardUtils.addKeyboardToggleListener(this, new KeyboardUtils.SoftKeyboardToggleListener()
{
    @Override
    public void onToggleSoftKeyboard(boolean isVisible)
    {
        Log.d("keyboard", "keyboard visible: "+isVisible);
    }
});

@MaulikDodia I checked and it works fine in fragments. Set it up like this: KeyboardUtils.addKeyboardToggleListener(getActivity(), this); and it should work. What device are you trying it on?

I created a simple class that can be used for this: https://github.com/ravindu1024/android-keyboardlistener. Just copy it in to your project and use as follows:

It's not working. It works only once when onCreateView method gets call in Fragment. I have one EditText, on click of that EdiText Keyboard is opening but ABOVE method is not getting call.@ravindu1024

Well, you can put it anywhere inside your activity. Just put it in the onCreate() method after the setContentView() call and you should be getting callbacks. Btw, what device are you trying it on?

Where in the code exaclty do I have to put this? I put this into an activity, however, it does not detect any keyboard appearance or disappearance.

Note
Rectangle 27 0

How do I Detect if Software Keyboard is Visible on Android Device?


You can use the callback result of showSoftInput() and hideSoftInput() to check for the status of the keyboard. Full details and example code at

Note
Rectangle 27 0

How do I Detect if Software Keyboard is Visible on Android Device?


private final String TAG = "TextEditor";
private TextView mTextEditor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_editor);
    mTextEditor = (TextView) findViewById(R.id.text_editor);
    mTextEditor.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            isKeyboardShown(mTextEditor.getRootView());
        }
    });
}

private boolean isKeyboardShown(View rootView) {
    /* 128dp = 32dp * 4, minimum button height 32dp and generic 4 rows soft keyboard */
    final int SOFT_KEYBOARD_HEIGHT_DP_THRESHOLD = 128;

    Rect r = new Rect();
    rootView.getWindowVisibleDisplayFrame(r);
    DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
    /* heightDiff = rootView height - status bar height (r.top) - visible frame height (r.bottom - r.top) */
    int heightDiff = rootView.getBottom() - r.bottom;
    /* Threshold size: dp to pixels, multiply with display density */
    boolean isKeyboardShown = heightDiff > SOFT_KEYBOARD_HEIGHT_DP_THRESHOLD * dm.density;

    Log.d(TAG, "isKeyboardShown ? " + isKeyboardShown + ", heightDiff:" + heightDiff + ", density:" + dm.density
            + "root view height:" + rootView.getHeight() + ", rect:" + r);

    return isKeyboardShown;
}

Try this code it's really working if KeyboardShown is Shown then this function return true value....

Note
Rectangle 27 0

How do I Detect if Software Keyboard is Visible on Android Device?


adb shell dumpsys window InputMethod | grep "mHasSurface"

This is only useful during development - not a solution for use in an app. (Users won't have adb running.)

Note
Rectangle 27 0

How do I Detect if Software Keyboard is Visible on Android Device?


contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

    Rect r = new Rect();
    contentView.getWindowVisibleDisplayFrame(r);
    int screenHeight = contentView.getRootView().getHeight();

    // r.bottom is the position above soft keypad or device button.
    // if keypad is shown, the r.bottom is smaller than that before.
    int keypadHeight = screenHeight - r.bottom;

    Log.d(TAG, "keypadHeight = " + keypadHeight);

    if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
        // keyboard is opened
    }
    else {
        // keyboard is closed
    }
}
});

for all versions

@Code-Apprentice In the activity/fragment you're looking to respond to soft keyboard changes. ContentView is the root view of the layout of this activity/fragment.

The only thing that worked on Android 6. Thanks.

Note
Rectangle 27 0

How do I Detect if Software Keyboard is Visible on Android Device?


<View
android:layout_width="1dp"
android:layout_height="1dp"
android:focusable="true"
android:focusableInTouchMode="true"/>

In my case i had only one EditText to manage in my layout so i came up whit this solution. It works well, basically it is a custom EditText which listens for focus and sends a local broadcast if the focus changes or if the back/done button is pressed. To work you need to place a dummy View in your layout with android:focusable="true" and android:focusableInTouchMode="true" because when you call clearFocus() the focus will be reassigned to the first focusable view. Example of dummy view:

The solution which detects the difference in layout changes doesn't work very well because it strongly depends on screen density, since 100px can be a lot in a certain device and nothing in some others you could get false positives. Also different vendors have different keyboards.

Note
Rectangle 27 0

How do I Detect if Software Keyboard is Visible on Android Device?


InputMethodManager imm = (InputMethodManager) getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    if (imm.isAcceptingText()) {
        writeToLog("Software Keyboard was shown");
    } else {
        writeToLog("Software Keyboard was not shown");
    }

It is pathetic that the Android framework is lacking, and worse, inconsistent in this regard. This ought to be super-simple.

This does not work for me. The keyboard shown branch triggers even in instance when the keyboard was never shown or was shown and then closed.

Note
Rectangle 27 0

How do I Detect if Software Keyboard is Visible on Android Device?


contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

    Rect r = new Rect();
    contentView.getWindowVisibleDisplayFrame(r);
    int screenHeight = contentView.getRootView().getHeight();

    // r.bottom is the position above soft keypad or device button.
    // if keypad is shown, the r.bottom is smaller than that before.
    int keypadHeight = screenHeight - r.bottom;

    Log.d(TAG, "keypadHeight = " + keypadHeight);

    if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
        // keyboard is opened
    }
    else {
        // keyboard is closed
    }
}
});

for all versions

@Code-Apprentice In the activity/fragment you're looking to respond to soft keyboard changes. ContentView is the root view of the layout of this activity/fragment.

The only thing that worked on Android 6. Thanks.

Note
Rectangle 27 0

How do I Detect if Software Keyboard is Visible on Android Device?


<View
android:layout_width="1dp"
android:layout_height="1dp"
android:focusable="true"
android:focusableInTouchMode="true"/>

In my case i had only one EditText to manage in my layout so i came up whit this solution. It works well, basically it is a custom EditText which listens for focus and sends a local broadcast if the focus changes or if the back/done button is pressed. To work you need to place a dummy View in your layout with android:focusable="true" and android:focusableInTouchMode="true" because when you call clearFocus() the focus will be reassigned to the first focusable view. Example of dummy view:

The solution which detects the difference in layout changes doesn't work very well because it strongly depends on screen density, since 100px can be a lot in a certain device and nothing in some others you could get false positives. Also different vendors have different keyboards.

Note