Tuesday, March 16, 2021

Android AutoComplete TextView

 

Android AutoCompleteTextView Example

android autocompletetextview

Android AutoCompleteTextView completes the word based on the reserved words, so no need to write all the characters of the word.

Android AutoCompleteTextView is a editable text field, it displays a list of suggestions in a drop down menu from which user can select only one suggestion or value.

Android AutoCompleteTextView is the subclass of EditText class. The MultiAutoCompleteTextView is the subclass of AutoCompleteTextView class.





Android AutoCompleteTextView Example

In this example, we are displaying the programming languages in the autocompletetextview. All the programming languages are stored in string array. We are using the ArrayAdapter class to display the array content.

Let's see the simple example of autocompletetextview in android.

activity_main.xml

Drag the AutoCompleteTextView and TextView from the pallete, now the activity_main.xml file will like this:

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout 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="example.javatpoint.com.autocompletetextview.MainActivity">  
  8.   
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="What is your favourite programming language?"  
  13.         app:layout_constraintBottom_toBottomOf="parent"  
  14.         app:layout_constraintLeft_toLeftOf="parent"  
  15.         app:layout_constraintRight_toRightOf="parent"  
  16.         app:layout_constraintTop_toTopOf="parent"  
  17.         app:layout_constraintVertical_bias="0.032" />  
  18.   
  19.     <AutoCompleteTextView  
  20.         android:id="@+id/autoCompleteTextView"  
  21.         android:layout_width="200dp"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_marginLeft="92dp"  
  24.         android:layout_marginTop="144dp"  
  25.         android:text=""  
  26.         app:layout_constraintStart_toStartOf="parent"  
  27.         app:layout_constraintTop_toTopOf="parent" />  
  28.   
  29. </android.support.constraint.ConstraintLayout>  

Activity class

Let's write the code of AutoCompleteTextView.

File: MainActivity.java
  1. package example.javatpoint.com.autocompletetextview;  
  2.   
  3. import android.graphics.Color;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.widget.ArrayAdapter;  
  7. import android.widget.AutoCompleteTextView;  
  8.   
  9. public class MainActivity extends AppCompatActivity {  
  10.     String[] language ={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP"};  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         //Creating the instance of ArrayAdapter containing list of language names  
  16.         ArrayAdapter<String> adapter = new ArrayAdapter<String>  
  17.                 (this,android.R.layout.select_dialog_item,language);  
  18.         //Getting the instance of AutoCompleteTextView  
  19.         AutoCompleteTextView actv =  (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);  
  20.         actv.setThreshold(1);//will start working from first character  
  21.         actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView  
  22.         actv.setTextColor(Color.RED);  
  23.     }  
  24. }  

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile