autocompletetextview custom adapter

Android AutocompleteTextView with Custom Adapter Example

Before this Android AutocompleteTextView with customized adapter instance, we made the code on Android AutocompleteTextView with ideas from SQLite database, that’s actually effective and useful, however here’s another drawback:

What if you wish to customise the appearance of the drop-down strategies?

That is the place the customized ArrayAdapter will come to the scene. Using this publish, you will be able to customize the feel and appear of your AutocompleteTextView drop down – in line with your app design theme or visual wants.

We’ll cover the next content material: AutocompleteTextView with Custom Adapter Video Demo
2.0 Android AutoComplete EditText Program Information
three.0 Android AutocompleteTextView with Custom Adapter Example Codes
3.1 to three.eight Most important Information Description with Codes Defined by way of Comments

DOWNLOAD SOURCE CODE AutocompleteTextView with Custom Adapter Video Demo

Right here’s a video I shoot so you’ll be able to see the final output of our code for right now. Android AutoComplete EditText Program Information

You could click on every of the information under to see the code and some insights or explanations. File # 1 and a couple of have been found in your res/format/ folder, the remaining is in fact in the src/your.package deal.identify

  1. activity_main.xml
  2. list_view_row.xml
  8. Android AutocompleteTextView with Custom Adapter Example Codes

Now here’s the thrilling part. We’ll dig deeper with every essential information of the program, see the codes and a few rationalization. Take pleasure in!

three.1 activity_main.xml – see how our customized AutoCompleteTextView have been put within the XML format.

The tag seems like com.instance.autocompletetextviewdb.CustomAutoCompleteView as a result of it was referenced to our file.

3.2 list_view_row.xml – helps customise the appearance of AutoCompleteTextView recommendations, you’ll be able to add an ImageView or one thing suits your app theme or design.

three.3 – the place the program will attempt to insert pattern knowledge, initialize some variables and will probably be proven whenever you the program runs.

package deal com.example.autocompletetextviewcustomadapter;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Exercise

* Change to sort CustomAutoCompleteView as an alternative of AutoCompleteTextView
* since we’re extending to customise the view and disable filter
* The same with the XML view, sort might be CustomAutoCompleteView
CustomAutoCompleteView myAutoComplete;

// adapter for auto-complete
ArrayAdapter myAdapter;

// for database operations
DatabaseHandler databaseH;

protected void onCreate(Bundle savedInstanceState)



// instantiate database handler
databaseH = new DatabaseHandler(MainActivity.this);

// put pattern knowledge to database

