Jack
Jack

Reputation: 743

How to detect when virtual keyboar of a SearchView is dismissed

I wanna call mSearchView.clearFocus() when virtual keyboard is dismissed, how to do that?

My problem is once the SearchView gets focused, it keeps focused, so if I dismissed the virtual keyboard using back button, and I opened an AlertDialog - for example - the virtual keyboard pops up again once I close the AlertDialog as the search view still has the focus, as if it regains focus.

for the SearchView I used:

    android:iconifiedByDefault="false"
    android:focusable="false"

for the activity holds the SearchView I use:

android:windowSoftInputMode="stateUnspecified|adjustPan"

even if I changed it to the following I get the same problem

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

Eidt 1:

changing

    android:iconifiedByDefault="false"

to be

    android:iconifiedByDefault="true"

doesn't solve the problem, I get the same result.

Edit 2:

I tried the approach of creating a custom SearchView and to override onKeyPreIme and call clearFocus(), but onKeyPreIme doesn't get called.

public class ModifiedSearchView extends SearchView {
    public ModifiedSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onKeyPreIme (int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            clearFocus();
            return false;
        }

        return super.dispatchKeyEvent(event);

    }
}

Upvotes: 0

Views: 521

Answers (3)

Sam
Sam

Reputation: 6405

try this way

internal class ProductSearchView(context: Context, attrs: AttributeSet) : SearchView(context, attrs) {

    override fun dispatchKeyEventPreIme(event: KeyEvent?): Boolean {
       return false
    }
}

Upvotes: 0

ddassa
ddassa

Reputation: 309

I have tried adding a searchview to a linerlayout and i do not have the same problem like you. But if you want to track virtual keyboard hide event use the following code in onCreate()

mLLWrapper.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                mLLWrapper.getWindowVisibleDisplayFrame(r);

                int heightDiff = mLLWrapper.getRootView().getHeight() - (r.bottom - r.top);
                if (heightDiff > 300) { // if more than 100 pixels, its probably
                    // keyboard visible
                } else {
                    // keyboard in not visible
                }
            }
        });

mLLWrapper is root LinearLayout view of activity

Once the keyboard is dismissed call clear focus. That might help. If not update your question with more code which will be easy for us to help you.

Upvotes: 0

Abdul Kawee
Abdul Kawee

Reputation: 2727

To hide keyboard use this

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

    <SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"/>

</LinearLayout>

and then in you onBackPressed()

if (searchView != null) {
searchView.setQuery("", false);
searchView.clearFocus();
rootView.requestFocus();

}

while rootView is

rootView = findViewById(R.id.root_layout);

Upvotes: 0

Related Questions