Android - Simple Search List View
Submitted by razormist on Monday, February 19, 2018 - 19:00.
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...
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.
Finally, initialize the require methods inside the onCreate method to run the application.
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.- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.razormist.simplesearchlistview.MainActivity">
- <EditText
- android:id="@+id/et_search"
- android:layout_width="wrap_content"
- android:layout_marginTop="10dp"
- android:layout_alignParentTop="true"
- android:layout_marginLeft="5dp"
- android:ems="10"
- android:layout_height="wrap_content"
- android:hint="Search here..."/>
- android:id="@+id/lv_content"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:layout_below="@+id/et_search"/>
- </RelativeLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:id="@+id/tv_list"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:padding="10dp"
- />
- </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.- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.razormist.simplesearchlistview">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity"
- android:configChanges="orientation"
- android:screenOrientation="portrait">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </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.
Then write these method to make to code work correctly.
- if(!item.contains(keyword)){
- arrayList.remove(item);
- }
- }
- arrayAdapter.notifyDataSetChanged();
- }
- public void IntializeList(){
- "C",
- "C++",
- "C#",
- "Java",
- "VB",
- "Python",
- "Perl",
- "PHP",
- "Ruby",
- "Delphi"
- };
- arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_content, R.id.tv_list, arrayList);
- arrayAdapter.sort(new Comparator<String>() {
- @Override
- return lhs.compareTo(rhs);
- }
- });
- lv_content.setAdapter(arrayAdapter);
- }
- et_search = (EditText)findViewById(R.id.et_search);
- IntializeList();
- et_search.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- if(s.toString().equals("")){
- IntializeList();
- }else{
- IntializeList();
- }
- }
- @Override
- public void afterTextChanged(Editable s) {
- }
- });
- <java>
- Try to run the app and see if it worked.
- 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.
Add new comment
- 922 views