<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Restrict user to select from suggestions Archives - Truiton</title>
	<atom:link href="https://www.truiton.com/tag/restrict-user-to-select-from-suggestions/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Technology Reaching Us In Time - Online</description>
	<lastBuildDate>Mon, 25 Nov 2019 12:36:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://i0.wp.com/www.truiton.com/wp-content/uploads/2022/11/cropped-truiton_new_logo_half_white.png?fit=32%2C32&#038;ssl=1</url>
	<title>Restrict user to select from suggestions Archives - Truiton</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">54534495</site>	<item>
		<title>Android AutoCompleteTextView SetOnFocusChangeListener Example</title>
		<link>https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/</link>
					<comments>https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/#comments</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Sat, 13 Apr 2013 15:34:48 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[force select item from list]]></category>
		<category><![CDATA[Restrict user to select from suggestions]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Validation]]></category>
		<guid isPermaLink="false">http://blog.truiton.com/?p=188</guid>

					<description><![CDATA[<p>In every day programming we encounter new problems, and in search of solution to those we head to internet. While developing an app for Truiton on Android I also encountered a problem. I had a long list of countries from which I had to force user to select one. To solve this, I used Andorid&#8217;s&#8230;&#160;<a href="https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android AutoCompleteTextView SetOnFocusChangeListener Example</span></a></p>
<p>The post <a href="https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/">Android AutoCompleteTextView SetOnFocusChangeListener Example</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="640" height="481" data-attachment-id="205" data-permalink="https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/sound-system/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/2736400378_5bce7fc0d9_z.jpg?fit=640%2C481&amp;ssl=1" data-orig-size="640,481" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}" data-image-title="Sound / system" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/2736400378_5bce7fc0d9_z.jpg?fit=300%2C225&amp;ssl=1" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/2736400378_5bce7fc0d9_z.jpg?fit=640%2C481&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/2736400378_5bce7fc0d9_z.jpg?resize=640%2C481" alt="Sound / system" class="wp-image-205" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/2736400378_5bce7fc0d9_z.jpg?w=640&amp;ssl=1 640w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/2736400378_5bce7fc0d9_z.jpg?resize=300%2C225&amp;ssl=1 300w" sizes="(max-width: 640px) 100vw, 640px" /></figure></div>



<p></p>



<p>In every day programming we encounter new problems, and in search of solution to those we head to internet. While developing an app for <a title="Truiton" href="http://www.truiton.com" target="_blank" rel="noopener noreferrer">Truiton</a> on Android I also encountered a problem. I had a long list of countries from which I had to force user to select one. To solve this, I used Andorid&#8217;s <a title="Android AutoCompleteTextView" href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html" target="_blank" rel="noopener noreferrer"><em>AutoCompleteTextView</em></a> with an <a title="Android OnFocusChangeListener" href="http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html" target="_blank" rel="noopener noreferrer"><em>OnFocusChangeListene</em>r</a>. This <em>OnFocusChangeListener</em> in&nbsp;Android <em>AutoCompleteTextView</em> is set by <em>SetOnFocusChangeListener</em> method of <em>View</em> class. In this <a title="Android AutoCompleteTextView SetOnFocusChangeListener Example" href="http://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/">Android <em>AutoCompleteTextView SetOnFocusChangeListener</em> example</a> I would also explain why this approach is better than others.</p>



<p>If we have to force the user to select an item from <em>AutoCompleteTextView</em> suggestions list, another approach is also available. For that we need to utilize <em>addTextChangedListener</em> and <em>TextWatcher() </em>methods. But this approach has a major disadvantage, that all of its three methods <em>afterTextChanged, beforeTextChanged, onTextChanged</em> are invoked every-time there is a change in text. This clearly is a processing overhead therefore its not suitable for our scenario.<code></code></p>



<p></p>



<p>Coming back to our original Android AutoCompleteTextView SetOnFocusChangeListener Example, I believe this is the correct way to restrict a user, to select from suggestions list. So lets begin, In this example I am going to create a layout with two AutoCompleteTextViews.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".TruitonAutoCompleteTextViewActivity" >

    &lt;AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:hint="Select Country">
        &lt;requestFocus />
    &lt;/AutoCompleteTextView>

    &lt;ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:src="@drawable/truiton" />

    &lt;AutoCompleteTextView
        android:id="@+id/autoCompleteTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/autoCompleteTextView1"
        android:layout_below="@+id/autoCompleteTextView1"
        android:ems="10"
        android:hint="Select City" />

&lt;/RelativeLayout></pre>



<p>The layout should look something like this:</p>



<ul data-carousel-extra='{&quot;blog_id&quot;:1,&quot;permalink&quot;:&quot;https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/&quot;}'  class="wp-block-gallery columns-1 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex"><li class="blocks-gallery-item"><figure><img data-recalc-dims="1" decoding="async" width="332" height="589" data-attachment-id="194" data-permalink="https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/autocompletetextview1/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView1.png?fit=332%2C589&amp;ssl=1" data-orig-size="332,589" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}" data-image-title="" data-image-description="" data-image-caption="&lt;p&gt;Android AutoCompleteTextView SetOnFocusChangeListener&lt;/p&gt;
" data-medium-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView1.png?fit=169%2C300&amp;ssl=1" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView1.png?fit=332%2C589&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView1.png?resize=332%2C589&#038;ssl=1" alt="Android AutoCompleteTextView SetOnFocusChangeListener" data-id="194" data-link="https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/autocompletetextview1/" class="wp-image-194" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView1.png?w=332&amp;ssl=1 332w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView1.png?resize=169%2C300&amp;ssl=1 169w" sizes="(max-width: 332px) 100vw, 332px" /><figcaption>Android AutoCompleteTextView SetOnFocusChangeListener</figcaption></figure></li></ul>



