0

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);

    }
}
Jack
  • 693
  • 1
  • 7
  • 25

3 Answers3

0

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);
Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26
  • Thanks, it delivers the same result and worst, where I want SearchView to be focused when the activity starts (but without showing the keyboard), which is android:iconifiedByDefault="false" fulfills – Jack Feb 13 '18 at 14:03
  • we usually set `searchView` to the toolbar as the menue_options , and have more control over showing and hiding of it. – Abdul Kawee Feb 13 '18 at 14:14
  • Thanks, but I don't want to hide the keyboard manually, I just want to force it to loose focus - to avoid reopening the keyboard - when the user close it using back key – Jack Feb 13 '18 at 14:25
  • i would suggest you use it as the options_menu in the toolbar , you will have the exact output, i am using it in the same way. – Abdul Kawee Feb 13 '18 at 14:27
  • Thanks, but per design it's not possible to have it in the toolbar, as there's no toolbar, it's a full screen activity. – Jack Feb 13 '18 at 14:28
  • Thanks but onBackPressed() doesn't get called for the first time - when the keyboard gets disappeared, it gets called when I press the back button again after it's hidden – Jack Feb 13 '18 at 15:05
0

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.

ddassa
  • 309
  • 1
  • 6
0

try this way

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

    override fun dispatchKeyEventPreIme(event: KeyEvent?): Boolean {
       return false
    }
}
Sam
  • 6,215
  • 9
  • 71
  • 90