Android Search Dialog Implementation with SQLite Database Example

In this tutorial, we are going to learn how to implement android search dialog in our android application. Search functionality is one of the major features most android applications have.

Android search dialog can be use to search internal data or user provided data. If your application is planning to support android 3 and above, Google suggestion is to use Android SearchView instead of Android Search dialog.

Android SearchView was introduced in android 3 so it is not by default compatible with android versions less than 3. If you plan to use Android SearchView in your app, you will rather consider following my post on Android how to add Search Widget and Implement SearchView in your app.

Android search dialog is hidden by default. In other to activate or show it, you need to call the onSearchRequested() method either in your activity UI or menu action view.

We are going to let the android system to handle the search for us but we will provide our own data which is stored locally in our application SQLite database. To acccomplish this, we need to create a file with android intent action Search. This implies that our search input will be handled by this activity.

Before we dive into more details, it is important for us to understand what we are planning to achieve. Below is the screen-shot of the application we will be creating.

Android search implementation

Lets start to soil our hands in code. we will create our project in our IDE. For this tutorial, I am using the following tools and environment, feel free to use what works for you.

Windows 7

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 23

To create a new android application project, follow the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidSearchDialog

Package: com.inducesmile.androidsearchdialog

Select Blank Activity

Keep other default selections

Continue to click on next button until Finish button is active, then click on Finish Button.

Go to the res folder, then double click on the values folder and double click on the colors.xml file and add the following lines of codes. This will holder all the colors we will use in this tutorial.

<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<resources> <color name="colorPrimary">#3F51B5</color> 
<color name="colorPrimaryDark">#303F9F</color> 
<color name="colorAccent">#FF4081</color> 
<color name="colorBlack">#000000</color>

Now, head over to the strings.xml file and modify the content with the code below.

<string name="app_name">Android Location API</string> 
<string name="latitude">Latitude</string> 
<string name="longitude">Longitude</string> 
<string name="city">Present Location</string> 
<string name="no_text"> </string>

We are going to make use of two Activity pages. The default Activity Page created by our IDE when we create our project – and the second Activity page – which the android system will invoke to handle our search.

We will activate the search dialog box in MainActivity class so that all our search will take place there and the process is directed to SearchableActivity class by android system

We are going to add some meta-data in these activity elements in our project Mainfest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="" package="com.inducesmile.androidsearchdialog"> 
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" 
<activity android:name=".MainActivity"> 
<action android:name="android.intent.action.MAIN" /> 
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> 
<meta-data android:name="" android:value=".SearchableActivity" /> 
<activity android:name=".SearchableActivity" android:launchMode="singleTop"> 
<action android:name="android.intent.action.SEARCH" /> 
<meta-data android:name="" android:resource="@xml/searchable" /> 

The meta-data in the SearchableActivity element has name and resource. The resource is linked to searchable configuration file located in res/xml folder. The system uses this file to instantiate a SearchableInfo object, but you cannot create this object yourself at runtime—you must declare the searchable configuration in XML.

The searchable configuration file must include the <searchable> element as the root node and specify one or more attributes

Now create a folder in your res directory and name it xml. Right click on this folder, click on new file, select xml resource file and name it – searchable.xml.

Open this file in your IDE, copy and paste the code below inside the file

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="" android:label="@string/app_label" 
android:hint="@string/search_hint" >

The android:label attribute is the only required attribute. It points to a string resource, which should be the application name.

Since we have planned to active our hidden search dialog with a menu action view, we are going to create a menu layout that contains a single action view with the default android search icon.

Create a menu directory under the res directory if you project does not have one already. Right click on the menu directory and select new menu resource file. Name the file search_menu.xml. Copy and paste the code below to the file.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="" xmlns:app=""> 
<item android:id="@+id/search_icon" 
android:title="@string/search_hint" android:icon="@android:drawable/ic_menu_search" app:showAsAction="collapseActionView|ifRoom"/>

Now, we will move over to our default layout file – activity_main.xml located in the layout directory. This layout file will only contain a TextView widget that contains some instruction. You can decide to omit it or create your own layout according to your project requirement.

Open activity_main.xml, copy and paste the code below in the file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.inducesmile.androidsearchdialog.MainActivity"> <TextView android:id="@+id/instruction" android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:layout_centerVertical="true" android:textColor="@color/colorPrimaryDark" android:text="@string/instruction" 

For the MainActivity class which will use the layout above to construct its UI elements, We are going to override these two methods onCreateOptionsMenu(Menu menu) and onOptionsItemSelected(MenuItem item). When the search icon item we added in our menu list is click, we will call onSearchRequested() method which will display the android search dialog.

The complete code for the file is as shown below.

