Android - Simple Search List View

Operating System
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="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="com.razormist.simplesearchlistview.MainActivity">
  8.  
  9.     <EditText
  10.         android:id="@+id/et_search"
  11.         android:layout_width="wrap_content"
  12.         android:layout_marginTop="10dp"
  13.         android:layout_alignParentTop="true"
  14.         android:layout_marginLeft="5dp"
  15.         android:ems="10"
  16.         android:layout_height="wrap_content"
  17.         android:hint="Search here..."/>
  18.  
  19.     <ListView
  20.         android:id="@+id/lv_content"
  21.         android:layout_height="wrap_content"
  22.         android:layout_width="wrap_content"
  23.         android:layout_below="@+id/et_search"/>
  24. </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="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="match_parent"
  4.     android:layout_height="match_parent">
  5.  
  6.     <TextView
  7.         android:id="@+id/tv_list"
  8.         android:layout_height="wrap_content"
  9.         android:layout_width="wrap_content"
  10.         android:padding="10dp"
  11.         />
  12. </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="http://schemas.android.com/apk/res/android"
  3.     package="com.razormist.simplesearchlistview">
  4.  
  5.     <application
  6.         android:allowBackup="true"
  7.         android:icon="@mipmap/ic_launcher"
  8.         android:label="@string/app_name"
  9.         android:roundIcon="@mipmap/ic_launcher_round"
  10.         android:supportsRtl="true"
  11.         android:theme="@style/AppTheme">
  12.         <activity android:name=".MainActivity"
  13.             android:configChanges="orientation"
  14.             android:screenOrientation="portrait">
  15.             <intent-filter>
  16.                 <action android:name="android.intent.action.MAIN" />
  17.  
  18.                 <category android:name="android.intent.category.LAUNCHER" />
  19.             </intent-filter>
  20.         </activity>
  21.     </application>
  22. </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!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Tags

Add new comment