Android - Simple Auto Complete List

Operating System

In this tutorial we will try to create a Simple Auto Complete using Android. This simple app predict the word that you will type and give you some list about the word that you want. Android is basically a piece of software which allows your hardware to function. It used in several gadget like smartphone, tablet, and even television. The android is an open source operating system it's free and user friendly to mobile developers. So let's now 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.simpleautocompletelist.MainActivity">
  8.  
  9.  
  10. <AutoCompleteTextView
  11. android:id="@+id/act_list"
  12. android:layout_alignParentStart="true"
  13. android:layout_alignParentLeft="true"
  14. android:layout_width="wrap_content"
  15. android:ems="14"
  16. android:layout_alignParentTop="true"
  17. android:layout_marginTop="80dp"
  18. android:layout_height="wrap_content"
  19. android:hint="Enter Here"/>
  20.  
  21. android:id="@+id/btn_search"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_marginTop="80dp"
  25. android:layout_alignParentEnd="true"
  26. android:layout_alignParentRight="true"
  27. android:layout_toRightOf="@+id/act_list"
  28. android:text="Search"/>
  29.  
  30.  
  31.  
  32. </RelativeLayout>

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.simpleautocompletelist">
  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 display a list that predict the letter that you enter. To start with first locate your MainActivity java file and open it, then write these some important variables inside the MainActivity class.
  1. private AutoCompleteTextView autoCompleteTextView;
  2. private Button btn_search;
  3.  
  4. String[] languages = {"C", "C++", "C#", "PHP", "Python", "Java", "BASIC", "Pascal", "Perl", "Ruby", "VB", "Scala"};
Then after that write these block of codes inside onCreate method, this code will start the app when run.
  1. btn_search = (Button)findViewById(R.id.btn_search);
  2. autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.act_list);
  3.  
  4. ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.select_dialog_item, languages);
  5. adapter.sort(new Comparator<String>() {
  6. @Override
  7. public int compare(String lhs, String rhs) {
  8. return lhs.compareTo(rhs);
  9. }
  10. });
  11. autoCompleteTextView.setThreshold(1);
  12. autoCompleteTextView.setAdapter(adapter);
  13.  
  14. btn_search.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View v) {
  17. if(!autoCompleteTextView.getText().toString().equals("")){
  18. String text = autoCompleteTextView.getText().toString();
  19. Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
  20. }
  21. }
  22. });
Try to run the app and see if it worked. There you have it we create a Simple Auto Complete List 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