Android AutoCompleteTextView Example

Posted by

Hello friends welcome to easytechsolutions A place for all your queries , So in today’s post we are going to implement autocompletetextview feature in android.

As you can see in many apps its also called auto filling like your type one letter and it will give suggestions to the matching words to make your writing less time consuming , This feature is mostly used in chat apps in which user got the auto filling words option whenever he/she type something.

So let’s get started implementing it through a simple app :-

Steps you need to do:-

Step 1:- Open up Android Studio and make a new project  and select Blank Activity to start with

Step 2:- Open up activity_main.xml and paste the below code in it :-

activity_main.xml

<?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.autocompletetextviewexample.MainActivity">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text"
        android:textSize="18sp"
        android:layout_marginTop="16dp"
        android:textColor="#000"
         />

    <AutoCompleteTextView
        android:id="@+id/auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:layout_marginTop="16dp"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:text=""
        />
</RelativeLayout>

So in above step we added AutoCompleteTextView in layout now we need to implement it in our Activity class for its working .

Step 3:- Open up MainActivity.java and paste below code in it:-

MainActivity.java

package com.autocompletetextviewexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import static android.graphics.Color.RED;

public class MainActivity extends AppCompatActivity {

    String[] languages = {"C","C++","Java","Python","Html","Css","MySql","Android","ASP.Net"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Making ArrayAdapter instance and pass three argument in it(Context,layout,array name):-
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item,languages);

        //FindViewById:-
        AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.auto);

        //Setting threshhold and adapter on AutoCompleteText instance:-
        autoCompleteTextView.setThreshold(1); //Start working from first letter
        autoCompleteTextView.setAdapter(adapter);
        autoCompleteTextView.setTextColor(RED);
    }
}

That’s it now just save all and run and you will see a TextView field now when you start typing word you will get suggestion by the matching ones make sure add more number of words in your array so that you can explore it more.

Facebook Page URL:-https://www.facebook.com/easytechsolutions.info/

Leave a Reply

Your email address will not be published. Required fields are marked *