Informatheus
Informatheus

Reputation: 1025

Detect if keyboard is shown in android with navigation drawer

I've already tried this solution but onGlobalLayout() never gets called. Yes, I've already set android:windowSoftInputMode="adjustResize" in manifest.

Maybe it is because I'm using a DrawerLayout as my activity root layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- The main content view -->

<LinearLayout
    android:id="@+id/main_fragment_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" />

<!-- The navigation drawer -->

<LinearLayout
    android:id="@+id/user_fragment_layout"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:orientation="vertical" >
</LinearLayout>

</android.support.v4.widget.DrawerLayout>

I've tested activity_drawer_layout and main_fragment_layout in findViewById() but unsuccessfully; onGlobalLayout() never gets called. Please help me. Thanks in advance.

Upvotes: 1

Views: 892

Answers (1)

Antrromet
Antrromet

Reputation: 15424

I'm not really sure as to what are you trying to achieve by identifying whether your keyboard is visible or not, but if you only want to hide the keyboard when the navigation drawer is opened, I suggest you use the following code

private void hideKeyboard() {   
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

Also did you take a look at How to check visibility of software keyboard in Android?

Upvotes: 2

Related Questions