C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Android

Implement auto complete in Android

In this article, we are going to use about auto complete text view and learn about auto complete in Android.
Submitted by Manu Jemini, on February 06, 2018

This example, simply shows how to implement auto-complete on a AutoCompleteTextView. To use this first we need to import a library in our java file from the widget package and ArrayAdapter to implement it on AutoCompleteTextView.

    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;

Now, we have to make a XML file to have the AutoCompleteTextView in center. Here we have to use <requestFocus /> inside the component. You can have this in your XML file by:

<AutoCompleteTextView
	android:id="@+id/autoCompleteTextView"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:ems="10"
	android:layout_marginTop="150dp"
	android:hint="AutoComplete TextView"
	android:layout_marginLeft="60dp">
	<requestFocus />
</AutoCompleteTextView>

In our java file, make an array of strings like:

 String[] Cellphone_companies={"Apple ","Samsung","Redmi","Lenovo","Vivo","Oppo"};

We have to set this array on AutoCompleteTextView by using a method called setAdapter.

    T.setAdapter(adapter);
    T.setThreshold(1);

In above code, T is an object and we are setting our adapter on it.

1) Java file:

package com.example.hp.myapplication;

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


public class MainActivity extends AppCompatActivity {
    AutoCompleteTextView T;
    String[] Cellphone_companies={"Apple ","Samsung","Redmi","Lenovo","Vivo","Oppo"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        T=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);

        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,Cellphone_companies);

        T.setAdapter(adapter);
        T.setThreshold(1);
    }
}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hp.myapplication.MainActivity">

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_marginTop="150dp"
        android:hint="AutoComplete TextView"
        android:layout_marginLeft="60dp">
        <requestFocus />
    </AutoCompleteTextView>

</RelativeLayout>

3) Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

Android - AutoCompleteView Example 1
Android - AutoCompleteView Example 2

Android - AutoCompleteView Example 3



Liked this article? Do share with your friends :)


COMMENTS