7

I'm trying to implement a search dialog in a Mono Android app per the documentation here: http://developer.android.com/guide/topics/search/search-dialog.html

I have an activity that the user should be able to search from:

[Activity (Label = "MyActivity", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/MyStyle")]
[MetaData ("android.app.default_searchable", Value = ".SearchActivity")]
public class MainActivity : BaseActivity {...

I have a searchable activity (where the heavy-lifting will happen):

[Activity(Theme = "@style/MyStyle", Label = "Searchable", LaunchMode = Android.Content.PM.LaunchMode.SingleTop)]
[IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryLauncher, Intent.ActionSearch })]
[MetaData("searchable", Resource = "@xml/searchable")]
public class SearchActivity : BaseActivity { ...

And I have my searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:label="MyLabel"
  android:hint="Search Products">
</searchable>

When I press the search key on the phone in the MainActivity, nothing happens - no dialog. I think my problem lies with how the attributes are being translated into the AndroidManifest.xml at runtime but I'm not sure.

UPDATE 1/3/2012: I have posted a project distilled down to the most basic elements here. Press the search button on your Android and you should see the SearchDialog but it doesn't appear: Demo Project Here

Rune FS
  • 21,497
  • 7
  • 62
  • 96
profexorgeek
  • 1,190
  • 11
  • 21

3 Answers3

4

The problem is in the [MetaData] attribute on MainActivity. If you provide the properly namespaced version of the class the search dialog appears correctly:

[MetaData ("android.app.default_searchable", Value = "searchdialogtest.SearchActivity")]
public class MainActivity : BaseActivity {
Greg Shackles
  • 10,009
  • 2
  • 29
  • 35
  • Sorry I waited so long to accept the answer. This solution worked but was missing some information. First of all, the Google Docs imply that prefixing your Activity name with a "." will fully qualify it which is apparently wrong. Second, the namespace must be lower case. I had already tried fully qualifying the class name but it didn't work because my namespace had upper case characters in it. I do not understand why this would be the case. – profexorgeek Jan 12 '12 at 15:46
  • Java requires namespaces to be lowercased, so Mono for Android translates this for you behind the scenes. In this case you had to use the Java fully qualified class name. You can customize the namespace that will be used in the project properties, and you can always check the AndroidManifest.xml file generated during packaging in the obj/Debug/android folder to see what is being generated. – Greg Shackles Jan 12 '12 at 15:55
  • Didn't know that about Java namespaces or how to find the generated manifest file. The one I tried to look at (packaged in the apk) was in binary format. This was very helpful, thanks! – profexorgeek Jan 12 '12 at 23:12
1

in your serachable.xml you can't have a constant string as a name and hint ... they should be linked dynamically @string/my_app_name @string/my_hint

and all work's fine!

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
blackmoon
  • 324
  • 6
  • 17
0

I feel In the manifest file, the searachable meta-data tag is not proper (android:name="android.app.searchable")

Sample Manifest - MainActivity Tag -

<activity android:name=".MainActivity"> <meta-data android:name="android.app.default_searchable" android:value="pckname.SearchClass"/> </activity>

SearchActivity tag -

<activity android:name=".SearchActivity" >
<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>
Vamsi
  • 5,853
  • 6
  • 29
  • 36