6

I have the following code:

  if(mSearchView != null){
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setIconified(false);
        mSearchView.setOnQueryTextListener(this);
        int searchPlateId = mSearchView.getContext().getResources()
                .getIdentifier("android:id/search_plate", null, null);
        View searchPlateView = mSearchView.findViewById(searchPlateId);
        if (searchPlateView != null) {
            searchPlateView.setBackgroundColor(getResources().getColor(R.color.white));
        }
    }

The problem is that the moment I setIconified(false) on the serachview, the keyboard pops up, and I do not want this to happen. Is it possible to prevent this somehow? PS: I Have this in the manifest:

 android:windowSoftInputMode="adjustNothing|stateHidden"

Also do it programmatically in onCreate but no luck

rosu alin
  • 5,674
  • 11
  • 69
  • 150
  • does `mSearchView.clearFocus()` work for you? – Gennadii Saprykin Jul 31 '15 at 15:52
  • nope, also tried listview.requestFocus() The issue is, even if I say clearFocus, the moment I set setIconified(false) it calls the keyboard. Now I can clear focus after. and it works on new modern phones. But on slow phones, the keyboard will appear, and then dismiss (Even on new phones, testing on a LG G3, even if the keyboard doesn't appear, the icon still appears and disappears on the status bar – rosu alin Jul 31 '15 at 16:14
  • the code above, are you calling this in activity's `onCreate`? – Gennadii Saprykin Jul 31 '15 at 16:26
  • yes, I have an "init()" function inside onCreate, and it calls this code – rosu alin Aug 03 '15 at 08:27

6 Answers6

12

Try this:

searchView.setFocusable(false);
searchView.setIconified(false);
searchView.clearFocus();
Lakhan Sharma
  • 1,741
  • 1
  • 12
  • 19
2

Programmatically remove all the fields like

searchView.setFocusable(false);
searchView.setIconified(false);
searchView.clearFocus();

and set through xml attributes for serach view:

<!-- Dummy item to prevent Search view from receiving focus -->

    <LinearLayout
        android:focusable="true" 
        android:focusableInTouchMode="true"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->

    <android.support.v7.widget.SearchView android:id="@+id/serach_view"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:nextFocusUp="@id/serach_view" 
        android:nextFocusLeft="@id/serach_view"/>

It will work.

check this link for reference enter link description here

Mohamed Niyaz
  • 228
  • 1
  • 9
1

I was also having the same issue . I spend lots of hours for this issue and finally i created a dummy EditText which was hidden in xml and requested focus for the edit text in java code

    mSearchView.setFocusable(false);
    mSearchView.setIconified(false);
    mSearchView.clearFocus();
    EditText editText = (EditText) findViewById(R.id.dummy);
    editText.requestFocus();

hope it will helpful for someone

Nikhil
  • 911
  • 15
  • 28
0

I used instead a edittext, and made the functionality of the search icon and close icon myself. This way, I do not have to set "setIconifiedByDefault" being a edittext. and it resolves all my issues

rosu alin
  • 5,674
  • 11
  • 69
  • 150
0
    searchView.setFocusable(false);
    searchView.setIconified(false);
    searchView.clearFocus();
Fanatic_L
  • 1
  • 1
0

Just make Search View Iconified to true.

mSearchView.setIconified(true);
Kamal Panara
  • 482
  • 7
  • 16