How To Create AutoCompleteTextView

How To Create AutoCompleteTextView in Android Studio

The result will show as below screen.



Step 1 : layout have to create a AutoCompleteTextView with id "ac_student".
Layout Name : activity_main.xml


<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout    
   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=".MainActivity">

    <AutoCompleteTextView        
       android:id="@+id/ac_student"        
       android:layout_width="match_parent"       
       android:layout_height="wrap_content"        
       app:layout_constraintEnd_toEndOf="parent"        
       app:layout_constraintStart_toStartOf="parent"        
       app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


Step 2 : Java class add in following:
1. String arrays for "studentList" consist of studentA, studentB, studentC, studentD & studentE.
2. Create a new ArrayAdapter and assigned the "studentList".
3. Declare AutoCompleteTextView and find id from the activity_main.xml.
4. SetThreshold to 1 will start working from first character.


String[] studentList ={"studentA","studentB","studentC","studentD","studentE"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>
        (this,android.R.layout.select_dialog_item,studentList);

AutoCompleteTextView actv =  (AutoCompleteTextView)findViewById(R.id.ac_student);
actv.setThreshold(1);
actv.setAdapter(adapter);


THE END. HAPPY CODING. :)

No comments:

Post a Comment

Welcome To My Android Tutorial Lesson. Now this blogger cover : Android Studio Tutorial 1.  How To Start / Create a new project...