WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

AutoCompleteTextView Tutorial Android


AutoCompleteTextView is a view i.e similar to EditText.


Attributes AutoCompleteTextView

id id is an attribute used to unique identify a text AutoCompleteTextView.
<AutoCompleteTextView
android:id="@+id/demoAutoCompleteTextView"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>

text text attribute is used to set the text in a AutoCompleteTextView Below we set the text “Serach” in a AutoCompleteTextView.

<AutoCompleteTextView
    android:id="@+id/demoAutoCompleteTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Serach"/>
hint hint attribute gives the hint to the user that what should he Enter in this AutoCompleteTextView
<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Your search Name Here" />

set up java class
/*Add in Oncreate() funtion after setContentView()*/
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.demoAutoCompleteTextView);

autoCompleteTextView.setHint("Enter Your Name Here");//display hint

textColor text color is set color of a text in AutoCompleteTextView
<AutoCompleteTextView
android:id="@+id/demoAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="search"
android:textColor="#ff0"/> 
set up java class
/*Add in Oncreate() funtion after setContentView()*/
AutoCompleteTextView simpleAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView);

simpleAutoCompleteTextView.setTextColor(Color.BLUE);//blue color for text
textColorHintused to set the color of displayed hint.
<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Your Name Here"
android:textColorHint="#00f"/>

set up in java class
/*Add in Oncreate() funtion after setContentView()*/
AutoCompleteTextView demoAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.demoAutoCompleteTextView);

//blue color for displayed hint
simpleAutoCompleteTextView.setHintTextColor(Color.blue(0));/*Add in Oncreate() funtion after setContentView()*/
AutoCompleteTextView demoAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.demoAutoCompleteTextView);

//blue color for displayed hint
demoAutoCompleteTextView.setHintTextColor(Color.blue(0));
textSize set the size of text in AutoCompleteTextView.

<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="serach"
android:textSize="28sp" />
set up in java class
/*Add in Oncreate() funtion after setContentView()*/
AutoCompleteTextView simpleAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.demoAutoCompleteTextView);
//set the text size
demoAutoCompleteTextView.setTextSize(20);

AutoCompleteTextView Example

project and name it AutoCompleteTextViewExample. Open res -> layout -> activity_mail.xml (or) main.xml
<?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="abhiandroid.com.autocompletetextviewtexting.MainActivity">

 <AutoCompleteTextView
        android:id="@+id/simpleAutoCompleteTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#00f"
        android:hint="Enter Your Name Here"
        android:padding="15dp"
        android:textColorHint="#000"
        android:textStyle="bold|italic" />
</RelativeLayout>

Open app -> package -> MainActivity.java
package example.welookups.autocompletetextviewexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

    String[] serachNameList = {"Baidu", "Gigablast", "Google", "Yahoo", "Yandex" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //initiate an auto complete text view
        AutoCompleteTextView demoAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.demoAutoCompleteTextView);
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.demo_list_item_1, demoNameList);

        simpleAutoCompleteTextView.setAdapter(adapter);
        simpleAutoCompleteTextView.setThreshold(1);//start searching from 1 character
        simpleAutoCompleteTextView.setAdapter(adapter);   //set the adapter for displaying country name list
    }

    
}