6

How can I get to know keyboard is open.actually my problem is that if keyboard is open then only I call for hide,not for always call hide. is there any method to check if keyboard is open ? currently I am using this method to hide keyboard.

EditText myEditText = (EditText) findViewById(R.id.myEditText);
View view = this.getCurrentFocus();
if (view != null) {ditText) findViewById(R.id.myEditText);
View view = this.getCurrentFocus();
if (view != null) { Inp` 
im.hideSoftInputFromWindow(view.getWindowToken(), 0);
Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34
  • 2
    have you check http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android? – Rajesh Jadav Sep 10 '15 at 05:00

1 Answers1

1

Try GlobalLayout Listener like:

    main_layout.getViewTreeObserver().addOnGlobalLayoutListener(
        new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() 
                {

                    Rect r = new Rect();
                    // r will be populated with the coordinates of your view
                    // that area still visible.
                    main_layout.getWindowVisibleDisplayFrame(r);

                    int heightDiff = main_layout.getRootView().getHeight()-(r.bottom -r.top);    
                   //if(hightDiff>100) --> It may be keyboard.

                }
});

Replace main_layout with your layout.

Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34