How to know if the keyboard (Keyboard) is visible?

Asked

Viewed 241 times

1

Is there any method that tells me when the keyboard appears and disappears?

  • 1

    You want to know if the keyboard is visible or how to show hide it?

2 answers

2

There is no direct method to achieve this.
What we can do is check whether the height of our layout was amended.

For this, we declare a method that will be executed when our Layouthas changed its status or visibility.

final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
        if (heightDiff > 100) { // Se são mais de 100 pixels, provávelmente é o keyboard...
            //... faça aqui o que quer quando o teclado passa a ser visível.
        }
     }
});  

Notes

1 - This code should be placed in the method onCreate of our Activity.
2 - The Layout root of Activity should have a Id assigned so that it can be referenced in the code, in this case it is activityRoot.
3 - You must add this attribute to your Activity: android:windowSoftInputMode="adjustResize"

Adapted from this response of SO AS.

1

Try this:

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

    if (imm.isAcceptingText()) {
        writeToLog("Visível");
    } else {
        writeToLog("Não visível");
    }

More details here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.