Site icon C1CTech

Android AutoCompleteTextview Example

<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>What is AutoComplete Textview&quest;<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>An <strong><span style&equals;"color&colon; &num;008000&semi;">editable<&sol;span><&sol;strong> text view that shows completion suggestions automatically while the user is typing&period; The list of suggestions is displayed in a drop-down menu from which the user can choose an item to replace the content of the edit box with&period;<&sol;p>&NewLine;<p>Get <span style&equals;"color&colon; &num;0000ff&semi;"><strong>GITHUB<&sol;strong><&sol;span> code from <span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;AutoCompleteTextviewExp"><strong>Here<&sol;strong><&sol;a><&sol;span>&period;<&sol;p>&NewLine;<p><span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"DYEtvC7merk" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;DYEtvC7merk" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;DYEtvC7merk&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;DYEtvC7merk&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span><&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Creating New Project<&sol;span><&sol;strong><&sol;h3>&NewLine;<ol>&NewLine;<li>In Android Studio&comma; go to <span style&equals;"color&colon; &num;008000&semi;"><strong>File <&sol;strong><strong>&Implies;<&sol;strong><strong> New Project<&sol;strong><&sol;span> and fill all the details required to create a new project&period; When it prompts to select a default activity&comma; select <span style&equals;"color&colon; &num;008000&semi;"><strong>Blank Activity<&sol;strong><&sol;span> and proceed&period;<&sol;li>&NewLine;<li>Open <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span> in which I have add  one<strong><span style&equals;"color&colon; &num;008000&semi;"> AutoCompleteTextview<&sol;span><&sol;strong> with following basic properties&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><span style&equals;"color&colon; &num;3366ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre><strong>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;margin&equals;"10dp"&NewLine; tools&colon;context&equals;"com&period;example&period;lenovo&period;autocompletetextview&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;country&lowbar;name"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"10dp"&NewLine; android&colon;text&equals;"Enter country name &colon;"&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; android&colon;textSize&equals;"20dp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;AutoCompleteTextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;ac&lowbar;tv"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;below&equals;"&commat;id&sol;tv&lowbar;country&lowbar;name"&NewLine; android&colon;layout&lowbar;marginTop&equals;"5dp" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;strong>&NewLine;&NewLine;<&sol;pre>&NewLine;<p style&equals;"padding-left&colon; 30px&semi;">3&period;  Now open  <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> and and add the below code&period; The following code snippet shows how to create a text view which suggests various countries names while the user is typing&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;3366ff&semi;"><strong>MainActivity&period;java<br &sol;>&NewLine;<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><strong>package com&period;example&period;lenovo&period;autocompletetextview&semi;&NewLine;&NewLine;import android&period;graphics&period;Color&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;widget&period;ArrayAdapter&semi;&NewLine;import android&period;widget&period;AutoCompleteTextView&semi;&NewLine;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; String&lbrack;&rsqb; country &equals; &lbrace;"Australia"&comma; "Albania"&comma; "Algeria"&comma; "Angola"&comma; "Belgium"&comma; "Bhutan"&comma; "Canada"&comma; "China"&comma; "India"&rcub;&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;We can use any of the built&lowbar;in android layout simple&lowbar;list&lowbar;item&lowbar;1 and select&lowbar;dialog&lowbar;item<&sol;span>&NewLine; ArrayAdapter&lt&semi;String&gt&semi; adapter &equals; new ArrayAdapter&lt&semi;String&gt&semi;&lpar;this&comma; android&period;R&period;layout&period;simple&lowbar;list&lowbar;item&lowbar;1&comma; country&rpar;&semi;&NewLine;&NewLine; AutoCompleteTextView ac&lowbar;tv &equals; &lpar;AutoCompleteTextView&rpar; findViewById&lpar;R&period;id&period;ac&lowbar;tv&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Start character for search<&sol;span>&NewLine; ac&lowbar;tv&period;setThreshold&lpar;1&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Setting adapter<&sol;span>&NewLine; ac&lowbar;tv&period;setAdapter&lpar;adapter&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Setting textcolor after selection<&sol;span>&NewLine; ac&lowbar;tv&period;setTextColor&lpar;Color&period;BLUE&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;strong><&sol;pre>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Now if you run the app you can see the book list will look like this&colon;<&sol;strong><&sol;span><&sol;h5>&NewLine;<p><img class&equals;"wp-image-171 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;Screenshot&lowbar;2018-03-06-18-07-441&period;png" alt&equals;"" width&equals;"443" height&equals;"738" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version