Android - Simple Search List View

Submitted by: 
Operating System: 
Visitors have accessed this post 1773 times.

In this tutorial we will try to create a Simple Search List View using Android. This code can search any word through entering the keyword. Android is the world’s most widely used operating system with over a millions of user, that are already installed to smartphones, tablets, and even television. Android is an open source so that developer find it easy to develop and expand new features. So let's do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simplesearchlistview.MainActivity">
  11.  
  12. <EditText
  13. android:id="@+id/et_search"
  14. android:layout_width="wrap_content"
  15. android:layout_marginTop="10dp"
  16. android:layout_alignParentTop="true"
  17. android:layout_marginLeft="5dp"
  18. android:ems="10"
  19. android:layout_height="wrap_content"
  20. android:hint="Search here..."/>
  21.  
  22. android:id="@+id/lv_content"
  23. android:layout_height="wrap_content"
  24. android:layout_width="wrap_content"
  25. android:layout_below="@+id/et_search"/>
  26. </RelativeLayout>

After that we will create a layout that will display all the list. To do that right click on the layout select New then select resource file, and name it as list_content.xml. Write this certain xml code inside the newly created resource file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> android:orientation="vertical" android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6.  
  7. <TextView
  8. android:id="@+id/tv_list"
  9. android:layout_height="wrap_content"
  10. android:layout_width="wrap_content"
  11. android:padding="10dp"
  12. />
  13. </LinearLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simplesearchlistview">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>

The Main Function

This code contains the main function of the application. This code will automatically filter the list every time the keyword is entered. To start with first locate your MainActivity java file and open it, then write this variable inside the MainActivity class.

  1. String[] items;
  2. ArrayList<String> arrayList;
  3. ArrayAdapter<String> arrayAdapter;
  4. ListView lv_content;
  5. EditText et_search;

Then write these method to make to code work correctly.

  1. public void SearchResult(String keyword){
  2. for(String item:items){
  3. if(!item.contains(keyword)){
  4. arrayList.remove(item);
  5. }
  6. }
  7.  
  8. arrayAdapter.notifyDataSetChanged();
  9. }
  10.  
  11. public void IntializeList(){
  12. items = new String[] {
  13. "C",
  14. "C++",
  15. "C#",
  16. "Java",
  17. "VB",
  18. "Python",
  19. "Perl",
  20. "PHP",
  21. "Ruby",
  22. "Delphi"
  23. };
  24.  
  25. arrayList = new ArrayList<>(Arrays.asList(items));
  26. arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_content, R.id.tv_list, arrayList);
  27. arrayAdapter.sort(new Comparator<String>() {
  28. @Override
  29. public int compare(String lhs, String rhs) {
  30. return lhs.compareTo(rhs);
  31. }
  32. });
  33. lv_content.setAdapter(arrayAdapter);
  34. }

Finally, initialize the require methods inside the onCreate method to run the application.

  1. lv_content = (ListView)findViewById(R.id.lv_content);
  2. et_search = (EditText)findViewById(R.id.et_search);
  3.  
  4. IntializeList();
  5. et_search.addTextChangedListener(new TextWatcher() {
  6. @Override
  7. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  8.  
  9. }
  10.  
  11. @Override
  12. public void onTextChanged(CharSequence s, int start, int before, int count) {
  13. if(s.toString().equals("")){
  14. IntializeList();
  15. }else{
  16. IntializeList();
  17. SearchResult(s.toString());
  18. }
  19. }
  20.  
  21. @Override
  22. public void afterTextChanged(Editable s) {
  23.  
  24. }
  25. });
  26. <java>
  27. Try to run the app and see if it worked.
  28.  
  29. There you have it we have created a <strong>Simple Search List View</strong> using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.