Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom SearchView whole clickable in android

Tags:

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); } 
like image 249
Anhayt Ananun Avatar asked Jul 16 '13 07:07

Anhayt Ananun


People also ask

How to use SearchView in android?

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. For making search field visible, SearchView uses setIconifiedByDefault(false) method.

How to add SearchView in ToolBar in android?

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.


2 Answers

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);         }     }); 
like image 157
Eric Liu Avatar answered Sep 19 '22 00:09

Eric Liu


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.

like image 21
roghayeh hosseini Avatar answered Sep 19 '22 00:09

roghayeh hosseini