Search in android activity - android

I am new in android and I am working with a search related feature. After read the tutorial I have some questions:
1 Does the SearchableActivity unique in an application?
It seems that 3 steps are required to setup the search feature(from the android developer guide):
1)create the SearchableActivity to receive the search keyword.
2)make the SearchableActivity to accept the ACTION_SEARCH intent
3)specify the searchable configuration to use, in a element.
Then I wonder if all the search related job will be delegated to the SearchableActivity?
And suppose I have two MainActivity and MainActivityTwo, both of them make a SearchView inside the action bar.
The AndroidMinefset.xml:
<activity android:name="com.app.activity.MainActivityTwo" />
<activity android:name="com.app.activity.MainActivity" />
<activity android:name="com.app.activity.SearchableActivity"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
xml/search.xml:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/app_name" >
</searchable>
And register the search configuration(serchableInfo) toSerchView:
.MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.u_action_menu_map, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
This does not work, it seems that there is no serchableInfo bound with the current Activity(MainActivity).
Even I changed to this:
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName("com.app.activity", "SearchableActivity")));
It still cannot find the searchableInfo.
After I add the search related configuration to MainActivity:
<activity android:name="com.app.activity.MainActivityTwo" />
<activity android:name="com.app.activity.SearchableActivity" />
<activity android:name="com.app.activity.MainActivity"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
This worked. WTF.
I always thought that the Activity provide the Search UI and the Activity handle the input are different, but after my test it seems that they are the same Activity, that't to say the user input are submit to the Activity itself,but why use the **ing intent to transfer the input data?
2 The search submit
The Search dialog have a search submit button at the right of the dialog:
But there is nothing inside the SearchView so how to submit the search in a SearchView:

There should be a search key on your keyboard. It can also be go, return or similar keys that should start the search.

Related

SearchView in one activity, results in another one

My use case is the following: in activity A, I have an action bar with a collapsible SearchView. When the user gives her query and presses the 'search' button, I would like to show activity B with the results. I'm failing to do so, here is my code:
searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/action_search_hint"
android:label="#string/app_name"/>
Activity A:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
AndroidManifest for Activity A and B:
<activity
android:name=".ActivityA">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ActivityB">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
Howerver, no matter what I try, getSearchableInfo in A always returns null.
I have the feeling I'm not understanding something. The only way I can make getSearchableInfo() return not null is when I put the SEARCH intent filter and the searchable meta-data into activity A in the manifest. But then, when I press the 'search' button, another instance of activity A is started again on top of the current one, which definitely is not what I want. I guess I could add singleTop to A and handle the SEARCH action in onNewIntent and start B, but I generally don't want A to be singleTop, I want the default. Or do I?
I tried adding this to A in the manifest (as suggested in some other threads):
<meta-data
android:name="android.app.default_searchable"
android:value=".ActivityB" >
</meta-data>
What am I doing wrong?
EDIT: I replaced the 'getComponentName()' line in A with the following:
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, ActivityB.class)));
and it seems to work the way I want! Am I doing something wrong here, or is this the correct way? I'm a bit uncertain because it is not mentioned anywhere!
According to Android documentations, SearchManager.getSearchableInfo(componentName) takes one argument, which is:
componentName: The activity to get searchable information for
In your case you need ActivityB, so constructing a ComponentName that points to ActivityB is the correct way
new ComponentName(this, ActivityB.class)
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
add this intent filter to activity A.
2.SearchView Menu
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:showAsAction="collapseActionView|always"
android:title="#string/search_hint"/>
</menu>
3.Call this method from oncreate.
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Debug.e("Suggestion handle click", "call " + query);
startNextActivity(query);
}
}
4. if you are using Autosuggestion Listview implement searchView.setOnQueryTextListener ,setOnSuggestionListener and setSuggestionsAdapter.

searchSuggestThreshold attribute doesn't work : ContentProvider triggered on every typed character

I have a search widget within the action bar of my main activity. I would like to display suggestions after the user type at least 2 characters.
I'm aware this can be done by setting android:searchSuggestThreshold="2", but it doesn't seem to work because the SuggestionContentProvider is called when the user types zero or many characters.
Even when the user clicks on the search icon (0 characters) to expand the widget, the ContentProvider is called.
Here is my searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint"
android:searchSettingsDescription="#string/search_settings"
android:searchSuggestAuthority="com.studiofrenetic.rmfb.Services.SearchContentProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestThreshold="2">
</searchable>
Only one searchable activity in my Manifest.xml:
<activity
android:name="com.studiofrenetic.rmfb.MainActivity"
android:screenOrientation="portrait"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Code to set SearchableInfo in onCreateOptionsMenu:
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setOnQueryTextListener(this);
// Assumes current activity is the searchable activity
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.setIconifiedByDefault(true);
I use ActionBarCompat (with minSdkVersion 14 and targetSdkVersion 19).
I search across the web and nobody seems to have this strange behavior... Please help me to understand the problem.
Thanks in advance :)
Finally I had a misunderstanding concerning the behavior related to searchSuggestThreshold:
It's normal that SuggestionContentProvider is called on every typed character, but the uri parameter doesn't contain the query until the threshold is reached.
So, the code related to suggestions generation should be skipped.

