Search code examples
androidandroid-activitykeyboardsearchviewlisteners

Get keyboard to disappear when clicking outside of searchview


I'm currently stuck on trying to make the keyboard disappear when I click outside of the searchview tab.

My issue is that I tried to attach a onfocus change listener to the topmost layout to clear focus from the searchview when clicked outside, and while it works, the listener only works up until slightly below the progress bar. I can't get the listner to cover the whole screen. Help? I've tried to get the top most layout's parent and that hasn't worked.

Here is a picture of my screen showing up until where the listener works too: https://drive.google.com/file/d/0B7Dd6tnM1aneaUVZNTI1NXQydDA/view?usp=sharing

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/session_linear_layout"
    android:orientation="vertical" >


    <SearchView
        android:id="@+id/Session_searchbar"
        android:layout_width="match_parent"
        android:layout_height="47dp"
        android:focusable="true"
        android:focusableInTouchMode="true" />

    <ProgressBar
        android:id="@+id/session_progress_spinner"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal" >
    </ProgressBar>

</LinearLayout>

Solution

  • Try the following code

    InputMethodManager inputManager = 
            (InputMethodManager) context.
                getSystemService(Context.INPUT_METHOD_SERVICE); 
    inputManager.hideSoftInputFromWindow(
            searchView.getWindowToken(),
            InputMethodManager.HIDE_NOT_ALWAYS);