1

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?

yedidyak
  • 1,964
  • 13
  • 26

3 Answers3

0

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?

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
  • Thanks! I ended up using a similar version to this, taken from one of the later answers on your second link. It's a little unwieldly, as the EditText I need to set is buried deep in a layout structure, so I'll have to pass an event down a ladder, but it will work. – yedidyak Apr 28 '14 at 13:12
0

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();
    }
}
Mark Buikema
  • 2,483
  • 30
  • 53
-1
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);
  • 1
    That is not what he asked. Your code shows or hides the keyboard, he wants to get a callback when the user shows or hides the keyboard manually. – Mark Buikema Apr 28 '14 at 10:36