Android Adding Search Functionality to ListView
Adding search functionality to listview will filters the list data with a matching string, hence provides user an easy way to find the information he needs. In this tutorial i am discussing how to enable search filter to android ListView.
1. Create a new project in Eclipse File New ⇒ Android ⇒ Application Project and fill the required details.
2. Create required files needed to generate a listview. I am using my default activity_main.xml as listview and created a new xml file for single listitem named list_item.xml. Also make sure that you have created a EditText above the listview which will be used to search the listview.
activity_main.xml
list_item.xml
3. Now open your MainActivity.java and paste the following code to create a simple ListView. In the following code i stored all the list data in an array called products[] and attached to listview using simple ArrayAdapter.
Enabling Search Functionality
4. Search functionality can be enabled by writing simple lines of code. All you need to do is adding addTextChangedListener to EditText. Once user enters a new data in EditText we need to get the text from it and passing it to array adapter filter. All the following code in your MainActivity.java
5. Finally add the following property in your AndroidManifest.xml file to hide the keyboard on loading Activity.
AndroidManifest.xml
Final Code:
Hi there! I am Founder at androidhive and programming enthusiast. My skills includes Android, iOS, PHP, Ruby on Rails and lot more. If you have any idea that you would want me to develop? Let’s talk: [email protected]
Related Posts
Android Easy Runtime Permissions with Dexter
Android User Activity Recognition – Still, Walking, Running, Driving etc.,
Android Working with ButterKnife ViewBinding Library
Hi! when I do -ListView.setOnItemClickListener ….. it’s shows the old data from listview and not the data I was just filtered… how can I get the new data listView.
thank u for ur tutorials ravi..it helped me a lot in my project…
I need a help to implement the above code…In my case the content of list view is retrieved from databse(I refered your tutorial http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ for that).What i need is to update my list view content on a button click
the code I have written will create a new ListView with updated content on each button click and display it below the previous ListView.Which I need to correct.
hi.. i m a using this code but one problem occur actually i m a write the edit text they are filter the item but send the data before filtering..
i.e. list item:
12
13
14
15
filter item: 15
and 15 become no first and i m a click on no 15 that show the data of no 12 first position in list view please help i m a click on no 15 then show data on no 15 after filtering
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) <
// When user changed the Text
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
// TODO Auto-generated method stub
public void afterTextChanged(Editable arg0) <
// TODO Auto-generated method stub
public void onItemClick(AdapterView arg0, View v, int po,
Intent i=new Intent(List.this,VocabbAC.class);
Awesome tutorial… It has really helped me. @ravi! Please keep posting more stuff…
Hi guys I’d like to create two spinner widget, which will be connected to each other.
For example the first spinner will have the continents (America,europe,Asia etc.).
Supposed that someone choose Europe from the first spinner, then the
european countries to be displayed to the second spinner. Another
example is: if someone choose “America” for his first selection then the
American countries to be available to the second spinner. pls ravi help me out my id is [email protected]
Apply cold water to burned area..
Its incomplete code…..why have you not given code for getFilter() method?
Same problem for me..Have you found any solution ?
how to add an icon in the listview for each rows
I have same problem ! any idea
public void onItemClick(AdapterView parent, View view,
int position, long id) <
// String product = ((TextView) view).getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
My issue is im trying to get the searched items actual array position to display correct items on next activity how do I get that actual array position im stumped..
love it thank you!
Hi thanks a Lot for awesome tutorial!
I was wondering how to do it with the help of search widget as i suppose it does not have addTextChangedListener … Can you provide some hint .. I am really stuck .. I have implemented search widget in action bar btw…
Nice, Great Tut. but i was wondering how i can put another page in each item, example: when i click Dell Inspiron, it will go to dell inspiron page that has its definitions/descriptions
Thanks for your help
It is very simple, put switch cases in the listadapter ‘s onitemclick listener and in each switch cases, start an activity of your choice 😉
Edit :
Try this, worked for me:
@Override
public void onItemClick(AdapterView parent, View view,int position, long _id) <
String values = adapter.getItem(position);
// TODO Auto-generated method stub
Intent i =null;
if (values==”1,000 Hours”) <
i=new Intent(Allsongs.this, Thousandhours.class);
startActivity(i);>
if (values==”16″) <
i=new Intent(Allsongs.this, Sixteen.class);
startActivity(i);>
if (values==”2000 Light Years Away”) <
i=new Intent(Allsongs.this, Lightyears.class);
startActivity(i);>
if (values==”21 Guns”) <
i=new Intent(Allsongs.this, Guns.class);
startActivity(i);
>
Man, I cannot thank you @vishalnehra:disqus and @Ravi Tamada:disqus enough, you guys were really helpful, this was exactly what I needed! List + action to it’s values.
Pleasure to help you 🙂
I REALLY REALLY LIKE IT!!
but i have another question
i want to ,, when i click the word it will open new xml file how can i do that reply please
thanks for this great post ….
great information for me …
can please give a tutorial on how to attach search bar with database means if we search something than it will retrieve it from database and give the result..
How to get a video attached with a hashtag into a search bar for the user to serarch the video from there?
This is great. However, there is one thing that I don’t understand. The full search capability (letters that start in the middle), works sometimes and not others. For example, type s and you’ll see it properly finding all listitems that have s. Type e, and you don’t see the same result.
I assume you normally use
String product = products[position];
to determine which product was selected ?
If that’s the case just replace it by:
String product = adapter.getItem(position);
Hi.
How to add the search in Action Bar instead.
hello sir..
i have custom array adapter for listview…in array adapter bind two textview (id,name) that comes from databases…i want to implement search function on listview how to implement?
This is exactly what I want to know. Ravi please help us!
simply use this
@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) <
// TODO Auto-generated method stub
String products = adapter.getItem(position);
Intent i =null;
if (products==”Dell Inspironi”) <
i=new Intent(Product_Type.this, DellInspironi.class);
startActivity(i);>
if (products==”HTC One X”) <
i=new Intent(Product_Type.this, HTCOneX.class);
startActivity(i);>
if (products==”HTC Wildfire S”) <
i=new Intent(Product_Type.this, HTCWildfireS.class);
startActivity(i);>
>
I worked! Thank you very much 🙂
Hello Ravi, I am trying to implement this using a list that is inside a Fragment…
In other words my Fragment is visible and I want to make this filter connection between an EditText on my main Activity and my ListView within my Fragment. How can I do this? Ty
Can I use this code in Expandable list view ?
I have a custom ListView adapter. How do I achieve the above?
Prevent using android:inputType=”textVisiblePassword” in edittext
It prevents using swipe feature in soft keyboard!
hello, i not using array adapter, can after i implement your code, i having not accurate output
can you help me on this? below is my coding
lv = (ListView) findViewById(R.id.lvEditSavingList);
inputSearch = (EditText) findViewById(R.id.inputSearch);
loadListViewData();
Hello, I have a Question. Can I implement this code using an andorid Fragment in spite of Activity? Problem is the line ” MainActivity.this.adapter.getFilter().filter(cs); ” give me an error: NullPointerException.
I use all the code you have in my fragment activity, including
// ArrayList for Listview>
inputSearch = (EditText) view.findViewById(R.id.inputSearch);
public void onTextChanged(CharSequence cs, int start, int before, int count) <
// When user changed the Text
how to add details of that device so that if we click on that we should get details
woh! that reminds me again to check the documentation before going deeply in implementing anything. I was working on this from scratch unaware of the existence of such filtering API. yet this solution not retrieving the whole list that contains the ‘keyword’, instead it retrieves a list of the strings that start by the ‘keyword’. thank you so much!
I have a problem
Example:
String[] products = < “One”, “Two”,
“Three”, “Four”, “Five”, “Six”,
“Seven”,
Now I search for f
Then Four and five will come in list view
So position is changed. But I need position before list
view change. Because I will pass this position another activity. Then that activity
access a database by this position.
What Can I don now?
Send me code I will check out your issues than I vil rectify and provide you solution.
get an long ID instead of position
Thanks Ravi for the tutorial,
I am getting the error bellow when I type on the search textview
05-11 16:12:34.026: E/AndroidRuntime(9989): java.lang.NullPointerException
05-11 16:12:34.026: E/AndroidRuntime(9989): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
05-11 16:12:34.026: E/AndroidRuntime(9989): at android.widget.AdapterView.checkFocus(AdapterView.java:715)
05-11 16:12:34.026: E/AndroidRuntime(9989): at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:838)
05-11 16:12:34.026: E/AndroidRuntime(9989): at android.widget.AbsListView$AdapterDataSetObserver.onInvalidated(AbsListView.java:7622)
05-11 16:12:34.026: E/AndroidRuntime(9989): at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:50)
05-11 16:12:34.026: E/AndroidRuntime(9989): at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:59)
05-11 16:12:34.026: E/AndroidRuntime(9989): at android.widget.ArrayAdapter$ArrayFilter.publishResults(ArrayAdapter.java:514)
05-11 16:12:34.026: E/AndroidRuntime(9989): at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
05-11 16:12:34.026: E/AndroidRuntime(9989): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 16:12:34.026: E/AndroidRuntime(9989): at android.os.Looper.loop(Looper.java:137)
05-11 16:12:34.026: E/AndroidRuntime(9989): at android.app.ActivityThread.main(ActivityThread.java:5419)
05-11 16:12:34.026: E/AndroidRuntime(9989): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 16:12:34.026: E/AndroidRuntime(9989): at java.lang.reflect.Method.invoke(Method.java:525)
05-11 16:12:34.026: E/AndroidRuntime(9989): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
05-11 16:12:34.026: E/AndroidRuntime(9989): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
05-11 16:12:34.026: E/AndroidRuntime(9989): at dalvik.system.NativeStart.main(Native Method)
Bellow is my code
String products[] =db.productname();
final ListView lv = (ListView) dialog.findViewById(R.id.list_view);
final EditText inputSearch = (EditText) dialog.findViewById(R.id.inputSearch);
// Adding items to listview
adapter = new ArrayAdapter(getActivity(), R.layout.list_item, R.id.product_name, products);
public void onItemClick(AdapterView myAdapter, View myView, int myItemInt, long mylng) <
String selectedFromList =(String) (lv.getItemAtPosition(myItemInt));
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) <
// When user changed the Text
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
// TODO Auto-generated method stub
public void afterTextChanged(Editable arg0) <
// TODO Auto-generated method stub
how to do same thing with data coming from a link in json formate…please help me…n thanx in advance
Did you get to know how to read from a json file?
I’m late for party, what ever!!
sir how to make bold text on listview that i typed in EditText ( inputSearch in your case )??
Just go in xml file and use Style Tag for Bold and Italic.
Hi I loaded my datas with json from internet what I’m gonna change in variables
hii.. iwant to load data in searchable list view from sqlite data base but i can’t get it…any toutorial/help??thanks!!
i m having the same problem.. did u get any solution of this?
Creating a CustomBaseAdapter may help you, think so.
I have a adapter which accepts a list of (With a question and answer). the listview then inflates the custom list. If I use this code, what will be filtered? Everything in that list (Question and answer) or how could adapt it to search only on the question? Thanks so much!
hello bro & everyone
how i can adding ” Listview Header (Two or More) In my app “
Thanks for the tutorial, I thought it would be pretty hard =P
i need to implement search suggestions from server in my app?
Hi – did you find a solution?
i m lookingfor this help as well
yes i found it.. try with this link…
Thank you!
I’ve been looking for how to do this for 1 day!
hey thanks this was so so helpful !
thanks this was so so helpful !
It’s really awesome tutorial. thanks this was so helpful to my project:)
Hello Ravi awesome tutorial….but i was wondering how i can do the same thing but with a custom listview adapter tha implements imageview and more than one textviews…can u make a tutorial for this??
hi
i have a problem
search.java code is :
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class Contact extends Activity <
WebView webView=null;
AlertDialog alertDialog=null;
public void onCreate(Bundle savedInstanceState) <
Источник