<p></p>



<p>Now that we have our layout ready the question arises, How to verify if a user has selected a suggested value or entered a new one? Ans is Android <em>AutoCompleteTextView SetOnFocusChangeListener</em>. Our objective here is to create a validation which checks whether the entered value is in the suggestion list or not. If its not in our list, our validation would notify user and would keep restricting user from entering anything other than those values of&nbsp;Android <em>AutoCompleteTextView</em> suggestion list. Now lets have a look at the class <strong>TruitonAutoCompleteTextViewActivity</strong> where the whole action is happening:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.autocompletetextview;

import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class TruitonAutoCompleteTextViewActivity extends Activity {
 HashMap&lt;String, String> CountryData;
 HashMap&lt;String, String> CityData;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_truiton_auto_complete_text_view);
 CountryData = new HashMap&lt;String, String>();
 CountryData.put("India", "IN");
 CountryData.put("United States", "US");
 CountryData.put("United Kingdom", "GB");
 CountryData.put("Italy", "IT");

 CityData = new HashMap&lt;String, String>();
 CityData.put("City 1", "1");
 CityData.put("City 2", "2");
 CityData.put("City 3", "3");
 CityData.put("City 4", "4");

 setCountries(CountryData.keySet().toArray(new String[0]));
 }

 private void setCountries(String cData[]) {
 ArrayAdapter&lt;String> adapter = new ArrayAdapter&lt;String>(this,
 android.R.layout.simple_dropdown_item_1line, cData);
 final AutoCompleteTextView countryTextView = (AutoCompleteTextView) 
 findViewById(R.id.autoCompleteTextView1);
 countryTextView.setThreshold(2);
 countryTextView.setAdapter(adapter);
 countryTextView
 .setOnFocusChangeListener(new View.OnFocusChangeListener() {
 @Override
 public void onFocusChange(View view, boolean hasFocus) {
 if (!hasFocus) {
 String val = countryTextView.getText() + "";
 String code = CountryData.get(val);
 Log.v("TruitonAutoCompleteTextViewActivity",
 "Selected Country Code: " + code);
 if (code != null) {
 setCities(CityData.keySet().toArray(
 new String[0]));
 } else {
 countryTextView.setError("Invalid Country");
 }
 }
 }
 });
 }

 private void setCities(String cData[]) {
 ArrayAdapter&lt;String> adapter = new ArrayAdapter&lt;String>(this,
 android.R.layout.simple_dropdown_item_1line, cData);
 final AutoCompleteTextView cityTextView = (AutoCompleteTextView) 
 findViewById(R.id.autoCompleteTextView2);
 cityTextView.setThreshold(2);
 cityTextView.setAdapter(adapter);
 cityTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
 @Override
 public void onFocusChange(View view, boolean hasFocus) {
 if (!hasFocus) {
 String val = cityTextView.getText() + "";
 String code = CityData.get(val);
 Log.v("TruitonAutoCompleteTextViewActivity",
 "Selected City Code: " + code);
 if (code == null) {
 cityTextView.setError("Invalid City");
 }
 }
 }
 });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.truiton_auto_complete_text_view, menu);
 return true;
 }

}</pre>



<p>The logic applied in Android <em>AutoCompleteTextView SetOnFocusChangeListener</em> example is simple, all our values for country names are stored in CountryData <em>HashMap</em>. When user starts typing in Android <em>AutoCompleteTextView</em> suggestions appear like this:</p>



<div class="wp-block-image size-full wp-image-195"><figure class="aligncenter"><img data-recalc-dims="1" decoding="async" width="332" height="182" data-attachment-id="195" data-permalink="https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/autocompletetextview2/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView2.png?fit=332%2C182&amp;ssl=1" data-orig-size="332,182" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}" data-image-title="" data-image-description="" data-image-caption="&lt;p&gt;Android AutoCompleteTextView&lt;/p&gt;
" data-medium-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView2.png?fit=300%2C164&amp;ssl=1" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView2.png?fit=332%2C182&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView2.png?resize=332%2C182" alt="Android AutoCompleteTextView SetOnFocusChangeListener Example" class="wp-image-195" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView2.png?w=332&amp;ssl=1 332w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView2.png?resize=300%2C164&amp;ssl=1 300w" sizes="(max-width: 332px) 100vw, 332px" /><figcaption>Android AutoCompleteTextView</figcaption></figure></div>



<p>Now if focus is lost from <em>AutoCompleteTextView</em> then our <em>OnFocusChangeListener</em> comes into force unlike <em>TextWatcher</em> or <em>addTextChangedListener</em> which is always in force whenever we type a letter. Android&nbsp;<em>AutoCompleteTextView</em> validation is best implemented by&nbsp;<em>SetOnFocusChangeListener</em>. As the advantage of <em>OnFocusChangeListener</em> is that it runs when focus is changed, thus removing overhead processing. <em>AutoCompleteTextView</em> validation is shown like this :</p>



<div class="wp-block-image size-full wp-image-198"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="332" height="111" data-attachment-id="198" data-permalink="https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/autocompletetextview5/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView5.png?fit=332%2C111&amp;ssl=1" data-orig-size="332,111" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}" data-image-title="" data-image-description="" data-image-caption="&lt;p&gt;Android AutoCompleteTextView Validation&lt;/p&gt;
" data-medium-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView5.png?fit=300%2C100&amp;ssl=1" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView5.png?fit=332%2C111&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView5.png?resize=332%2C111" alt="Android AutoCompleteTextView SetOnFocusChangeListener" class="wp-image-198" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView5.png?w=332&amp;ssl=1 332w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView5.png?resize=300%2C100&amp;ssl=1 300w" sizes="auto, (max-width: 332px) 100vw, 332px" /><figcaption>Android AutoCompleteTextView Validation</figcaption></figure></div>



<p>Now if we tap back on the first Android <em>AutoCompleteTextView</em> then the validation error message would be shown:</p>



<div class="wp-block-image size-full wp-image-196"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="332" height="104" data-attachment-id="196" data-permalink="https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/autocompletetextview3/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView3.png?fit=332%2C104&amp;ssl=1" data-orig-size="332,104" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}" data-image-title="" data-image-description="" data-image-caption="&lt;p&gt;Android AutoCompleteTextView Validation Error Message&lt;/p&gt;
" data-medium-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView3.png?fit=300%2C93&amp;ssl=1" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView3.png?fit=332%2C104&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView3.png?resize=332%2C104" alt="Android AutoCompleteTextView SetOnFocusChangeListener" class="wp-image-196" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView3.png?w=332&amp;ssl=1 332w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView3.png?resize=300%2C93&amp;ssl=1 300w" sizes="auto, (max-width: 332px) 100vw, 332px" /><figcaption>Android AutoCompleteTextView Validation Error Message</figcaption></figure></div>



<p></p>



<p>Now if we start typing again in&nbsp;Android <em>AutoCompleteTextView</em> and select the appropriate suggestion from the list as shown below the validation error message would be gone.</p>



<div class="wp-block-image size-full wp-image-197"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="331" height="588" data-attachment-id="197" data-permalink="https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/autocompletetextview4/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView4.png?fit=331%2C588&amp;ssl=1" data-orig-size="331,588" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}" data-image-title="" data-image-description="" data-image-caption="&lt;p&gt;Android AutoCompleteTextView SetOnFocusChangeListener&lt;/p&gt;
" data-medium-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView4.png?fit=168%2C300&amp;ssl=1" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView4.png?fit=331%2C588&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2013/04/AutoCompleteTextView4.png?resize=331%2C588" alt="Android AutoCompleteTextView SetOnFocusChangeListener" class="wp-image-197"/><figcaption>Android AutoCompleteTextView SetOnFocusChangeListener</figcaption></figure></div>



<p></p>



<p>I hope this tutorial clears out how to force select item from list in case of Android <em>AutoCompleteTextView</em>. Also this tutorial clears out why Android <em>AutoCompleteTextView SetOnFocusChangeListener</em> approach is better than <em>afterTextChanged()</em> <em>beforeTextChanged()</em> <em>onTextChanged()</em> approach. If you found this tutorial interesting please like, +1, and share on Google+ and Facebook.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/">Android AutoCompleteTextView SetOnFocusChangeListener Example</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2013/04/android-autocompletetextview-setonfocuschangelistener-example/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">188</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: www.truiton.com @ 2026-04-17 09:56:12 by W3 Total Cache
-->