Working with AutoCompleteTextView


In this article we are going to learn about "AutoCompleteTextView" and how to add suggestions for "AutoComplete" Textview . In AutoCompleteTextView, text field provide suggestions to user.This is one of the important feature of text field.

This is how our android application looks like;


Project Structure;


activity_main.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<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"
    tools:context="com.example.a.workingwithautocompletetextview.MainActivity">


    <AutoCompleteTextView
        android:id="@+id/names"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:hint="names"
        android:layout_marginTop="26dp" />
</RelativeLayout>


MainActivity.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;



public class MainActivity extends AppCompatActivity {

    AutoCompleteTextView autoCompleteTextView;
    String baby_names[]={"Aabheer","Aadarsh","Aadarsh","Aachman","Aadesh",
            "Aadi","Aadidev","Aadhishankar","Aadhunik","Aadinath"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        autoCompleteTextView = findViewById(R.id.names);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,baby_names);
        autoCompleteTextView.setAdapter(adapter);
    }
}

You can download app from here;


You can download full source code of this tutorial here;


SHARE

Androidstuff.net

Hai. I am one of the Authors of androidstuff.net. I am Android Developer, UI/UX Designer, Web Developer, Business Enthusiast, Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment