Search code examples
androidandroid-searchmanager

Customization of the Android SearchView component


I need a component that allows the user to enter a value and checks if it exists by connecting to a certain site on the Internet, and also shows suggestions while the user is typing. I would like to use the SearchView component for that, but found out it has some limitations:

  1. The developer has to create a separate activity that handles the search. I don't want to use a different activity, I just want to connect to the site and check if what the user typed exists.

  2. In order to use the suggestions mechanism, the developer has to build a content provider. Again, I don't want to connect to any DB, I just need to send a search query to the site using standard HTTP messages, and for that I don't need a content provider.

So my question is: Is there a way to use the SearchView component with the customization I need, or do I have to find ugly workarounds that abide by the requirements of the component, but also do what I need?

Thanks.


Solution

  • You shouldn't run HTTP request on your UI thread as you can't guarantee they'll finish in a timely manner so they might lock up. You'll need to run that in a runnable or ASyncTask at the very lest.

    If you want a quick dirty hack way of doing it, you could create a ListFragment to display your custom suggestions. Then create an OnQueryTextListener (or have your Activity implement it) set it on the SearchView. Then in the Listener's method onQueryTextChange(String text) generate the suggestions (if getting from the webservice have this method kick off your ASyncTask) and then update the ListFragment Adapter with the data