Android AutoCompleteTextView SetOnFocusChangeListener Example


Sound / system

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’s AutoCompleteTextView with an OnFocusChangeListener. This OnFocusChangeListener in Android AutoCompleteTextView is set by SetOnFocusChangeListener method of View class. In this Android AutoCompleteTextView SetOnFocusChangeListener example I would also explain why this approach is better than others.

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

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.

The layout should look something like this:

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 AutoCompleteTextView SetOnFocusChangeListener. 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 Android AutoCompleteTextView suggestion list. Now lets have a look at the class TruitonAutoCompleteTextViewActivity where the whole action is happening:

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

Android AutoCompleteTextView SetOnFocusChangeListener Example

Android AutoCompleteTextView

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

Android AutoCompleteTextView SetOnFocusChangeListener

Android AutoCompleteTextView Validation

Now if we tap back on the first Android AutoCompleteTextView then the validation error message would be shown:

Android AutoCompleteTextView SetOnFocusChangeListener

Android AutoCompleteTextView Validation Error Message

Now if we start typing again in Android AutoCompleteTextView and select the appropriate suggestion from the list as shown below the validation error message would be gone.

Android AutoCompleteTextView SetOnFocusChangeListener

Android AutoCompleteTextView SetOnFocusChangeListener

I hope this tutorial clears out how to force select item from list in case of Android AutoCompleteTextView. Also this tutorial clears out why Android AutoCompleteTextView SetOnFocusChangeListener approach is better than afterTextChanged() beforeTextChanged() onTextChanged() approach. If you found this tutorial interesting please like, +1, and share on Google+ and Facebook.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

Your email address will not be published. Required fields are marked *

5 thoughts on “Android AutoCompleteTextView SetOnFocusChangeListener Example