1
Is there any method that tells me when the keyboard appears and disappears?
1
Is there any method that tells me when the keyboard appears and disappears?
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 Layout
has 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 ourActivity
.
2 - TheLayout root
ofActivity
should have aId
assigned so that it can be referenced in the code, in this case it isactivityRoot
.
3 - You must add this attribute to yourActivity
: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 java android
You are not signed in. Login or sign up in order to post.
You want to know if the keyboard is visible or how to show hide it?
– Lucas Lima