// autocompletetextview is in activity_main.xml
myAutoComplete = (CustomAutoCompleteView) findViewById(;

myAutoComplete.setOnItemClickListener(new OnItemClickListener()

public void onItemClick(AdapterView mother or father, View arg1, int pos, long id)

RelativeLayout rl = (RelativeLayout) arg1;
TextView tv = (TextView) rl.getChildAt(0);


// add the listener so it is going to tries to recommend whereas the consumer varieties
myAutoComplete.addTextChangedListener(new CustomAutoCompleteTextChangedListener(this));

// ObjectItemData has no worth at first
MyObject[] ObjectItemData = new MyObject[0];

// set the customized ArrayAdapter
myAdapter = new AutocompleteCustomArrayAdapter(this, R.format.list_view_row_item, ObjectItemData);

catch (NullPointerException e)
catch (Exception e)

public void insertSampleData()

databaseH.create( new MyObject(“January”) );
databaseH.create( new MyObject(“February”) );
databaseH.create( new MyObject(“March”) );
databaseH.create( new MyObject(“April”) );
databaseH.create( new MyObject(“May”) );
databaseH.create( new MyObject(“June”) );
databaseH.create( new MyObject(“July”) );
databaseH.create( new MyObject(“August”) );
databaseH.create( new MyObject(“September”) );
databaseH.create( new MyObject(“October”) );
databaseH.create( new MyObject(“November”) );
databaseH.create( new MyObject(“December”) );
databaseH.create( new MyObject(“New Caledonia this is just to make and see if the text will go down”) );
databaseH.create( new MyObject(“New Zealand this is just to make and see if the text will go down”) );
databaseH.create( new MyObject(“Papua New Guinea this is just to make and see if the text will go down”) );
databaseH.create( new MyObject(“COFFEE-1K”) );
databaseH.create( new MyObject(“coffee raw”) );
databaseH.create( new MyObject(“authentic COFFEE”) );
databaseH.create( new MyObject(“k12-coffee”) );
databaseH.create( new MyObject(“view coffee”) );
databaseH.create( new MyObject(“Indian-coffee-two”) );

3.four – where you’ll be able to customize the appearance of the suggestion drop-down. If your options will include a big listing, I recommend utilizing a ViewHolder sample to make it clean.

package deal com.example.autocompletetextviewcustomadapter;

import android.content material.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class AutocompleteCustomArrayAdapter extends ArrayAdapter

remaining String TAG = “”;

Context mContext;
int layoutResourceId;
MyObject knowledge[] = null;

public AutocompleteCustomArrayAdapter(Context mContext, int layoutResourceId, MyObject[] knowledge)

tremendous(mContext, layoutResourceId, knowledge);

this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.knowledge = knowledge;

public View getView(int place, View convertView, ViewGroup mum or dad)


* The convertView argument is actually a “ScrapView” as described is Lucas publish
* It is going to have a non-null value when ListView is asking you recycle the row format.
* So, when convertView is just not null, it is best to merely update its contents as an alternative of inflating a new row format.
// inflate the format
LayoutInflater inflater = ((MainActivity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, father or mother, false);

// object merchandise based mostly on the place
MyObject objectItem = knowledge[position];

// get the TextView and then set the text (item identify) and tag (item ID) values
TextView textViewItem = (TextView) convertView.findViewById(;

// in case you need to add some fashion, you are able to do one thing like:

catch (NullPointerException e)
catch (Exception e)

return convertView;

3.5 – since we have now to show every thing the database will get, we’ve to show the AutoCompleteTextView’s default filtering perform. That is additionally what was referred to as in the XML format. See activity_main.xml code above.

package deal com.example.autocompletetextviewcustomadapter;

import android.content material.Context;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;

public class CustomAutoCompleteView extends AutoCompleteTextView

public CustomAutoCompleteView(Context context)
// TODO Auto-generated constructor stub

public CustomAutoCompleteView(Context context, AttributeSet attrs)
tremendous(context, attrs);
// TODO Auto-generated constructor stub

public CustomAutoCompleteView(Context context, AttributeSet attrs, int defStyle)
tremendous(context, attrs, defStyle);
// TODO Auto-generated constructor stub

// that is learn how to disable AutoCompleteTextView filter
protected void performFiltering(remaining CharSequence text, remaining int keyCode)
String filterText = “”;
super.performFiltering(filterText, keyCode);

three.6 – the place we insert pattern knowledge and question the database for autocomplete ideas.

package deal com.example.autocompletetextviewcustomadapter;

import android.content material.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper

// for our logs
public static remaining String TAG = “”;

// database model
personal static ultimate int DATABASE_VERSION = 5;

// database identify
protected static remaining String DATABASE_NAME = “NinjaDatabase2”;

// desk particulars
public String tableName = “locations”;
public String fieldObjectId = “id”;
public String fieldObjectName = “name”;

// constructor
public DatabaseHandler(Context context)
super(context, DATABASE_NAME, null, DATABASE_VERSION);

// creating table
public void onCreate(SQLiteDatabase db)

String sql = “”;

sql += “CREATE TABLE ” + tableName;
sql += ” ( “;
sql += fieldObjectName + ” TEXT “;
sql += ” ) “;


// When upgrading the database, it’ll drop the current table and recreate.
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

String sql = “DROP TABLE IF EXISTS ” + tableName;


// create new report
// @param myObj accommodates particulars to be added as single row.
public boolean create(MyObject myObj)

boolean createSuccessful = false;


SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(fieldObjectName, myObj.objectName);
createSuccessful = db.insert(tableName, null, values) > 0;


Log.e(TAG, myObj.objectName + ” created.”);

return createSuccessful;

// verify if a document exists so it gained’t insert the subsequent time you run this code
public boolean checkIfExists(String objectName)

boolean recordExists = false;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(“SELECT ” + fieldObjectId + ” FROM ” + tableName + ” WHERE ” + fieldObjectName + ” = ‘” + objectName + “‘”, null);


recordExists = true;


return recordExists;

// Learn data associated to the search term
public MyObject[] learn(String searchTerm)

// select query
String sql = “”;
sql += “SELECT * FROM ” + tableName;
sql += ” WHERE ” + fieldObjectName + ” LIKE ‘%” + searchTerm + “%’”;
sql += ” ORDER BY ” + fieldObjectId + ” DESC”;
sql += ” LIMIT 0,5″;

SQLiteDatabase db = this.getWritableDatabase();

// execute the query
Cursor cursor = db.rawQuery(sql, null);

int recCount = cursor.getCount();

MyObject[] ObjectItemData = new MyObject[recCount];
int x = zero;

// looping by means of all rows and adding to listing
if (cursor.moveToFirst())

String objectName = cursor.getString(cursor.getColumnIndex(fieldObjectName));
Log.e(TAG, “objectName: ” + objectName);

MyObject myObject = new MyObject(objectName);

ObjectItemData[x] = myObject;


while (cursor.moveToNext());


return ObjectItemData;

3.7 – each time the consumer varieties a character, it queries the database and updates our custom-made ArrayAdapter.

package deal com.instance.autocompletetextviewcustomadapter;

import android.content material.Context;
import android.text.Editable;
import android.textual content.TextWatcher;
import android.util.Log;

public class CustomAutoCompleteTextChangedListener implements TextWatcher

public static last String TAG = “”;
Context context;

public CustomAutoCompleteTextChangedListener(Context context)
this.context = context;

public void afterTextChanged(Editable s)
// TODO Auto-generated technique stub

public void beforeTextChanged(CharSequence s, int start, int rely,
int after)
// TODO Auto-generated technique stub

public void onTextChanged(CharSequence userInput, int start, int earlier than, int rely)


// if you wish to see in the logcat what the consumer varieties
Log.e(TAG, “User input: ” + userInput);

MainActivity mainActivity = ((MainActivity) context);

// replace the adapater

// get recommendations from the database
MyObject[] myObjs = mainActivity.databaseH.learn(userInput.toString());

// replace the adapter
mainActivity.myAdapter = new AutocompleteCustomArrayAdapter(mainActivity, R.format.list_view_row_item, myObjs);


catch (NullPointerException e)
catch (Exception e)

3.8 – used for the pattern knowledge to seem in the auto-complete suggestion.

package deal com.example.autocompletetextviewcustomadapter;

public class MyObject

public String objectName;

// constructor for including pattern knowledge
public MyObject(String objectName)

this.objectName = objectName;

I recommend studying more about Android AutocompleteTextView. Be happy to drop your questions or recommendations to improve this Android AutocompleteTextView with customized adapter example. Thanks!