Android Tutorial : Example of AutoComplete Textview




Entering data in AutoComplete Textview

In this tutorial we will teach you about AutoComplete TextView in Android Application Development. How they are used and how they can be implemented in Android Application.

Don’t Miss :Android Tutorial – Example of Using CheckBox button

AutoComplete TextView– it is a type of TextView in Android i.e. this is a TextView which shows suggestions of completion while user is typing/entering the word/data.

Example code showing the use of AutoComplete Textview

In this example code we will make an android application which will show the suggestions for entered fruit name in editable text view.

In this example we use a one important function for implementation of AutoComplete TextView

setThreshold (int threshold) : this function work is to set when the suggestions drop down list will be shown. Like if threshold have value 2, then it will show suggestion list on entering minimum 2 words.

Now create a new project named as ‘AutoComplete’, give the name ‘com.youthgiri.autocomplete’ to your package, give the name ‘AutoCompleteActivity’ to your activity and give the name ‘activity_autocomplete’ to your layout.

Must see : Android Tutorial – Sending Data Using Intent

Edit your activity_autocomplete.xml

This is the layout for our project which displays AutoComplete Textview and user interface on the screen

<LinearLayout 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:orientation=”vertical”>

<TextView
android:id=”@+id/textView1″
android:layout_marginTop=”40dp”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Enter The Fruit Name ”
android:textAppearance=”?android:attr/textAppearanceMedium” />

<AutoCompleteTextView
android:id=”@+id/autoCompleteTextView1″
android:layout_marginTop=”15dp”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:hint=”Enter The Fruit” >

<requestFocus />
</AutoCompleteTextView>

<TextView
android:id=”@+id/textView2″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”15dp”
android:text=”You Entered :”
android:textAppearance=”?android:attr/textAppearanceMedium” />

<Button
android:id=”@+id/button1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”15dp”
android:layout_gravity=”center_horizontal”
android:text=” OK ”
/>

</LinearLayout>

 

Edit your AutoCompleteActivity.java

This is the java class of our project where implementation of our task is done using a function i.e. setThreshold (int threshold).

package com.youthgiri.autocomplete;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;

public class AutocompleteActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autocomplete);

String FRUITS[]={“APPLE”,”BANANA”,”ORANGE”,”GRAPES”,”WATERMELON”,”APRICOT”};

final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);

final TextView textViewSelectedCountry=(TextView)findViewById(R.id.textView2);
Button btnSelectedFruit=(Button)findViewById(R.id.button1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, FRUITS);

// Set the adapter
autoCompleteTextViewCountry.setAdapter(adapter);
autoCompleteTextViewCountry.setThreshold(1);

btnSelectedFruit.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub

String fruit=autoCompleteTextViewCountry.getText().toString();
textViewSelectedCountry.setText(“Selected Country: “+fruit);

}
});

}
}

Now the run application on emulator and it will be like this.

Start up screen
Start up screen
Entering data in AutoComplete Textview
Entering data in AutoComplete Textview
Selecting data from AutoComplete Textview
Selecting data from AutoComplete Textview

Hope you enjoyed this tutorial and gained information about Android Tutorial : Example of AutoComplete Textview.  For more details stay connected with us. Also, you can put your queries in Comment Box, we will respond quickly. 

Suggestions BY Google




Be the first to comment

Leave a Reply

Your email address will not be published.


*