Adding Local Search Functionality to Your Android Application

By Promotion Published Date
16 - Mar - 2016
| Last Updated
16 - Mar - 2016
Adding Local Search Functionality to Your Android Application


A basic need required in a wide variety of applications is the search feature. In our case we have a restaurant application and need the user to be able to easily and quickly search the menu for what they want. In this article, I will explain how I added local search capabilities to our existing restaurant application while maintaining UI continuity. I will go into detail about the UI choices I made and why, in addition to going over how to add a GestureOverlayView to the activity.

Figure 1: Screenshot of the search view in the restaurant application


For search, there are a number of design considerations to make before we even start coding. What do you want to search for? We want to search the title and the description for the user to get the maximum results and as titles are not always telling as to what the dish actually is. Additionally you could add some hidden metadata for each dish to search as well. As for the layout of the search activity, how do you want the search results to appear? I started out with a list view to display the results, just like the cart view activity. However, the dishes didn’t look appealing to tap on as the image was small and then when I made the image larger there was less space for results on the page. So I decided to stick with the grid view that the main part of the menu uses, but instead of having a larger detail view on one side, the grid view will take up the whole screen to make it easily differentiable from the normal menu. To see the detail view of the item now, the user taps the item and it appears as a dialogue fragment hovering above the page (see Figure 2). That way the user can quickly tap away to close it and tap on another item to view instead. Search needs to work quickly and smoothly for the user as they want to find what they are looking for as fast as possible, otherwise they might not be able to find the item or get frustrated looking for it and leave. And finally, how are we going to handle the user’s privacy? We could design a search that would offer suggestions based on recent queries or might have a search that requires personal information being entered. This brings up concerns about other people seeing what you are searching for and where your personal data is going. While in our case, it is just a restaurant application so you shouldn’t be too concerned with people knowing you love pie, there are applications where you will need to take privacy into account. For our application, it does not require any personal information, does not log any of the search terms, and there is no history of the search terms.

The first step to implement it in our restaurant app is to look at our database class and add a method to build a new table of search results for use to display. You can read more about the restaurant’s database setup here: Using a Database With Your Android* App. Using a SQLite query we can easily search our database with just a few lines of code to find our items. Here we are searching the name and description for anything that contains the search term plus any additional text after it. We are also returning all the columns as we will need that information later to display it in the detail view. Note that if your database is very large, there could be a delay and you will want to display a progress bar or loading spinner to the user.

