Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView setIconified(false) automatically calls the focus for the searchview. How can I disable this?

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

like image 539
rosu alin Avatar asked Jul 31 '15 14:07

rosu alin


People also ask

What is setOnQueryTextListener?

Android SearchView setOnQueryTextListener(OnQueryTextListener listener) Sets a listener for user actions within the SearchView.

What is a search view?

Android SearchView provides user interface to search query submitted over search provider. SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class.

How do I increase search view on Android?

To make the SearchView expanded by default, call setIconifiedByDefault(false) on it when you initialise it (e.g. in onCreateOptionsMenu(..) or onPrepareOptionsMenu(..) ). I've found in most cases this will give it focus automatically, but if not simply call requestFocus() on it too.

How do I set search view?

Add the Search View to the App Bar To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.


3 Answers

Try this:

searchView.setFocusable(false);
searchView.setIconified(false);
searchView.clearFocus();
like image 161
Lakhan Sharma Avatar answered Sep 27 '22 21:09

Lakhan Sharma


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

like image 21
Mohamed Niyaz Avatar answered Sep 27 '22 23:09

Mohamed Niyaz


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

like image 27
Nikhil Avatar answered Sep 27 '22 21:09

Nikhil