{"id":729,"date":"2021-07-16T05:36:38","date_gmt":"2021-07-16T05:36:38","guid":{"rendered":"http:\/\/meatpie\/?p=729"},"modified":"2021-07-16T05:36:38","modified_gmt":"2021-07-16T05:36:38","slug":"age-calculator-autocomplete-field","status":"publish","type":"post","link":"https:\/\/snersbots.co.uk\/index.php\/age-calculator-autocomplete-field\/","title":{"rendered":"Age Calculator &#8211; AutoComplete Field"},"content":{"rendered":"\n<p>I don&#8217;t want to use a calendar selector. You&#8217;re always hunting backwards and forwards for the correct month. I decided it would be better to use an AutoComplete text field. The most you ever have to do is click 3 letters, but that is only if you are born in June or July.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>It is a bit fiddly, but very straight-forward. And I think it is very user friendly. Thanks to Sai Geetha&#8217;s Blog &#8211; Android &#8211; <a href=\"http:\/\/saigeethamn.blogspot.com\/2010\/05\/auto-complete-text-view-android.html\">Auto Complete Text View | Android Beginner Dev Tutorial.<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"368\" height=\"402\" src=\"http:\/\/meatpie\/wp-content\/uploads\/2021\/07\/Android-autocomplete-dropdown-2.jpg\" alt=\"Screenshot showing the dropdown in action if you type the letter J\" class=\"wp-image-734\" srcset=\"https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/07\/Android-autocomplete-dropdown-2.jpg 368w, https:\/\/snersbots.co.uk\/wp-content\/uploads\/2021\/07\/Android-autocomplete-dropdown-2-275x300.jpg 275w\" sizes=\"auto, (max-width: 368px) 100vw, 368px\" \/><\/figure>\n\n\n\n<p>In the layout you need an AutoCompleteTextField as follows. Basically it is identical to EditText &#8211; just has a different tag.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   <code> &lt;AutoCompleteTextView\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            android:id=\"@+id\/edit_month\" \n    \/><\/code><\/code><\/pre>\n\n\n\n<p>In the project, in the values folder, add a new resource file called months.xml. These are the values that it will use in the autocomplete.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n&lt;resources>\r\n    &lt;string-array name=\"months\">\r\n        &lt;item >January&lt;\/item>\r\n        &lt;item >February&lt;\/item>\r\n        &lt;item >March&lt;\/item>\r\n        &lt;item >April&lt;\/item>\r\n        &lt;item >May&lt;\/item>\r\n        &lt;item >June&lt;\/item>\r\n        &lt;item >July&lt;\/item>\r\n        &lt;item >August&lt;\/item>\r\n        &lt;item >September&lt;\/item>\r\n        &lt;item >October&lt;\/item>\r\n        &lt;item >November&lt;\/item>\r\n        &lt;item >December&lt;\/item>\r\n    &lt;\/string-array>\r\n&lt;\/resources><\/code><\/pre>\n\n\n\n<p>Then in ActivityMain you hook it all together.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Load the month names from your array in Resources<\/li><li>Put this array into a layout field adapter <ul><li>You could make your own XML for the layout, but I&#8217;m happy with simple_list_item_1<\/li><\/ul><\/li><li>Locate the the text field in the layout file <\/li><li>The threshold is how many letters you have to type before it will suggest anything <ul><li>In a short list like this, 1 is perfect &#8211; for a long list 3 will probably be better<\/li><\/ul><\/li><li>Add the adapter to the text field<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>        val monthNames =resources.getStringArray(R.array.months)\r\n        val adapter: ArrayAdapter&lt;String> =\r\n            ArrayAdapter&lt;String>(this, android.R.layout.simple_list_item_1, monthNames)\r\n\r\n        birthMonth = findViewById(R.id.edit_month) as AutoCompleteTextView\r\n        birthMonth!!.setThreshold(1)\r\n        birthMonth!!.setAdapter(adapter)\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t want to use a calendar selector. You&#8217;re always hunting backwards and forwards for the correct month. I decided it would be better to use an AutoComplete text field. The most you ever have to do is click 3 letters, but that is only if you are born in June or July.<\/p>\n","protected":false},"author":1,"featured_media":736,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,8],"tags":[48,49,50,35],"class_list":["post-729","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-software","tag-age-calculator","tag-android","tag-autocompletetextview","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=729"}],"version-history":[{"count":5,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/729\/revisions"}],"predecessor-version":[{"id":735,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/729\/revisions\/735"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media\/736"}],"wp:attachment":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}