Search Interface opens a new Activity when search - android

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>

Related

Creating a Search Interface in a Android with Kotlin

I am having a hard time figuring out the documentation on creating a search Interface.
My main activity contains the search widget on the app bar but when I perform a search query it does not launch the Searchable Activity; responsible for displaying the results.
Below is my manifest file for the SearchbaleActivity and MainActivity
<activity
android:name=".activities.SearchableActivity"
android:exported="false"
android:label="#string/title_activity_searchable"
android:theme="#style/Theme.MoveApplication.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<activity
android:name=".activities.MainActivity"
android:exported="false" >
</activity>
Should my intent-filter and meta-data attributes be in my MainActivity or SearchableActivity?
And even so, my SearchableActivity should launch despite not handling the query, right?
My Android skills are not that great so i'll appreciate a vivid explanation with examples

Android Search Activity and android:launchMode="singleTop"

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.

Contacts's manifest explanation

I'm developing a custom contacts app. I read default Contacts app source from Github and I don't fully understand.
1, As far as I know ContactsSearchActivity will be called if an intent has action equal
com.android.contacts.action.FILTER_CONTACTS (full manifest)
<!-- The contacts search/filter UI -->
<activity android:name="ContactsListActivity$ContactsSearchActivity"
android:theme="#style/ContactsSearchTheme"
android:windowSoftInputMode="stateAlwaysVisible|adjustPan"
>
<intent-filter>
<action android:name="com.android.contacts.action.FILTER_CONTACTS" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/contact" android:host="com.android.contacts" />
</intent-filter>
</activity>
But the ContactsSearchActivity has no code.
public class ContactsListActivity extends ListActivity implements View.OnCreateContextMenuListener,
View.OnClickListener, View.OnKeyListener, TextWatcher, TextView.OnEditorActionListener,
OnFocusChangeListener, OnTouchListener {
public static class ContactsSearchActivity extends ContactsListActivity {
}
So i don't understand if ContactsSearchActivity is called.(Full source)
2, The second is about search interface. I know activity which has
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"
is the activity handle query and display result. And the one has android:name="android.app.default_searchable" is enabled search + point to search handle activity.
<meta-data
android:name="android.app.default_searchable"
android:value=".ContactsListActivity" />
But again in Contacts's manifest, SearchResultsActivity is empty class extended from ContactsListActivity.
<!-- The contacts search/filter UI -->
<activity android:name="SearchResultsActivity"
android:theme="#style/TallTitleBarTheme"
android:label="#string/contactsList"
>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"
/>
</activity>
and
<meta-data
android:name="android.app.default_searchable"
android:value=".ContactsListActivity" />
in application tag not activity tag (link).
I want to understand these because i don't know how to display this instead of
Thanks in advance and sorry for long question.
Edit: It's ok with part1. In part 2, I want to display a custom diaglog when press search button but now i can only archive result in second image.
That means everything is handled by the codes in ContactListActivity.
Take a look at the onCreate method of that, it reads the incoming Intent to determine how to display the result. It's nothing too special, as many Android app is working in this way.
I change my code and achieve my custom search dialog.

How do I invoke the search dialog using onSearchRequested()

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"/>

Two searchable.xml activities in one AndroidManifest.xml

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
}

Categories

Resources