Question

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.

Was it helpful?

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

OTHER TIPS

This is easy. You need just to use Google's Search Capability such that when the user long presses the menu button, it loads a search textbox where you can input a search query to search within your app.

The searchable app file must include the element as the root node and specify one or more attributes. e.g

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_label"
android:hint="@string/search_hint" >
</searchable>

Then declare an activity to accept ACTION_SEARCH i.e

<application ... >
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>

Now we need to control the query and how it is executed. I use this for my app.

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);

// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top