I have a login form with edittexts and a login button(connect). When a user click the edittexts the keyboard opens, and when they click the login button(connect) they keyboard disappears. The problem is if a user clicks on a edittext after clicking the login button, the keyboard shows for a second, then dissapears. Using rootview to calculate if the keyboard is showing or not, since that seems to be the easiest way according to stackoverflow. How do i make it so the keyboard can be shown after clicking the button?
Onclicklistener
boolean clicked=false;
connect.setOnClickListener(v -> {
clicked=true;
keyboard();
});
keyboard method
void keyboard(){
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
final View activityRootView = findViewById(R.id.wrap);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
if (clicked) {
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 0.25 * activityRootView.getRootView().getHeight()) {
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
});
}