Search is a core feature of Android. You can use a configurable standard search dialog for performing searches in your app. This search dialog can also be used within a Basic4Android App (with some limitations). This tutorial will show you how to use the Android search dialog in Basic4Android.
When the user executes a search from the search dialog, the system creates an Intent and stores the user query in it. The system then starts the activity that you've declared to handle searches (the "searchable activity") and delivers it the intent. To set up your application for this kind of assisted search, you need the following:
- A searchable configuration. This is an XML file with a configuration for the search dialog. Unfortunately we have to use XML here. It is not possible to create a searchable configuration by code.
- A searchable activity: This is a special activity which receives and executes the query. We can use a B4A activity module for this activity but we have to add some additional properties in the Manifest editor to it.
- An Activity which can open the standard Android search dialog. This can be any activity in our app. We again need to add some things in the Manifest editor for this.
The searchable configuration
Lets start with the searchable configuration. The searchable configuration is just a XML file where you can configure the features of the standard Android search dialog like voice search etc.
This is a simple example of a searchable configuration file:
The only mandatory property is android:label. Every other property is optional. As you can see we can not provide the strings directly to the "label" and "hint" properties but we have to use another resource file here for the strings. The third property (voiceSearchMode) enables the voice search if it is installed on your device.
For a complete documentation of the searchable configuration look here.
Save the searchable configuration in Objects/res/xml/searchable.xml folder in your project home and make it READ ONLY!
The string resource file looks like this and should be stored under Objects/res/values/string.xml
Be sure again to make the file READ ONLY otherwise the IDE will delete it on compile!
If you make any changes to the xml files use "Tools/Clean Project" before compiling.
The searchable activity
Now as we have our searchable configuration ready lets create a new Activity in B4A which will handle the search request. Select Project/Add new module/Activity Module from the Menu and name it "Result".
We have to declare this activity to our "searchable activity" in the manifest file. So add the following to the manifest editor:
With the android:resource property you specify the searchable configuration xml file we created in the first step.
The activity itself receives the query string in the Intent. So our Activity_Create sub should look something like this:
As you can see, the action of the intent is "android.intent.action.SEARCH" and we can get the query string with GetExtra("query").
What you do with the query string is totally up to the activity. You can search in a database or search some information online or just store it in a process global variable to pass it to the calling activity.
How to open the search dialog
Now we have a configuration for the serach dialog and an activity which can handle a search request. The last step now is to just call/open the search dialog. We do this in the main activity. To mark our main activity so that it is allowed to open the search dialog and to configure which activity to start for the search request we again have to add some configuration in the manifest editor:
This enables the search dialog in our main activity. The search dialog is hidden normally and you can activate it with the device search button. If you want to open the search dialog by code you have to use a simple part of code which uses the reflection library:
Now we are done. See the attachment for a more or less complete example.
In the example the searchable activity is invisible and the query string is just passed back to the main activity with the help of process global variables.
If you have any further questions please ask.
Some remarks:
- you can let the searchable activity initiate a search with itself. If the user initiates the search, Activity_Resume is called and you can get the search Intent with Activity.GetStartingIntent. For an example how to use this see the WorldClock example.
You can simulate this behavior with a transparent searchable activity. See the example.
- It is currently not possible to use suggestions for the search or a search history. Perhaps this will be possible with an additional library but I haven't tried it so far.
When the user executes a search from the search dialog, the system creates an Intent and stores the user query in it. The system then starts the activity that you've declared to handle searches (the "searchable activity") and delivers it the intent. To set up your application for this kind of assisted search, you need the following:
- A searchable configuration. This is an XML file with a configuration for the search dialog. Unfortunately we have to use XML here. It is not possible to create a searchable configuration by code.
- A searchable activity: This is a special activity which receives and executes the query. We can use a B4A activity module for this activity but we have to add some additional properties in the Manifest editor to it.
- An Activity which can open the standard Android search dialog. This can be any activity in our app. We again need to add some things in the Manifest editor for this.
The searchable configuration
Lets start with the searchable configuration. The searchable configuration is just a XML file where you can configure the features of the standard Android search dialog like voice search etc.
This is a simple example of a searchable configuration file:
B4X:
<?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_text"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" >
</searchable>
The only mandatory property is android:label. Every other property is optional. As you can see we can not provide the strings directly to the "label" and "hint" properties but we have to use another resource file here for the strings. The third property (voiceSearchMode) enables the voice search if it is installed on your device.
For a complete documentation of the searchable configuration look here.
Save the searchable configuration in Objects/res/xml/searchable.xml folder in your project home and make it READ ONLY!
The string resource file looks like this and should be stored under Objects/res/values/string.xml
B4X:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SearchDialogExample</string>
<string name="search_text">Search for something...</string>
</resources>
Be sure again to make the file READ ONLY otherwise the IDE will delete it on compile!
If you make any changes to the xml files use "Tools/Clean Project" before compiling.
The searchable activity
Now as we have our searchable configuration ready lets create a new Activity in B4A which will handle the search request. Select Project/Add new module/Activity Module from the Menu and name it "Result".
We have to declare this activity to our "searchable activity" in the manifest file. So add the following to the manifest editor:
B4X:
AddActivityText(Result, <intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>)
With the android:resource property you specify the searchable configuration xml file we created in the first step.
The activity itself receives the query string in the Intent. So our Activity_Create sub should look something like this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
searchIntent = Activity.GetStartingIntent
If searchIntent.Action = "android.intent.action.SEARCH" Then
Dim SearchString As String
SearchString = searchIntent.GetExtra("query")
ToastMessageShow("Search for: " & SearchString, False)
Else
ToastMessageShow("No search action!", False)
Activity.Finish
End If
End Sub
As you can see, the action of the intent is "android.intent.action.SEARCH" and we can get the query string with GetExtra("query").
What you do with the query string is totally up to the activity. You can search in a database or search some information online or just store it in a process global variable to pass it to the calling activity.
How to open the search dialog
Now we have a configuration for the serach dialog and an activity which can handle a search request. The last step now is to just call/open the search dialog. We do this in the main activity. To mark our main activity so that it is allowed to open the search dialog and to configure which activity to start for the search request we again have to add some configuration in the manifest editor:
B4X:
AddActivityText(Main, <meta-data android:name="android.app.default_searchable"
android:value=".result" />)
This enables the search dialog in our main activity. The search dialog is hidden normally and you can activate it with the device search button. If you want to open the search dialog by code you have to use a simple part of code which uses the reflection library:
B4X:
Sub Button1_Click
Dim ref As Reflector
ref.Target = ref.GetActivity
ref.RunPublicmethod("onSearchRequested", Null, Null)
End Sub
Now we are done. See the attachment for a more or less complete example.
In the example the searchable activity is invisible and the query string is just passed back to the main activity with the help of process global variables.
If you have any further questions please ask.
Some remarks:
- you can let the searchable activity initiate a search with itself. If the user initiates the search, Activity_Resume is called and you can get the search Intent with Activity.GetStartingIntent. For an example how to use this see the WorldClock example.
You can simulate this behavior with a transparent searchable activity. See the example.
- It is currently not possible to use suggestions for the search or a search history. Perhaps this will be possible with an additional library but I haven't tried it so far.
Attachments
Last edited: