Quick Search is a great thing on the Android platform. Especially since Android 1.6 will have a global suggestions list for you to add in. This means all apps that have global search enabled will show the search suggestions your app is providing. Quick Search is the little search UI that pops over your Activity when you press the looking glass search-key on your Android device. However, tapping into it with your application for the first time just sucks hard. Follow this blogpost and you’ll have search in your app in no time.

Implementing basic Android search with SearchManager in your app involves these steps:

  1. Creating a search activity. This activity will display all the results for the keyword provided by the user via de Quick Search bar.
  2. Adding the required elements to your AndroidManifest.xml as well as configuring a searchable.xml meta-data file.
  3. Displaying results in your search activity.

Creating the search activity

I use Eclipse to develop my apps, so if you are too, create a ListActivity by creating a new Java Class in your application package. Called it something like MySearchActivity and make sure the Superclass is ListActivity.

Press Command – Shift – O to load in the required imports and add the following onCreate function:

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final Intent queryIntent = getIntent();
		final String queryAction = queryIntent.getAction();
		if (Intent.ACTION_SEARCH.equals(queryAction)) {
			String searchKeywords = queryIntent.getStringExtra(SearchManager.QUERY);
		}
	}

You’re basically all done here. This onCreate function gets the Intent which your Activity has been called with. It contains the name of the action (ACTION_SEARCH) and the actual search keywords. You can now use the searchKeywords string to actually search your application. For example, get a web search out using a GET request or search your application’s internal sqlite database.

Adding the required elements to your AndroidManifest.xml

This part was the most tricky for me. The Android SearchManager documentation just had a jungle of information on it but it’s just too much to handle and hard to distill the actual information from. So save yourself some time and let me tell you what you need to add.

For the activity that actually displays the search results – the activity we created in the previous step – add the following:

<activity android:name="MySearchActivity"
                  android:label="Search">
            <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>

This activity element tells your application that the MySearchActivity is an Activity that can be used to invoke the search. It contains a meta-data element which is a pointer to an xml file that contains meta information for the search UI and behavior, such as labels and wether to include global search suggestions. This is the bit where I went wrong so pay close attention.

Now, right-click your res/ folder and create a new Android XML file called searchable.xml. Select Searchable from the options of file types. Eclipse should now create the res/xml/searchable.xml file. It will create the xml/ directory if it didn’t exist yet. In this file, add the following:

 <searchable xmlns:android="http://schemas.android.com/apk/res/android"
     android:label="@string/search_label"
     android:hint="@string/search_hint" >
 </searchable>

The contents of this file configures how the search field is displayed and you can set a lot of options for the behavior. Read about all the options in the SearchManager#SearchabilityMetadata section of the Android Developer documentation. Your own search bar won’t work if you don’t use @string resources for the configuration keys in this file. So don’t use static strings because they won’t work and you’ll loose precious hours of development time to debugging, like I did.

So, add the search_label and search_hint strings to res/values/strings.xml and put in something nice.

Displaying results in your search activity

So we have the initial onCreate function with the search keyword, and we’ve configured the search UI to pop up when the user presses the search key. Now it’s time for displaying the actual results. I will not get into technical details here because you can implement this in a lot of ways. Maybe I’ll dedicate the next blog post to this part. For now, do the the following:

  • Based on the searchKeywords string you have, fire up your search in your local sqlite database or do some HTTP requests to fetch a list of search results from.
  • Add them into an ArrayAdapter<MyClass> with nice formatting.
  • Bind the ArrayAdapter<MyClass> with the SearchActivity by calling setListAdapter(myListAdapter) in the onCreate function.
  • You’re ready to go!

Take a look at ListActivity on the Android Developer site.

Conclusion

If you know the right – and actually simple – steps, implementing search in your application is easy. It’s the actual researching of the docs that suck. Please feel free to leave a comment or reply me on Twitter at @michiels.

{ 45 comments }

Research it the practical way

by Michiel on October 16, 2009 · 0 comments

in Uncategorized

We are working on various projects that involve e-commerce. Having good content and a great website structure is important for this so it will show up nicely in all the search engine results and will be linked to by many people. I have some experience with building a web app so it displays nicely for Google but I decided that I needed to know more. Especially how users and search engines react to certain content and site changes.

If I need to learn something, I need to learn it the practical way. Oh of course I read books and tutorials for all the nice tips and best practices but actually experiencing the thing you’d like to research is the best way of learning something because you can tweak things fase and see how it comes out.

Regarding the e-commerce/SEO thing I decided to set up a “fake” website where the visitor can search for books and see where they can buy it online, hopefully for the lowest price. This however, is not a required thing. The goal is to see how people reach the website and if they actually use the referral links to order the books. And of course, how I can improve these sort of things.

To be certain: this is not a new web concept that is providing a great service for the user of finding and buying books. It’s a bad-ass affiliate site because I want to know if they work and how they work because I’ve heard of a lot of these sites and apparently they somethings work really well.

The website in question is Bestel het Bij. I’ve already had some conversations with people around and the next focus will be: adding dynamic content and quality content.

{ 0 comments }

What the most awesome app deployment app looks like

by Michiel on October 15, 2009 · 0 comments

in Uncategorized

Today was another day of the first day of the first release of a new web application. After prototyping the new project for a few days, the time had come that we’d finally show the first part to the rest of the team members and the client. Currently, it’s the least thing I like about releasing new apps: deploying. It always involves setting up the Git repository on Github, adding domain configuration on the web server (or even worse, setting up a load balanced environment), launch ec2 instances and write or configure the (web|cap)istrano scripts. It takes hours and multiple things fail you’ve already done a thousand time on other deployments. Here’s a list of what the most awesome deployment app would have:

  • It should be a web app, hosted, pre-installed, no fuzz, clean-interface, Basecamp-like subscription payed shizzle.
  • It MUST support with deploying from various SCM’s like Git, SVN, Bzr and maybe even still CVS. (Git with submodules, svn with externals, you do the math)
  • It MUST constrain the user to some broadly supported standard deployment strategies. For instance Ruby on Rails applications with Apache/NGINX and Phusion Passenger (aka Mod Rails). The user should not have to worry about the background of the deployment and should not have the power to break stuff.
  • It MUST have a philosophy of projects/applications over servers. Your app runs “in the cloud”. The architecture behind it is secondary. Instead of installing an application clone on a new server and configure the load balancer, you add a server – or better an instance – to your application and the application makes sure the new instance of itself gets picked up.
  • It SHOULD take over all the configuration of the application. From generating passwords to setting up databases and migrations to configuring new servers in a load balancer. The deployment app knows best.
  • The user should only have to add their deployment keys or maybe a local deployment password everything else (like deployment locations and stuff) should be convention over configuration. Everything the user want’s to add should be available through nice gradient-like big buttons with smilies on them and the user MUST be greeted with “Yo, dude”.
  • Everything should be as sexy as your girl or boyfriend.

And no worries guys, we are buiding this right now.

P.S.: If you don’t have a girl or boyfriend, pick your dog or cat. If you don’t have those either. You’re not allowed into the beta.

{ 0 comments }

Sneak preview of Cash Buddy

by Michiel on September 29, 2009 · 0 comments

in General Posts

This is a sneak preview screenshot of Cash Buddy. Cash Buddy is about tracking your personal cashflow. It helps you plan bills and expenses and let’s you keep track of what money is coming in and going out. Just to make you sure you won’t have to worry about your personal finances. What do you think?

Cash Buddy Design

{ 0 comments }

The yacht I’m going to own in 5 years

by Michiel on September 29, 2009 · 2 comments

in General Posts

Yacht

Yacht

{ 2 comments }