Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i know Keyboard is open

Tags:

android

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);
like image 633
Ashish srivastava Avatar asked Sep 10 '15 04:09

Ashish srivastava


1 Answers

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.

like image 159
Aditi Parikh Avatar answered Sep 21 '22 03:09

Aditi Parikh