0

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.

user940016
  • 2,878
  • 7
  • 35
  • 56

2 Answers2

0

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);
}
}

mutiemule
  • 2,319
  • 2
  • 28
  • 34
  • I think you didn't understand my question... I know how to use the search widget in the standard way, but I don't want to have a searchable activity... – user940016 Aug 24 '12 at 16:27
0

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

bennettaur
  • 1,243
  • 10
  • 8
  • Ok, but if I use ListFragment, then I won't have to use SearchView at all. I can just use EditText and OnKeyListener to get the same effect. The question is if there's a way to hook into SearchView and use its suggestions mechanism and UI, but in a customized way that gets the suggestions from any source I want, not necessarily a content provider. – user940016 Aug 24 '12 at 16:33
  • Just make your own ContentProvider that returns data from where ever you want (not necessarily a DB). That or subclass a SearchView and re-write the appropriate methods to accept data from where you want and display it appropriately (you'll probably have to look at the source to see how this is done already) – bennettaur Aug 24 '12 at 16:42
  • Ok, but then what about the searchable activity? I don't want to use it, but I must define it. Can I build a dummy activity that is not really displayed? – user940016 Aug 24 '12 at 18:08
  • So you want to use a SearchView but are trying to jump through hoops to avoid setting up all of the components it needs? Seems like you don't want to actually use a SearchView. Why not use an AutoCompleteTextView instead? You can set the adapter to your custom suggestions – bennettaur Aug 24 '12 at 18:33