Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android search list while typing

How can I build a search bar where while I'm typing the results are shown in the ListView in which I'm searching?

For example, I have a list view with 20 strings. I press the search key and appears the bar. I want when I type 3 words or more the search starts running showing the results in the list view (as a filter: only shows the strings in the list that matching what I type)

like image 623
Gerardo Avatar asked Mar 03 '10 19:03

Gerardo


People also ask

How to add search functionality in Android?

You can add voice search functionality to your search dialog or widget by adding the android:voiceSearchMode attribute to your searchable configuration. This adds a voice search button that launches a voice prompt.

How to add a search bar in Android?

To get the Google Search bar widget back on your screen, follow the path Home Screen > Widgets > Google Search. You should then see the Google Search bar reappear on your phone's main screen.

How do I customize SearchView?

just do your own search view, it is very simple. you then can include this layout in your activity layout file. This is a simple layout which includes a "search icon" followed by EditText, followed by "clear icon". The clear icon is shown after user types some text.

How to search an item in listview Using EditText?

create some array list for the listview .. And on "AddTextChangeListener" you can search for similar items in the list and load a new arraylist for the searched text... Show activity on this post. The below code uses a custom list adapter.


2 Answers

I believe this is what you are looking for:

http://www.java2s.com/Code/Android/2D-Graphics/ShowsalistthatcanbefilteredinplacewithaSearchViewinnoniconifiedmode.htm

Have your Activity implement SearchView.OnQueryTextListener

and add the following methods:

public boolean onQueryTextChange(String newText) {     if (TextUtils.isEmpty(newText)) {         mListView.clearTextFilter();     } else {         mListView.setFilterText(newText.toString());     }     return true; }  public boolean onQueryTextSubmit(String query) {     return false; } 
like image 61
edst Avatar answered Oct 06 '22 22:10

edst


You can't do this with the search bar. But the listview has a possibility to filter on key pressed, like it is done in the contacts. The user simply starts typing and the list gets filtered then. Filtering is not really like searching. If you list contains the word foo somewhere and you type oo foo will be filtered out, but if you type fo it will stay even if the list item is call bar foo.

You simply have to enable it:

ListView lv = getListView(); lv.setTextFilterEnabled(true); 

I don't know how this is done if you don't have a hardware keyboard. I'm using the droid and starting to type starts the list to filter and to show only matching results.

like image 33
Janusz Avatar answered Oct 06 '22 23:10

Janusz