Search code examples
androidkeyboardandroid-edittextandroid-softkeyboardonfocuschangelistener

Hiding soft keyboard not working with onFocusChangeListener on Android


I want the soft keyboard to disappear when the user touches anything other than the EditText on the screen. I've gone through SO posts on this question before and implemented the use of onFocusChangeListener and it did the trick on other projects I have worked on but it doesn't seem to be working now and I can't figure out what is wrong. Any help is appreciated.

My activity.java (inputGuardian1 is an example of the EditText):

inputGuardian1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (!hasFocus) {
                InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                mgr.hideSoftInputFromWindow(inputGuardian1.getWindowToken(), 0);
            }
        }
    });

Layout xml file:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                             xmlns:app="http://schemas.android.com/apk/res-auto"
                                             android:layout_width="match_parent"
                                             android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp" >
        <!--android:layout_marginTop="?attr/actionBarSize"-->


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_guardian1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/inputGuardian1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:imeOptions="actionDone"
                android:hint="Guardian 1" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_Guardian2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/inputGuardian2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:imeOptions="actionDone"
                android:hint="Guardian 2" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout_Guardian3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/inputGuardian3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:imeOptions="actionDone"
            android:hint="Guardian 3" />
    </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_Guardian4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/inputGuardian4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:imeOptions="actionDone"
                android:hint="Guardian 4" />
        </android.support.design.widget.TextInputLayout>
        </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="40sp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_ChildName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/inputChildName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:hint="Child's Name"
                android:layout_marginTop="30sp"
                android:imeOptions="actionDone"
                />

        </android.support.design.widget.TextInputLayout>



    </LinearLayout>

        <Button android:id="@+id/buttonNext"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Next"
                android:background="@color/colorPrimary"
                android:layout_marginTop="40dp"
                android:textColor="@android:color/white" />

    </LinearLayout>
</ScrollView>


Solution

  • Setting android:clickable="true" and android:focusableInTouchMode="true" to all the containers solved the problem.