* Builds a table of items matching the searchTerm in their name or description
    public Cursor searchMenuItems(String searchTerm) {
        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        Cursor c = qb.query(db, null, "("+MenuColumns.NAME+" LIKE '%"+searchTerm+"%') " +
                "OR ("+MenuColumns.DESCRIPTION+" LIKE '%" + searchTerm+"%')",
                null, null, null, null);
        return c;

Code Example 1: Method for searching our database

Next, our main activity needs to be set up with the search option in the action bar. For more about how to set up the action bar read this article:

Building Dynamic UI for Android* Devices. The search functionality will be handled entirely within our application; we don’t want the search to start listing applications installed on the device or send an intent for another search application to handle.

Add this string variable to the MainActivity class. We will be using this to send the query string to the search intent:

/* Search string label */
    public final static String SEARCH_MESSAGE= "";

Code Example 2: Class variable to add extended data to the search intent

Update the MainActivity’s onCreateOptionsMenu method:

 * Initialize the action menu on action bar
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(, menu);
    //set up the search
    MenuItem searchItem = menu.findItem(;
    SearchView mSearchView = (SearchView) searchItem.getActionView();
    //set up the query listener
    mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
        public boolean onQueryTextSubmit(String query) {
            //start the search intent
            Intent searchIntent = new Intent(MainActivity.this, SearchResultsActivity.class);
            searchIntent.putExtra(SEARCH_MESSAGE, query);
            return false;
        public boolean onQueryTextChange(String query) {
            //do nothing in our case
            return true;
    return super.onCreateOptionsMenu(menu);

Code Example 3: Action bar initialization code

And the SearchResultsActivity class:

public class SearchResultsActivity extends Activity{

    TextView mQueryText;
    GridView searchListResults;
    SearchAdapter adapter;
    Vector<> searchList;

    public void onCreate(Bundle savedInstanceState) {

        mQueryText = (TextView) findViewById(;

        //setup the grid view
        searchListResults = (GridView)findViewById(;
        searchList= new Vector<>();
        //get and process search query here
        final Intent queryIntent = getIntent();
        adapter= new SearchAdapter(this,searchList);
        //Listener for grid view
        searchListResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id){
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                Fragment prev = getFragmentManager().findFragmentByTag("dialog");
                if (prev != null) {
                DialogFragment newFragment = SearchResultsDialogFragment.newInstance(searchList.elementAt(position));
      , "dialog");


Code Example 4: The main search results class (continued below)

When we build the list, we will also handle the case of what to do when we don’t find any matching results as well. If there are no matches, we display a dialogue message to the viewer to let them know and close the search activity so they don’t see a blank page.

     * Builds the found item list.
    private void doSearchQuery(final Intent queryIntent) {
        //Get the query text
        String message= queryIntent.getStringExtra(MainActivity.SEARCH_MESSAGE);
        //Set the UI field
        RestaurantDatabase dB= new RestaurantDatabase(this);
        MenuFactory mMF= MenuFactory.getInstance();
        Cursor c= dB.searchMenuItems(message);
        Set<String> categories = new HashSet<String>();
        while (c.moveToNext()) {
            String category = c.getString(c.getColumnIndexOrThrow(RestaurantDatabase.MenuColumns.CATEGORY));

            //build a new menu item and add it to the list
            MenuItem item= MenuItem();
        //Handle the case of not finding anything
            Intent intent = new Intent(SearchResultsActivity.this, OrderViewDialogue.class);
            intent.putExtra(OrderViewActivity.DIALOGUE_MESSAGE, "Sorry, no matching items found.");

Code Example 4 continued

In this part of the class is the adapter for the grid view, which we were able to reuse from the main menu code itself with some minor edits. We are also able to adapt the layout files as well, so keeping the UI consistent visually has the other benefit of easily recycling code and not having to start from scratch. You might have noticed above, that I also reused OrderViewDialogue, a class I wrote for the cart to use but will also work here.

     * SearchAdapter to handle the grid view of found items. Each grid item contains
     * a view_grid_item which includes a image, name, and price.
    class SearchAdapter extends BaseAdapter {
        private Vector<> mFoundList;
        private LayoutInflater inflater;

        public SearchAdapter(Context c, Vector<> list) {
            mFoundList= list;
            inflater = LayoutInflater.from(c);

        public int getCount() {
            return mFoundList.size();

        public Object getItem(int position) {
            return mFoundList.get(position);
        public long getItemId(int position) {
            return 0;

        // create a new ItemView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            ImageView picture;
            TextView name;       
            TextView price;

            if(v == null) {
                v = inflater.inflate(R.layout.view_grid_item, parent, false);
                v.setTag(, v.findViewById(;
                v.setTag(, v.findViewById(;
                v.setTag(, v.findViewById(;
            picture= (ImageView) v.getTag(;
            name= (TextView) v.getTag(;
            price= (TextView) v.getTag(;

            final MenuItem foundItem = (MenuItem) mFoundList.get(position);
            InputStream inputStream = null;
            AssetManager assetManager = null;
            try {
                assetManager = getAssets();
                inputStream =;
            } catch (Exception e) {
                Log.d("ActionBarLog", e.getMessage());
            } finally {
            return v;

Code Example 4 continued

Another consideration on the layout of the UI is landscape versus portrait mode. Below is the search_query_grid_results.xml in the res/layout-land folder. Here you can see that numColumns is set to four, the res/layout-port file is identical except that the field has a value of two.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
            android:text="Results For: " />
        <TextView android:id="@+id/txt_query"

Figure 2: Detail view of an item that shows when user taps on it

Gesture Overlay

To exit the search view, we would like it to slide left or right with a swipe, similar to the view pager scrolling in the rest of the menu. A GestureDetector works brilliantly over a list view for this, but has no effect when used in conjunction with a grid view. Hence we have to switch to using a GestureOverlayView. You will need to build a gesture library using the GestureBuilder application that you can find in the SDK samples (eg. android\sdk\samples\android-19\legacy\GestureBuilder). Build and launch the app on your device device and use it to name and create gestures.

Once you have added all the gestures you need (in our case, left swipe and right swipe), copy the ‘gestures’ file from your device to res/raw folder. The app will tell you where it is saving the gesture file. In my case, all I had to do was plug in my device via USB and it was there in the root folder.

Figure 3: Screen shot of the gesture builder application and the gestures we added

One you have your file in place update the SearchResultsActivity class with:

GestureLibrary gestureLibrary;
GestureOverlayView gestureOverlayView;

Code Example 6: Variable declaration for the GestureOverlayView

In the onCreate method, initialize the view, load the library, and set up the listener for what to do when the user performs a matching gesture. Make sure to match the name to what you created in the library. For the animation we are going to perform it with the overridePendingTransition call. We have a value of 0 for the incoming animation to specify no animation. You could make a blank animation xml file and reference it, but a good percentage of the time the system will get confused and the outgoing animation will execute extremely quickly.

gestureOverlayView = (GestureOverlayView)findViewById(;
        //initialize the gesture library and set up the gesture listener
        gestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener(){
            public void onGesturePerformed(GestureOverlayView view, Gesture gesture) {
                ArrayList<Prediction> prediction = gestureLibrary.recognize(gesture);
                if(prediction.size() > 0){
                    String action= prediction.get(0).name;
                    //our gesture library contains "left swipe" and "right swipe" gestures
                    if("left swipe".equals(action)){
                        //slide out to the left
                        overridePendingTransition(0, R.anim.move_left);
                    } else if("right swipe".equals(action)){
                        //slide out to the right
                        overridePendingTransition(0, R.anim.move_right);

        //gesture is transparent (no longer a yellow line)

Code Example 7: Initialize the GestureOverlayView in the onCreate

The animation file move_left.xml: (move_right.xml is the same except toXDelta is positive)

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android=""

Code Example 8: Move left animation code

Note that your GridView cannot have a layout_height of 0dp when inside a GestureOverlayView, as it really will only get 0dp instead of expanding as needed like in a linear layout. To accommodate for this in our case, we set the layout_height to fill_parent. We also don’t want our gesture to be visible and we don’t want a delay while we wait for our invisible gesture to fade, so you will see fadeOffset and fadeDuration set to 0.


Code Example 9:Updated GridView piece with the GestureOverlayView for the layout xml

For more such Android resources and tools from Intel, please visit the Intel® Developer Zone