1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Tutorial Using standard Android search dialog with Basic4Android

Discussion in 'Tutorials & Examples' started by corwin42, Feb 29, 2012.

  1. corwin42

    corwin42 Expert Licensed User

    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:

    Code:
    <?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

    Code:
    <?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:

    Code:
    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:

    Code:
    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:

    Code:
    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:

    Code:
    Sub Button1_Click
       
    Dim ref As Reflector
       ref.Target = ref.GetActivity
       ref.RunPublicmethod(
    "onSearchRequested"NullNull)
    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.
     

    Attached Files:

    Last edited: Feb 28, 2013
    pedrocam, Mashiane, iz0ndg and 2 others like this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    Thank you for sharing this great tutorial!
     
  3. Asmoro

    Asmoro Active Member Licensed User

    Using the search dialog with scrollview result

    Hi Corwin42,

    Thanks a lot with your search dialog.:sign0098:
    I'm implementing your code right now.

    However, I tried with my scrollview function to get the search result, unfortunately nothing happens.

    I think it has to do with this part of code:

    Code:
    SearchString = searchIntent.GetExtra("query")
          PerformSearch(SearchString)
    Instead of "query", can I put here my foldername or string to my sdcard or....?
     
    Last edited: Mar 1, 2012
  4. corwin42

    corwin42 Expert Licensed User

    No. With GetExtra("query") you get the query string from the search dialog. You can not get any other data here.

    You are trying to use just one activity for initiating the search and as the searchable activity. This is not possible (with B4A). You have to use a second activity as searchable activity as in the example code.
     
  5. Asmoro

    Asmoro Active Member Licensed User

    Hi Corwin42,

    According to your answer I made your example build with a scrollview.
    But the function itself is not working yet.

    I will post this example at Basic4android Updates and Questions forum,
    for further help.

    Thanks again.
     
  6. Eugenio Serrano

    Eugenio Serrano Member Licensed User

    Thanks a lto. It is very useful.

    Now have a question: Are there any way to change the text "Search for something..." by code ? I just want to show different text for each query..

    Regards,
    Eugenio
     
  7. corwin42

    corwin42 Expert Licensed User

    I don't think this is possible. You can't create the searchable object by code. This MUST be xml and you can only add support for multiple languages with the standard android method using different string resource files for each language.
     
  8. cjack3

    cjack3 Member

    Hi everyone. I'm kind of new using this software, (I think it's great, though), and I was thinking of using this search feature. However, I've been getting this error message :

    Compiling code. Error
    Error compiling program.
    Error description: Unknown member: action
    Occurred on line: 16
    If searchIntent.Action = "android.intent.action.SEARCH" Then
    Word: action

    Can anyone help me with this error? I think I've followed the steps correctly, though based on this error, obviously something went wrong somewhere. Could anyone point me in the right direction about this?
     
  9. corwin42

    corwin42 Expert Licensed User

    You have to declare the searchIntent variable with

    Code:
    Dim searchIntent as Intent
    this is missing in the text above. See the Example code it should be in there.
     
  10. gapi

    gapi Member Licensed User

    How can show it on activity start ? (not hidden)


    thanks ;)
     
    Last edited: May 4, 2012
  11. corwin42

    corwin42 Expert Licensed User

    You can open the search dialog with the code in the Button1_Click Sub in the example. You should be able to call this in Activity_Create/Resume.

    The search string has to be sent to a second activity. The same Activity is possible in a Java project, but unfortunately not in B4A. The workaround is to use a invisible activity, store the search string in a process global variable and immediately finish the activity again to return to the old activity. You can then process the search string in Activity_Resume.
     
  12. gapi

    gapi Member Licensed User

    Thanks corwin42 try on Activity_Create (to open search dialog) why b4app make a new AndroidManifest-Example.xml ? (I make it read-only) Can create some problem this ?

    thanks :icon_clap:
     
  13. xor83

    xor83 Member Licensed User

    I am getting error message Please check screenshot
     

    Attached Files:

  14. Erel

    Erel Administrator Staff Member Licensed User

    You should check the logs and post the error message.
     
  15. gapi

    gapi Member Licensed User

    Corwin in B4A 2.02 there's problem with manifest ? thanks
     
  16. corwin42

    corwin42 Expert Licensed User

    No known problem here.
     
  17. QLogic

    QLogic Member Licensed User

    I have a question regarding search. If I want to just filter the search to just display results that contain PDF's, is that possible?

    Thanks.
     
  18. corwin42

    corwin42 Expert Licensed User

    The resulting Activity just gets the search string the user have entered. You can do what you want with this string, even do a search for PDF files or whatever you want.
     
  19. bluedude

    bluedude Well-Known Member Licensed User

    Pretty nice but a few problems

    Hi,

    This is nice but I face a few problems. I want to rename the Result activity to actSearch and I changed all manifest stuff and code for that but it does not work.

    No error message and the search does not show etc. Wondering if it only works with lowercase names?

    Cheers,
     
  20. bluedude

    bluedude Well-Known Member Licensed User

    Solved the problem, indeed a key case.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice