27

I have a SearchView widget in my app, and I want to ask some questions about making it custom. First of all, you can start search only by clicking on search icon, is there any way to make whole SearchView clickable? Also, is there a way to make SearchView appear something like this when it is clicked?

a busy cat

It is now in this state:

a busy cat

Here is my code:

citySearch = (SearchView) findViewById(R.id.city_search_bar);
citySearch.setBackgroundResource(R.drawable.search_background);
citySearch.setOnSearchClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        citySearch.setIconifiedByDefault(true);
        //citySearch.setIconified(true);
    }

});
citySearch.setOnQueryTextListener(new OnQueryTextListener() {

    @Override
    public boolean onQueryTextChange(String text) {

        ((Filterable) cityListView.getAdapter()).getFilter().filter(text);

        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String text) {

        return false;
    }

});

try
{
    Field searchField = SearchView.class.getDeclaredField("mSearchButton");
    searchField.setAccessible(true);
    ImageView searchBtn = (ImageView)searchField.get(citySearch);
    searchBtn.setImageResource(R.drawable.search_icon);
    searchBtn.setScaleType(ScaleType.FIT_CENTER);
    searchPlate.setBackgroundResource(R.drawable.search_background);
}
catch (NoSuchFieldException e)
{
    Log.e("SEARCHSTYLEERROR",e.getMessage(),e);
}
catch (IllegalAccessException e)
{
    Log.e("SEARCHSTYLEERROR",e.getMessage(),e);
}
denvercoder9
  • 2,979
  • 3
  • 28
  • 41
Anhayt Ananun
  • 896
  • 1
  • 11
  • 27

5 Answers5

52

By default the SearchView is 'iconified', which is displayed as a magnifying glass icon and only if the user clicks on the icon, then the edit field expands.

To enable the user to click anywhere on the SearchView and expand the input field. We just need to add a click listener and call setIconified(false) when the user clicks.

searchView = (SearchView)findViewById(R.id.searchView);
searchView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchView.setIconified(false);
        }
    });
Eric Liu
  • 1,516
  • 13
  • 19
5

By default the SearchView is 'iconified'. So you should use this code in java:

SearchView searchView = (SearchView) findViewById(R.id.searchView);
searchView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        searchView.setIconified(false);
    }
});

or use this in xml:

<SearchView
        android:id="@+id/searchView"
        android:iconifiedByDefault="false" />

this 2 ways is correct, but different in icons display.

roghayeh hosseini
  • 676
  • 1
  • 8
  • 21
4

Another simple way to do it by setting onActionViewExpanded().

searchView = (SearchView)findViewById(R.id.searchView);
searchView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        searchView.onActionViewExpanded();
    }
});
Aashish Kumar
  • 2,771
  • 3
  • 28
  • 43
3

To allow writing when clicking in the whole component, I'd recommend you declaring iconifiedByDefault="false" on the xml.

<SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"/>
jordivilagut
  • 333
  • 2
  • 9
  • This is answer I was looking for. I just had to add `android:focusableInTouchMode="true"` to layout to get rid of initial focus on Searchview. – Petr Kramolis Apr 24 '18 at 05:49
0

You can use this anywhere in the code: searchView.setIconifiedByDefault(false);

From Android SDK:

When the SearchView is used in an ActionBar as an action view for a collapsible menu item, it needs to be set to iconified by default using setIconifiedByDefault(true). This is the default, so nothing needs to be done.

If you want the search field to always be visible, then call setIconifiedByDefault(false).

RasulOs
  • 41
  • 4