Question

I want to hide the cursor and do some other stuff as soon as the keyboard is minimized. I already enable the cursor and show the keyboard when the editext is touched, but I can't find a way to know when a user minimizes it without pressing 'Done'.

Is there a way to do that?

Was it helpful?

Solution

try below code:-

activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    activityRootView.getWindowVisibleDisplayFrame(r);

                    int heightDiff = activityRootView.getRootView()
                            .getHeight() - (r.bottom - r.top);
                    if (lastDiff == heightDiff)
                        return;
                    lastDiff = heightDiff;
                    Log.i("aerfin","arefin "+lastDiff);
                    if (heightDiff > 100) { // if more than 100 pixels, its
                                            // probably a keyboard...
                        flag2 = 0;
                    } else {
                        if (flag == false)
                            flag2 = 1;
                    }
                }
            });

more info see below link least use full for me:-

Maintain keyboard open/closed state for EditText when app comes to foreground

How to check visibility of software keyboard in Android?

OTHER TIPS

In your manifest, make sure the configuration changes when the keyboard shows or hides by putting this line as argument in your <activity> tag in your manifest:

android:configChanges="orientation|keyboardHidden"

Then override this method in your Activity.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks whether a hardware keyboard is available
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
    }
}
InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top