
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
How to set Adapter to Auto Complete Text view?
Before getting into an example, we should know what is autocomplete textview in android. Autocomplete textview is just like an edit text and it is a subclass of editext, but it is going to show suggestion from a list as a dropdown list. We have to set up Threshold value to auto-complete text view. for example, we have set it up Threshold as 1 so if user enters one letter is going to give suggestion according to Threshold letter.
This example demonstrates about how to set up an adapter to auto-complete Textview.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <AutoCompleteTextView android:id="@+id/autoComplete" android:layout_width="fill_parent" android:hint="Enter programming language" android:layout_height="wrap_content" /> </LinearLayout>
In the above we have declared autocomplete textview, when user enters a letter, it going to show list as suggestions in a drop-down menu.
Step 3 − Add the following code to src/MainActivity.java
package com.example.andy.myapplication; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.RadioButton; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { RadioButton radioButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final AutoCompleteTextView autoCompleteTextView=findViewById(R.id.autoComplete); ArrayList arrayList=new ArrayList<>(); arrayList.add("Android"); arrayList.add("JAVA"); arrayList.add("CPP"); arrayList.add("C Programming"); arrayList.add("Kotlin"); arrayList.add("CSS"); arrayList.add("HTML"); arrayList.add("PHP"); arrayList.add("Swift"); ArrayAdapter arrayAdapter=new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, arrayList); autoCompleteTextView.setAdapter(arrayAdapter); autoCompleteTextView.setThreshold(1); autoCompleteTextView.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.d("beforeTextChanged", String.valueOf(s)); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("onTextChanged", String.valueOf(s)); } @Override public void afterTextChanged(Editable s) { Log.d("afterTextChanged", String.valueOf(s)); } }); } }
In the above code, we have stored some values in ArrayList and appended ArrayList to array adapter. We have set the adapter to auto-complete textview and added threshold as 1. Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from an android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −
Initially, it going to show screen as above and enter ja in that textview it going to show result from adapter as shown below-
In the above result we have only one suggestion, Remove J and type c in that text view, it going to show multiple suggestions as shown below -
- Related Articles
- How to write a custom adapter for my list view on Android using Kotlin?
- How to set the part of the Android text view as clickable in Kotlin?
- Auto Complete TextBox and How to Create it using Android
- How to use concept () in Android text view?
- How to use contains () in Android text view?
- How to view the complete output of tibble in R?
- How to Set opacity for View in Android?
- How to Set opacity for View in iOS?
- How to set alignment to text in text flow layout?
- How to set auto-delete history in YouTube App?
- How to update RecyclerView Adapter Data?
- Auto-complete feature using Trie
- ImplementJavaScript Auto Complete / Suggestion feature
- How to set Web view Render Priority in android?
- How to set initial value and auto increment in MySQL?
