androidandroid-virtual-keyboard

Virtual keyboard status


Is there some way to know that virtual keyboard is made hidden by user during run time. Before tagging me duplicate question, first understand my question because it seems like duplicate and I also found lots of question with related topic. But not found any answer.

My problem is during some event to occur like say orientation change, I need to know whether user has minimized the keyboard or not.So that I take action accordingly. I tried to be very specific to question even any explanation feel free to ask. Any help will appreciated .Thanks in advance My Try

final View activityRootView = findViewById(R.id.ll_main_root);
                activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
                        if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                            Toast.makeText(MainActivity.instance, "KeyBoarad" + value, Toast.LENGTH_LONG).show();
                        }
                     }
                });

Solution

  • Hey It may be late but just for the query and my suggestion. I was facing the same issue somewhat. Actually in my case while orientation change my keyboard was getting minimized even if it was visible before orientation change. So I used android:windowSoftInputMode="stateUnchanged" so that my keyboard remains visible even on orientation change.

    And there may be one more issue that u want to change the visiblity may be so u can use toggleSoftInputFromWindow (IBinder windowToken, int showFlags, int hideFlags).

    Specifically

    InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputMgr.toggleSoftInput(0, 0);

    Hope it help.