Is there a method that tells me when the keyboard appears and disappears?
Is there a method that tells me when the keyboard appears and disappears?
There is no direct method to achieve this.
What we can do is check if the height of our layout
has changed.
For this, declare a method that will be executed when our Layout
has its state or visibility changed.
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
onCreate
method of ourActivity
. 2 - TheLayout root
ofActivity
must have aId
assigned so that it can be referenced in the code, in this case it isactivityRoot
. 3 - You should add this attribute to yourActivity
:android:windowSoftInputMode="adjustResize"
Adapted from this answer from SO.com
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 .