Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'collapsArch' does not have a method 'enqueue_scripts' in /homepages/15/d370320864/htdocs/wordpress/wp-includes/class-wp-hook.php on line 307

Grokking Android

Getting Down to the Nitty Gritty of Android Development

Why is There no Configuration by Default for Android Search?


If you use Android search a lot you will have noticed that you write the same lines of code again and again. You have to configure two things to make Android's search work:

Coming from the Java EE world, this reminds me of the state there a few years ago. Tedious XML files had to be edited - and they had to be way too verbose. But then the Java EE world moved gradually to convention by default. You just use XML for what isn't part of the code or isn't covered by reasonable defaults anyway. That's what I would like to see for Android as well.

Android's search configuration could also be replaced by meaningful defaults, so that some unnecessary definitions could be left out. Let me explain what I hope the Android developers will implement one day:

Configuration by default for the search configuration file

For your search activity you have to state where the configuration file can be found. You do this in your AndroidManifest.xml file:

      android:resource="@xml/searchable" />

Most often Android developers name the search configuration file searchable.xml and store it within the res/xml folder. Shouldn't it be reasonable to assume that this is the case unless stated otherwise?

Actually there is only one reason why you should ever have to stray from this default: That is when you need two or more different search configurations within one project. Which should be very rare. In this case there is no way around using the &lt;android:meta&gt; element for both configurations. But only then.

Configuration by default for the search activity

Another information that you must add to your AndroidManifest.xml file is the definition of the search activity to use. This is done like this for the default search activity of your app:

      android:value=".SampleSearchActivity" />

But configuring the search activity in most cases is also superfluous. Most often the search is global for a project. So why not assume that this is the default. So if the AndroidManifest.xml file contains exactly one activity that has an intent filter for the action android.action.SEARCH this activity should be the global default for search. If more than one activity with the intent filter exists or if you want to use search only for certain activities you still can configure this by hand.

Probably another attribute is needed if you want to use the default, but want to exclude one activity from search. A possible solution could be

<meta-data android:name="android.app.not_searchable" 
           android:value="true" />

Granted: Both snippets are not that much code. But anything that is not necessary should be left out. It could be a source of error - and also of frustration.

To be fair: There are reasonable default values for most keys in the search configuration file. So this file can be kept to a minimum. Configuration by default would just be the next logical step.

Are there other configuration files or entries, you think that are superfluous and could be replaced by freasonable defaults? And what else could be optimized? Let us know in the comments - and if you have posted issues, please add a link to them. Thanks!

Wolfram Rittmeyer lives in Germany and has been developing with Java for many years.

He has been interested in Android for quite a while and has been blogging about all kind of topics around Android.

You can find him on Google+ and Twitter.