I'm trying to implement the search dialog and I am unable to display the search from an Activity.
I have my main activity defined in my manifest file, this activity shows the user a list of options they have to choose from. One of the options is a Search option.
<activity
android:name=".MenuListActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
My Search activity is defined in my manifest file like so.
<activity android:name=".SearchActivity"
android:launchMode="singleTop"
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>
Now my problem is when I call the onSearchRequested() from my MenuListActivity nothing happens.
public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
String strText = items[position];
if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_browse))) {
startActivity(new Intent(MenuListActivity.this, WSMobileActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_search))) {
// If i call it like this it doesn't work, nothing happens
onSearchRequested();
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_locator))) {
startActivity(new Intent(MenuListActivity.this, StoreLocatorActivity.class));
}
}
Please help, how do I invoke the search request from my MenuActivity?
did you check your res/xml/searchable.xml?
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:label="#string/search_label">
</searchable>
Search dialog doesn't show up when you have hard coded strings for hint and label. They have to be #string resources.
Also, there is no need to call or override the onSearchRequested() method in your calling activity unless you want to invoke search from one of your UI widgets like a Button or a ListItem.
Apart from declaring the SearchActivity in the manifest file you need to include the meta-data info.
If you want to invoke the search dialog throughout the application then include
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
inside the application tag.
If you want to invoke the search dialog only in a particular activity then include
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
inside the activity tag.
for more details please refer
http://developer.android.com/guide/topics/search/search-dialog.html.
You are missing tag in your activity tag in manifest. replace your activity declaration by following.
<activity
android:name=".MenuListActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
I've done all that but still cannot see the search dialog on API 17. It works however after adding
android:actionViewClass="android.widget.SearchView"
in the fragment menu
<item
android:id="#+id/menu_item_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:title="#string/search"/>
Related
In an Android's search activity, is it possible to handle the event when the user touches the cross button to clear the search box?
The button I'm talking about is the cross in the screenshot:
This is the definition on the manifest:
<activity
android:name=".MySearchActivity"
android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
This is the xml referenced on the meta-data:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" />
use SearchView.OnQueryTextListener interface, in particular see onQueryTextChange(String newText) method
I have a single activity in my app that uses a SearchView to search (see manifest-- the search intent is intercepted).
However, when I press search, nothing happens. The activity never re-starts itself. So, I removed android:launchMode="singleTop" from my activity and then it started re-starting itself and "working." However, it is preferable to have singleTop for me so that the activity only has one instance. Why is this not working when I have singleTop?
<activity
android:name="com.brianco.andypedia.MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop" >
<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>
<meta-data android:name="android.app.default_searchable"
android:value=".MainActivity" />
I don't know if this answers your question, but this page shows a simple searchview example with ActionBar Compat (it is the new google library compatible for the old Android versions)
You need to process your search intent on onNewIntent besides onCreate.
I am trying to use the Search Manager from Android.
In fact, I have an activity where I am calling
onSearchRequested()
Then, I am using in the same activity this function to get the search string:
// 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);
}
The problem is the next: When i am clicking to the search button, I am opening a new activity and i would like to stay on the same and do some searches. So, my goal is to avoid the new activity opening when I click to the search button.
Thank you.
The solution consists to reuse the developpers example http://developer.android.com/guide/topics/search/search-dialog.html
The important thing is to create 2 activities, one for searches and one other for displaying the results.
In the manifest, use this code:
<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>
...
use in Manifest
<activity android:name="BrowseItems" android:label="#string/browseitems"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/itemsearchable" />
</activity>
I am trying to implement a search menu for my app.I made a menu "seach",in onOptionsItemSelected I have:
if (item.getItemId()==R.id.search) {
onSearchRequested();
return(true);
I put in strings.xml :
<string name="searchLabel">Lists</string>
<string name="searchHint">search</string>
and in searchable.xml :
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/searchLabel"
android:hint="#string/searchHint" />
but nothing happens after I type something. What means "Lists" from strings.xml?
Where is my mistake?
This topic is covered in Android developer guide here:
Don't forget you need to tell Android about your search activity in your AndroidManifest.xml
<activity
android:name=".ui.SearchActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action
android:name="android.intent.action.SEARCH" />
<action
android:name="android.intent.action.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
I have an Android app which has a few different activities for browsing articles and images downloaded from RSS.
I'd like to be able to offer to hook up the search button to the Search dialog, using the a searchable.xml file. I've managed to do this with one search, using:
<activity android:name=".search.SearchResultsActivity"
android:label="#string/search_results_activity_title" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable_articles"/>
</activity>
and in the <application />
<meta-data android:name="android.app.default_searchable"
android:value=".search.SearchResultsActivity" />
I can now launch the Search dialog from any activity, and it launches the SearchResultsActivity.
I would now like to be able to search for images when the user is an ImageListActivity, using a searchable_images.xml, and use the default everywhere else.
I have a SearchResultsImageActivity which includes the following meta-data element, and used the same element in the ImageListActivity.
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable_images"/>
On pressing the search button in the ImageListActivity, I get the default search from searchable_articles.xml.
If I change the default_searchable to SearchResultsImageActivity, the image search is always launched, and the article search is never launched.
If I remove the default_searchable meta-data element altogether, and add searchable meta-data only selected activities, no search is launched.
I'm fairly sure this should be possible, but I don't know what I'm doing wrong.
In your Manifest file update the ImageListActivity activity tag
<activity
android:name=".ImageListActivity"
...
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsImageActivity" />
</activity>
So when you will trigger native search in ImageListActivity it will invoke the SearchResultsImageActivity and default one for others.
Assuming SearchResultsImageActivity is searchable.
One way I did this was to create fake activities then switch out the activities when you need them.
<activity android:name="activitySearchMain" />
<activity android:name="activitySearchSub1">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.ALTERNATIVE" />
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
<data android:scheme="user" />
</intent-filter>
</activity>
<activity android:name="activitySearchSub2">
<intent-filter>
<action android:name="com.sample.twitter.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.ALTERNATIVE" />
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
<data android:scheme="user" />
</intent-filter>
</activity>
Create two class that are named for the sub activities.
then create intents like this when component is clicked...
Intent sourceIntent = getIntent();
Intent newIntent = new Intent(this, activitySearchSub2.class);
newIntent.setAction(activitySearchSub2.ACTION2);
newIntent.setData(sourceIntent.getData());
startActivity(newIntent);
finish();
and call the intents from onClick when a button is clicked or some other component is click:
If you override the search function in just that activity, it should prevent the search call from going up to the application level. The return value controls if the call is propagated up.
#Override
public boolean onSearchRequested() {
onPromptSearch();
return false; // don't go ahead and show the search box
}