how to implement search box in my application in android? - android

i have one activity in my android application in which a list of orders is populated. I want to implement a search functionality on this activity so that user can search for particular order.
I have read about the search box facility provided by adding a searchable.xml file in the application but I am not able to make it work for me?
I just want to add a search box in the activity and search for specific data in database.
How can I do this?

#VNVN I would agree - I would review the developer guide. http://developer.android.com/guide/topics/search/search-dialog.html
You will need to make your activity search-able in the AndroidMainfest.xml file
<activity android:name=".MySearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>

You are probably missing creating a SearchableActivity. http://developer.android.com/guide/topics/search/search-dialog.html#SearchableActivity

Related

Searchable Activity in Android Studio

I am trying to gather as much information as I can about creating a search interface in Android Studio. I am working with developer.android.com and have a question about using the search interface.
I quote:
<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>
...
"If you want every activity in your application to provide the search dialog, insert the above meta-data element as a child of the application element, instead of each activity. This way, every activity inherits the value, provides the search dialog, and delivers searches to the same searchable activity."
I don't understand the first sentence, unfortunately. What do I have to do exactly in order to have my search dialog in every activity. In this code example, we only have the search dialog in one activity. Can anyone show me using the code above, how to insert meta-data as a child of the application element in my AndroidManifest.xml?
Thank you.
It just describes an easy way of doing things.
You add the <meta-data> element to every activity that you want to have this behaviour of providing a serach dialog. However, if all your activities have this behaviour, instead of adding the <meta-data> element to all of them, you can just add it to the <application> and it'll work as if you added it to all <activity>s. Just add it to your application like:
<application
android:name=".MyApp"
android:icon="#drawable/ic_launcher">
<meta-data android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
...
</application>

How to add my app into the google local search?

In the android we can see that there is a default "google search" on the launcher. And I found out that using setting, we can set the searchable items. For example amazon kindle, music, or browser.
The question is, how can I add my app into the searchable items in the google search? Are there any APIs I can use?
Exactly, the first thing you do is provide a searchable.xml resource to Android. How this is done is described here.
Next, you have to declare an Activity to be searchable in your Manifest, like so:
<activity android:name="...">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable" />
</activity>

How do I open the Search interface?

I've been following the Searchable Dictionary example pretty carefully as I'm trying to implement the search functionality in my app. Right now I simply can't seem to get the Search interface to open when I press the search button. I've added the following into my manifest and searchable.xml resource into my res folder (which I pretty much copied from the example).
<activity android:name=".main.SearchActivity"
android:theme="#android:style/Theme.NoTitleBar">
<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=".main.SearchActivity" />
What I'm not understanding is what else do I need to do to get this to work? Is there something I should have hard coded? Thanks a lot!
Nevermind, I realized that in the searchable.xml, you can't use static strings, they have to be from the strings.xml resource. I just had to create search_label and search_hint and refer to them in the searchable.xml file. Here's the original article on that.

Search suggestions causing inconsistent behavior in application after triggered from Quick Search Bar

I'm running into an issue with the Quick Search Bar that I'm hoping someone can help me with.
My application includes a Searchable activity used to provide search behavior within my application. That search activity is designed to trigger a separate Intent when the search item is clicked on so as to cause a different Activity to handle the viewing. When used within the application, the search behavior works perfectly every time.
My application also includes a ContentProvider used to provide search suggestions and is also configured to allow use in the Quick Search Bar. When used within the application itself, use of the search suggestions works fine every time it is used. When triggered from the QSB, the initial search suggestion brings up the viewing activity just as it should. After that point, however, any use of the search suggestions from within the application (i.e. bringing up the search and selecting a search suggestion) fails to trigger the viewing application. In fact, I've put debug statements in every single "onXXX()" method within the Searchable activity and I never see any of them get triggered. On the flip side, when I trigger a standard search at that same point (i.e. enter a query string and hit enter, rather than navigating to a search suggestion), the search dialog comes up, as expected, and selecting an item from that list successfully triggers my application.
I'm currently at a loss trying to determine why this would be occurring. Any ideas
As as some additional information, my manifest contains the following in regards to the searchable activity (".activity.SearchableActivity"), the suggestions provider (".content.TestSuggestionProvider") and the activity used to display the content (".activity.TestDisplayActivity"):
<activity
android:name=".activity.TestDisplayActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:finishOnTaskLaunch="true"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden">
<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=".activity.SearchableActivity" />
</activity>
<activity
android:name=".activity.SearchableActivity"
android:launchMode="singleTop"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<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>
<provider
android:name=".content.TestSuggestionProvider"
android:authorities="com.test.provider.suggest"
android:syncable="false"
/>
And the following is the XML used to further define the settings of the searchable activity:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/search_app_label"
android:hint="#string/search_app_hint"
android:searchSettingsDescription="#string/search_app_settings_description"
android:includeInGlobalSearch="true"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:searchSuggestAuthority="com.test.provider.suggest"
android:searchSuggestIntentAction="android.intent.action.VIEW">
</searchable>
Any thoughts? At the moment I'm at a complete loss…

Android: making search available throughout the whole application

Right now I'm adding
<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" />
to every single <activity> in the AndroidManifest.xml, receive the Intent in each activity and forward the search string to one singleTop activity that actually performs the search (via HTTP/JSON) and displays the results in a list.
I'm wondering if there's a way how I can set-up the AndroidManifest.xml, so that search is activated in each activity within my application, but the search string will get forwarded from Android's search box directly to my singleTop SearchResults_Activity instead of going the way through validating events in each activity and then forwarding the search string.
The answer to that is actually available in the SearchManager documentation, in the MetaData chapter.
Activities which are part of a
searchable application, but don't
implement search itself, require a bit
of "glue" in order to cause them to
invoke search using your searchable
activity as their primary context. If
this is not provided, then searches
from these activities will use the
system default search context.
<application>
<meta-data android:name="android.app.default_searchable"
android:value=".MySearchActivity" />
<!-- followed by activities, providers, etc... -->
</application>

Categories

Resources