0% found this document useful (0 votes)
58 views

AutoCompleteTextView Example

This document provides an example of implementing an Android AutoCompleteTextView. It demonstrates populating the AutoCompleteTextView with a string array of programming languages. The ArrayAdapter class is used to display the array contents in a dropdown for the user to select from. When the user enters a character, the AutoCompleteTextView will filter the options and display matching suggestions.

Uploaded by

Omkar Ghag
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

AutoCompleteTextView Example

This document provides an example of implementing an Android AutoCompleteTextView. It demonstrates populating the AutoCompleteTextView with a string array of programming languages. The ArrayAdapter class is used to display the array contents in a dropdown for the user to select from. When the user enters a character, the AutoCompleteTextView will filter the options and display matching suggestions.

Uploaded by

Omkar Ghag
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Android AutoCompleteTextView Example

Android AutoCompleteTextView completes the


word based on the reserved words, so no need to
write all the characters of the word.

Android AutoCompleteTextView is a editable text field,


it displays a list of suggestions in a drop down menu
from which user can select only one suggestion or
value.

Android AutoCompleteTextView is the subclass of


EditText class. The MultiAutoCompleteTextView is the
subclass of AutoCompleteTextView class.

Android AutoCompleteTextView Example

In this example, we are displaying the programming


languages in the autocompletetextview. All the
programming languages are stored in string array.
ArrayAdapter class is used to display the array
content.

Let's see the simple example of autocompletetextview


in android.

activity_main.xml

Drag the AutoCompleteTextView and TextView


from the pallete, now the activity_main.xml file
will like this:

File: activity_main.xml
1.<RelativeLayout xmlns:androclass="http://sc
hemas.android.com/apk/res/android"
2. xmlns:tools="http://schemas.android.com/to
ols"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. tools:context=".MainActivity" >
6.
7. <TextView
8. android:id="@+id/textView1"
9. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"

11. android:layout_alignParentLeft="true"
12. android:layout_alignParentTop="true"
13. android:layout_marginTop="15dp"
14. android:text="@string/what_is_your_fa
vourite_programming_language_" />
15.
16. <AutoCompleteTextView
17. android:id="@+id/autoCompleteTextVi
ew1"
18. android:layout_width="wrap_content"
19. android:layout_height="wrap_content"

20. android:layout_alignParentLeft="true"
21. android:layout_below="@+id/textView
1"
22. android:layout_marginLeft="36dp"
23. android:layout_marginTop="17dp"
24. android:ems="10"
25. android:text="">
26.
27. <requestFocus />
28. </AutoCompleteTextView>
29.
30. </RelativeLayout>

Activity class
Let's write the code of AutoCompleteTextView.

File: MainActivity.java

1. package com.example.autocompletetextview;
2.
3. import android.os.Bundle;
4. import android.app.Activity;
5. import android.graphics.Color;
6. import android.view.Menu;
7. import android.widget.ArrayAdapter;
8. import android.widget.AutoCompleteTextView;
9.
10.public class MainActivity extends Activity {
11. String[] language ={"C","C++","Java",".NET","iPhone","Android","ASP.NET
","PHP"};
12. @Override
13. protected void onCreate(Bundle savedInstanceState) {
14. super.onCreate(savedInstanceState);
15. setContentView(R.layout.activity_main);
16.
17. //Creating the instance of ArrayAdapter containing list of language name
s
18. ArrayAdapter<String> adapter = new ArrayAdapter<String>
19. (this,android.R.layout.select_dialog_item,language);
20. //Getting the instance of AutoCompleteTextView
21. AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.
id.autoCompleteTextView1);
22. actv.setThreshold(1);//will start working from first character
23. actv.setAdapter(adapter);//setting the adapter data into the AutoCom
pleteTextView
24. actv.setTextColor(Color.RED);
25.
26. }
27.
28. @Override
29. public boolean onCreateOptionsMenu(Menu menu) {
30. // Inflate the menu; this adds items to the action bar if it is present.
31. getMenuInflater().inflate(R.menu.activity_main, menu);
32. return true;
}

33.
34.}

Output:

You might also like