import android.os.Bundle;
import android.view.Menu;import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity { 

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

@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(, menu); 
return true; 

@Override public boolean onOptionsItemSelected(MenuItem item) { 
switch (item.getItemId()) { 
return true; 
return super.onOptionsItemSelected(item); } 

When you click on the menu search icon, the search dialog box will appear in the app bar, enter what you are planned to search and press the enter key. It will redirect you to the SearchableActivity class which the android system calls to automatically handle our query.

For the SearchableActivity, we are going to add a ListView in the layout file for this Activity – activity_searchable.xml. The ListView will hold the returned result from the search query execution.

The activity_searchable.xml layout file code is shown below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.inducesmile.androidsearchdialog.SearchableActivity">

<ListView android:id="@+id/listView" 
android:scrollbars="none" />

In the SearchableActivity class, we will get the instance of our ListView widget, we will create an adapter that will bind list items with our datasource.

Since the search intent is sent to this class, we will obtain the sent query and use it to query our database and returns matching results or null if no match is found.

The complete code for the SearchableActivity class as shown below.

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.inducesmile.androidsearchdialog.database.DbBackend;
import com.inducesmile.androidsearchdialog.database.ItemObject;
import java.util.List;

public class SearchableActivity extends AppCompatActivity { 

private ListView listView; 
private DbBackend databaseObject; 

@Override protected void onCreate(Bundle savedInstanceState) { 


// create database object databaseObject = new DbBackend(SearchableActivity.this); 
listView = (ListView)findViewById(; 
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } 



@Override protected void onNewIntent(Intent intent) { 



private void handleIntent(Intent intent) { 

if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
    String query = intent.getStringExtra(SearchManager.QUERY); 

private void doMySearch(String query){ 
List<ItemObject> dictionaryObject = databaseObject.searchDictionaryWords(query); 
SearchAdapter mSearchAdapter = new SearchAdapter(SearchableActivity.this, dictionaryObject); 


You can see that we created a doMySearch(String query) method. Inside this method, we use the obtain search string to query our database. The resulting output from the database query is passed as a parameter to our SearchAdapter class.

We have created a custom Adapter class which inherits from BaseAdapter class. The complete code for the adapter class is as shown below.

import android.content.Context;
import android.view.LayoutInflater;import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.inducesmile.androidsearchdialog.database.ItemObject;
import java.util.List;

public class SearchAdapter extends BaseAdapter {

private LayoutInflater layoutInflater; 
private List<ItemObject> listItemStorage; 

public SearchAdapter(Context context, List<ItemObject> customizedListView) { 
layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 listItemStorage = customizedListView; 


public int getCount() { return listItemStorage.size(); } 

public Object getItem(int position) { return listItemStorage.get(position); }

public long getItemId(int position) { return position; } 

public View getView(int position, View convertView, ViewGroup parent) { ViewHolder listViewHolder;
 if(convertView == null){ listViewHolder = new ViewHolder(); 
convertView = layoutInflater.inflate(R.layout.item_list, parent, false); listViewHolder.dictionaryWord = (TextView)convertView.findViewById(; convertView.setTag(listViewHolder); }else{ listViewHolder = (ViewHolder)convertView.getTag(); 

} listViewHolder.dictionaryWord.setText(listItemStorage.get(position).getWord()); 
return convertView; 


static class ViewHolder{ 
TextView dictionaryWord;


You can see that we used a List to hold the Entity object that wraps our individual item data in ItemObject class. This is the code for this class.

public class ItemObject { 

private int id; 
private String word; 
private String meaning; 

public ItemObject(int id, String word) { = id; 
this.word = word; 


public ItemObject(int id, String word, String meaning) { = id; 
this.word = word; 
this.meaning = meaning; 


public int getId() { return id; } 

public String getWord() { return word; } 

pblic String getMeaning() { return meaning; }}

You can also see that our custom adapter class inflate a layout file – list_item.xml. The contain of this layout file is as shown.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="" android:layout_width="match_parent" android:layout_height="match_parent"> 

<TextView android:id="@+id/list_item_search" android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:textColor="@color/colorBlack"

We stated that our datasource is stored in a local SQLite database. I will not go into details about the database class but the code for classes involved with database connection and retrieval of data is shown below.

import android.content.Context;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class DatabaseConfig extends SQLiteAssetHelper { 

private static final String DATABASE_NAMES = "dictionary"; 
private static final int DATABASE_VERSION = 1; 

public DatabaseConfig(Context context) { 


 DatabaseInstance –

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class DbObject { 

private static DatabaseConfig dbHelper; 
private SQLiteDatabase db; 
public DbObject(Context context) { 
dbHelper = new DatabaseConfig(context); 
this.db = dbHelper.getReadableDatabase(); 


public SQLiteDatabase getDbConnection(){
 return this.db; 

public void closeDbConnection(){ 
if(this.db != null){


import android.content.Context;import android.database.Cursor;
import java.util.ArrayList;
import java.util.List;

public class DbBackend extends DbObject{ 

public DbBackend(Context context) {

public List<ItemObject> searchDictionaryWords(String searchWord){ 

List<ItemObject> mItems = new ArrayList<ItemObject>(); 
String query = "Select * from dictionary where word like " + "'%" + searchWord + "%'"; 
Cursor cursor = this.getDbConnection().rawQuery(query, null); ArrayList<String> wordTerms = new ArrayList<String>(); if(cursor.moveToFirst()){ 
 int id = cursor.getInt(0); 
String word = cursor.getString(cursor.getColumnIndexOrThrow("word")); 

mItems.add(new ItemObject(id, word)); }while(cursor.moveToNext()); 



return mItems; 

Since I made use of SQLiteAssetHelper third party library for the database handling, we will add this single line of code to our app build.gradle dependencies section as shown below.

compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'

Finally, this brings us to the end of this tutorial.

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email address so that you will be among the first to receive my new post once it is published.

Please if you love this tutorial, kindly download my new android app – Daily Calculator Tool – in Google Play Store and let me know what you think about it.


    • Henry

Add a Comment