{"id":996,"date":"2016-02-26T06:25:52","date_gmt":"2016-02-26T06:25:52","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=996"},"modified":"2019-06-12T06:31:17","modified_gmt":"2019-06-12T06:31:17","slug":"autocompletetextview","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/autocompletetextview","title":{"rendered":"AutoCompleteTextView Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, AutoCompleteTextView is a view i.e similar to <a href=\"\/ui\/edittext\">EditText<\/a>, except that it displays a list of completion suggestions automatically while the user is typing. A list of suggestions is displayed in drop down menu from which user can choose an item\u00a0which actually\u00a0replace the content of Editbox with that.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1059\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/AutoCompleteTextView-in-Android.jpg\" alt=\"AutoCompleteTextView in Android\" width=\"293\" height=\"190\" \/><\/center>It\u00a0is a subclass of EditText class\u00a0so we can inherit all the properties of <a href=\"\/ui\/edittext\">EditText<\/a>\u00a0in a AutoCompleteTextView.<\/p>\n<p><strong>AutoCompleteTextView code in XML:<\/strong><\/p>\n<pre>&lt;AutoCompleteTextView\r\nandroid:id=\"@+id\/simpleAutoCompleteTextView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:text=\"This is an AutoCompleteTextView\"\/&gt;\r\n<\/pre>\n<hr \/>\n<h4><strong>Using Array Adapter To Display Text Values In AutoCompleteTextView:<\/strong><\/h4>\n<p>To display the Array content in an autocompletetextview we need to implement Adapter. In AutoCompleteTextView we mainly display text values so we use Array Adapter for that.<\/p>\n<p><span style=\"color: #008000;\"><strong>ArrayAdapter In Android:<\/strong><\/span><\/p>\n<p>ArrayAdapter is used when we need\u00a0list of single type of items which is backed by an Array. For example, list of phone contacts, countries or names.<\/p>\n<p><span style=\"color: #008000;\"><strong>ArrayAdapter code:<\/strong><\/span><\/p>\n<pre>ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)\r\n<\/pre>\n<p>For more details on ArrayAdapter,parameter used and more details please read\u00a0<a href=\"\/ui\/arrayadapter-tutorial-example.html\">ArrayAdapter tutorial<\/a>.<\/p>\n<hr \/>\n<h4><strong>Retrieving the Value From AutoCompleteTextView In Java Class:<\/strong><\/h4>\n<p>Below code\u00a0retrieve the value from a AutoCompleteTextView in Java class.<\/p>\n<pre>AutoCompleteTextView simpleAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.simpleAutoCompleteTextView);\r\n\r\nString AutoCompleteTextViewValue = simpleAutoCompleteTextView.getText().toString();\r\n<\/pre>\n<hr \/>\n<h4><strong>Attributes of AutoCompleteTextView:<\/strong><\/h4>\n<p>Now let\u2019s \u00a0we discuss about the attributes that helps us to configure a AutoCompleteTextView in your xml file.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. id:<\/strong><\/span> id is an attribute used to uniquely identify a text AutoCompleteTextView.<\/p>\n<pre>&lt;AutoCompleteTextView\r\nandroid:id=\"@+id\/simpleAutoCompleteTextView\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_width=\"match_parent\"\/&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. text:<\/strong><\/span> text attribute is used to set the text in a AutoCompleteTextView. We can set the text in XML\u00a0as well as in the java class.<\/p>\n<p>Below we set the text \u201cCountry\u201d in a AutoCompleteTextView.<\/p>\n<pre>&lt;AutoCompleteTextView\r\n    android:id=\"@+id\/simpleAutoCompleteTextView\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Country\"\/&gt;&lt;!--display text \"Country\"--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1060\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/text-in-AutoCompleteTextView.jpg\" alt=\"text in AutoCompleteTextView\" width=\"223\" height=\"175\" \/><\/center><span style=\"color: #008000;\"><strong>3. gravity: <\/strong><\/span>The gravity attribute is an optional attribute which control the alignment of the text like left, right, center, top, bottom, center_vertical, center_horizontal etc.<\/p>\n<p>Below we set the right gravity for text of a AutoCompleteTextView.<\/p>\n<pre>&lt;AutoCompleteTextView\r\n    android:id=\"@+id\/simpleAutoCompleteTextView\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Country\"\r\n    android:gravity=\"right\"\/&gt;&lt;!--right gravity for text--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1061\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/gravity-in-AutoCompleteTextView-Android.jpg\" alt=\"gravity in AutoCompleteTextView Android\" width=\"264\" height=\"220\" \/><\/center><strong>Setting Text Of AutoCompleteTextView In Java class:<\/strong><\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nAutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView);\r\n\r\n\/\/display text Country\r\nautoCompleteTextView.setText(\"Country\");<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. hint:<\/strong><\/span> hint attribute gives the\u00a0hint to the user that what should he Enter in this AutoCompleteTextView. Whenever he\u00a0start to type in AutoCompleteTextView the hint will automatically disappear.<\/p>\n<pre>&lt;AutoCompleteTextView\r\nandroid:id=\"@+id\/simpleAutoCompleteTextView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:hint=\"Enter Your Country Name Here\" \/&gt;&lt;!--display hint--&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1062\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/hint-in-AutoCompleteTextView-in-Android.jpg\" alt=\"hint in AutoCompleteTextView in Android\" width=\"264\" height=\"197\" \/><\/center><strong>Setting hint For AutoCompleteTextView In Java class:<\/strong><\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nAutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView);\r\n\r\nautoCompleteTextView.setHint(\"Enter Your Name Here\");\/\/display hint<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. textColor:<\/strong><\/span>\u00a0This attribute set the color of a text in AutoCompleteTextView. Color value can be\u00a0in the form of &#8220;#argb&#8221;, &#8220;#rgb&#8221;, &#8220;#rrggbb&#8221;, or &#8220;#aarrggbb&#8221;.<\/p>\n<pre>&lt;AutoCompleteTextView\r\nandroid:id=\"@+id\/simpleAutoCompleteTextView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:text=\"Country\"\r\nandroid:textColor=\"#f00\"\/&gt;&lt;!--red color for text--&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1063\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textColor-in-AutoCompleteTextView-Android.jpg\" alt=\"textColor in AutoCompleteTextView Android\" width=\"261\" height=\"204\" \/><\/center><strong>Setting TextColor of AutoCompleteView Text In Java class:<\/strong><\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nAutoCompleteTextView simpleAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.simpleAutoCompleteTextView);\r\n\r\nsimpleAutoCompleteTextView.setTextColor(Color.RED);\/\/red color for text<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. textColorHint:<\/strong><\/span>\u00a0This attribute is used to set the color of displayed hint.<\/p>\n<pre>&lt;AutoCompleteTextView\r\nandroid:id=\"@+id\/simpleAutoCompleteTextView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:hint=\"Enter Your Name Here\"\r\nandroid:textColorHint=\"#0f0\"\/&gt;&lt;!--green color for hint--&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1064\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textColorHint-in-AutoCompleteTextView-Android.jpg\" alt=\"textColorHint in AutoCompleteTextView Android\" width=\"267\" height=\"203\" \/><\/center><strong>Setting Color to Hint Of AutoCompleteTextView\u00a0In Java class:<\/strong><\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nAutoCompleteTextView simpleAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.<em>simpleAutoCompleteTextView<\/em>);\r\n\r\n\/\/green color for displayed hint\r\nsimpleAutoCompleteTextView.setHintTextColor(Color.<em>green<\/em>(0));<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. textSize:<\/strong><\/span>\u00a0This attribute set the size of text in\u00a0AutoCompleteTextView. We can set the text size in sp(scale independent pixel) or dp(density pixel).<\/p>\n<pre>&lt;AutoCompleteTextView\r\nandroid:id=\"@+id\/simpleAutoCompleteTextView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:text=\"Country\"\r\nandroid:textSize=\"25sp\" \/&gt;&lt;!--set the text size--&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1065\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textSize-in-AutoCompleteTextView-Android.jpg\" alt=\"textSize in AutoCompleteTextView Android\" width=\"263\" height=\"214\" \/><\/center><strong>Setting Text Size In Java class:<\/strong><\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nAutoCompleteTextView simpleAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.<em>simpleAutoCompleteTextView<\/em>);\r\n\/\/set the text size\r\nsimpleAutoCompleteTextView.setTextSize(25);<\/pre>\n<p><span style=\"color: #008000;\"><strong>8. textStyle:<\/strong><\/span>\u00a0textStyle attribute is used to give\u00a0text style of a AutoCompleteTextView. We can add\u00a0bold, italic and normal style. If we want\u00a0to use two or more styles for a AutoCompleteTextView then \u201c|\u201d operator is used for that.<\/p>\n<pre>&lt;AutoCompleteTextView\r\n    android:id=\"@+id\/simpleAutoCompleteTextView\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Country\"\r\n    android:textSize=\"25sp\"\r\n    android:textStyle=\"bold|italic\"\/&gt;&lt;!--bold and italic text style--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1066\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textStyle-in-AutoCompleteTextView-Android.jpg\" alt=\"textStyle in AutoCompleteTextView Android\" width=\"265\" height=\"238\" \/><\/center><span style=\"color: #008000;\"><strong>9. background and padding:<\/strong><\/span> background attribute is used to set the background of a AutoCompleteTextView. We can set a color or a drawable in the background of a AutoCompleteTextView.<\/p>\n<p>padding attribute is used to set the padding from left, right, top or bottom.<\/p>\n<p>Below we set black color as\u00a0background, white color\u00a0as displayed hint and set 15dp padding from all the side\u2019s for AutoCompleteTextView.<\/p>\n<pre>&lt;AutoCompleteTextView\r\nandroid:id=\"@+id\/simpleAutoCompleteTextView\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:background=\"#000\"\r\nandroid:hint=\"Enter Your Name Here\"\r\nandroid:padding=\"15dp\"\r\nandroid:textColorHint=\"#fff\"\r\nandroid:textStyle=\"bold|italic\" \/&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1067\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Background-and-Padding-in-AutoCompleteTextView-Android.jpg\" alt=\"Background and Padding in AutoCompleteTextView Android\" width=\"266\" height=\"206\" \/><\/center><strong>Setting Background of AutoCompleteTextView In Java class:<\/strong><\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nAutoCompleteTextView simpleAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.<em>simpleAutoCompleteTextView<\/em>);\r\n\r\nsimpleAutoCompleteTextView.setBackgroundColor(Color.<em>BLACK<\/em>);\/\/set black background color\r\n<\/pre>\n<hr \/>\n<h4><strong>AutoCompleteTextView Example in Android Studio:<\/strong><\/h4>\n<p>In\u00a0the example of AutoCompleteTextView we display a auto complete text view with suggestion list which include\u00a0country list. To fill the data in country list we implement an Array Adapter. Below is the final output, download code and step by step tutorial:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/AutoCompleteTextView\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1068\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/AutoCompleteTextView-Example-in-Android-Studio.jpg\" alt=\"AutoCompleteTextView Example in Android Studio\" width=\"422\" height=\"309\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/AutoCompleteTextView-Example-in-Android-Studio.jpg 422w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/AutoCompleteTextView-Example-in-Android-Studio-300x220.jpg 300w\" sizes=\"auto, (max-width: 422px) 100vw, 422px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it AutoCompleteTextViewExample.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open res -&gt; layout -&gt; activity_mail.xml (or) main.xml\u00a0and add following code:<\/p>\n<p>In this step we open an xml file and add the code for displaying an auto complete text view by using different attributes as we discussed earlier in this article.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\"abhiandroid.com.autocompletetextviewtexting.MainActivity\"&gt;\r\n\r\n    &lt;AutoCompleteTextView\r\n        android:id=\"@+id\/simpleAutoCompleteTextView\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:background=\"#000\"\r\n        android:hint=\"Enter Your Name Here\"\r\n        android:padding=\"15dp\"\r\n        android:textColorHint=\"#fff\"\r\n        android:textStyle=\"bold|italic\" \/&gt;\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Open app\u00a0-&gt; package -&gt;\u00a0MainActivity.java<\/p>\n<p>In this step we open\u00a0MainActivity\u00a0where\u00a0we add the code to\u00a0initiate the auto complete text view and then fill the data in the suggestion list by using an <a href=\"\/ui\/arrayadapter-tutorial-example.html\">Array Adapter<\/a>.<\/p>\n<pre>package example.abhiandriod.autocompletetextviewexample;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.widget.ArrayAdapter;\r\nimport android.widget.AutoCompleteTextView;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    String[] countryNameList = {\"India\", \"China\", \"Australia\", \"New Zealand\", \"England\", \"Pakistan\"};\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/initiate an auto complete text view\r\n        AutoCompleteTextView simpleAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.simpleAutoCompleteTextView);\r\n        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, countryNameList);\r\n\r\n        simpleAutoCompleteTextView.setAdapter(adapter);\r\n        simpleAutoCompleteTextView.setThreshold(1);\/\/start searching from 1 character\r\n        simpleAutoCompleteTextView.setAdapter(adapter);   \/\/set the adapter for displaying country name list\r\n    }\r\n\r\n    \r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now start the AVD in Emulator and run the App. Type &#8216;i&#8217;\u00a0&amp; it will show &#8216;India&#8217; as suggestion, type &#8216;A&#8217; &amp; it will show &#8216;Australia&#8217; as suggestion and so on. This is done using AutoCompleteTextView.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1068\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/AutoCompleteTextView-Example-in-Android-Studio-300x220.jpg\" alt=\"AutoCompleteTextView Example in Android Studio\" width=\"300\" height=\"220\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/AutoCompleteTextView-Example-in-Android-Studio-300x220.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/AutoCompleteTextView-Example-in-Android-Studio.jpg 422w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, AutoCompleteTextView is a view i.e similar to EditText, except that it displays a list of completion suggestions automatically while the user is typing. A list of suggestions is displayed in drop down menu from which user can choose an item\u00a0which actually\u00a0replace the content of Editbox with that. It\u00a0is a subclass of EditText class\u00a0so &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/autocompletetextview\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">AutoCompleteTextView Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-996","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>AutoCompleteTextView Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn AutoCompleteTextView topic and how to use Adapter with example in Android Studio. In Android, AutoCompleteTextView is a view i.e similar to EditText, except that it displays a list of completion suggestions automatically while the user is typing.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/autocompletetextview\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=996"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/996\/revisions"}],"predecessor-version":[{"id":2775,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/996\/revisions\/2775"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}