Android Searchable Configuration not getting attached to SearchView

I'm currently working on adding search ability to my app but I'm facing some problems. First of all I'd just like to clear out that I don't want to create a search activity, I just want to use the Action Bar SearchView and press on suggestions to perform the search.
I've created a SearchView which expands correctly although it doesn't seem like it can attach to my Searchable Configuration xml object. I added this line of code:
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
Yet my SearchView doesn't seem to be affected by it. The same goes for this line of code:
android:hint="Search my stuff"
Again, no effect.
Here's my activity in my AndroidManifest.xml:
<activity
android:name="com.simon.holocountownapp.ItemListActivity"
android:label="Holo Countdown" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Here's my setup of the SearchView in my onCreateOptionsMenu():
searchView = (SearchView) menu.findItem(R.id.abSearch)
.getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setQueryRefinementEnabled(true);
So to summarise things:
The tags used to my searchable.xml doesn't seem to attach to my SearchView, what am I doing wrong?
Thanks for taking the time to read this, I hope you can help me :)
You need to add the android.intent.action.SEARCH intent filter as well. See the code here for reference.
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
I think you also need a <meta-data> element as a child of the <application> element
<application>
<meta-data
android:name="android.app.default_searchable"
android:value="com.simon.holocountownapp.ItemListActivity" >
</meta-data>
</application>
I don't recall if this is documented. I think I found it in one of the sample applications.
I had the same problem, seems that I was missing the default searchable meta-data tag in the manifest.
Add this where you declare the Activity that sends the search to the searchable Activity:
<meta-data android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
Your manifest should look like this:
<application ... >
<!-- this is the searchable activity; it performs searches -->
<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>
<!-- this activity enables the search dialog to initiate searches
in the SearchableActivity -->
<activity android:name=".OtherActivity" ... >
<!-- enable the search dialog to send searches to SearchableActivity -->
<meta-data android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
</activity>
...
docs

android searchable doesn't work

I'm working around with a searchable action bar. And I gotta a problem that the action bar doesn't react with the searchable activity. THAT IS, I enter something in the action bar's searchable textedit then clicked the submit but nothing happened. The debug tracking shows that the code in my searchresultactivity is never executed. So I'm wondering if there's something wrong in searchable configuration. Well this time I think I've explained the problem clearly and I don't expect any negative votes without any words even spits!
I followed the instruction of developer's document beginning with the manifest.xml, the meta-data is added in the searchresult activity:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".GermanDictionaryActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
</application>
then I initialized the searchview with my menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.srhbar, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSrhView = (SearchView) menu.findItem(R.id.search).getActionView();
mSrhView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSrhView.setSubmitButtonEnabled(true);
return true;
}
And the searchable item in menu:
<item android:id="#+id/search"
android:title="#string/srh_title"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
Any answer related is appreciated!
I solved the problem by changing this line:
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(getApplicationContext(), SearchResultsActivity.class)));

How to declare 2 search activities in android manifest

I am trying to implement two different searchable activities, one for honeycomb(with search widget) and others for non-honeycomb(with search dialog).
My Manifest looks like:
<activity android:name=".activities.Search"
android:theme="#style/NoTitleTheme"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTop">
android:enabled="#bool/disableForNonHoneycomb"
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"
android:value=".activities.Search"/>
</activity>
<activity android:name=".activities.SearchHoneycomb"
android:theme="#style/CustomTheme"
android:configChanges="orientation|keyboardHidden"
android:enabled="#bool/enableForNonHoneycomb"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"
android:value=".activities.SearchHoneycomb"/>
</activity>
And, I use following code to get the searchable configuration and set up the search view in my activity.
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
_searchView = (SearchView) mCustomView.findViewById(R.id.action_bar_searchwidget);
_searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
Problem is that when I use the search widget, it does not start the SearchHoneycomb activity. How can I make this work?
Thanks.
So, after a lot of investigation... there is no way to have 2 searchable activities as android manifest only accepts 1 meta-data for default-searchable.
I got a work around by using a search dialog with default-searchable meta-data and the implemented the querytextlistener for search widget.

Categories

Resources