Theory
Type 'Gan', see the rest
FestConnect's last field is 'College'. There are hundreds, and making students type the full name invites typos. Better: as they type 'Gan', a dropdown offers 'Ganpat University', tap it, done. And while they type any field, you might want a live character count or instant validation.
Two pieces do this: the AutoCompleteTextView (a text field with a suggestion dropdown) and the TextWatcher (code that runs on every keystroke). Together they make text input smart.
Theory
A search box that reads your mind
Think of a search bar that, after a couple of letters, drops a short list of likely completions, you rarely finish typing. That is the AutoCompleteTextView: it filters a list against what you have typed so far and offers matches. And imagine a helper watching over your shoulder counting characters as you type, that is the TextWatcher, reacting to each keystroke in real time.
Theory
Suggestions and keystroke events
AutoCompleteTextView:
- Feed suggestions with an ArrayAdapter (like a Spinner).
- `setThreshold(n)`: show suggestions after n typed characters (default 2).
- The user can still type free text; suggestions only assist.
TextWatcher (attach to any EditText with addTextChangedListener), three callbacks:
beforeTextChanged(...),onTextChanged(...),afterTextChanged(...).
Use them for live counts, validation, or enabling a button as the user types.
Practical
College autocomplete + live count
// AutoCompleteTextView: suggest colleges after 1 char
AutoCompleteTextView college = findViewById(R.id.college);
String[] list = {"Ganpat University", "MSU Baroda", "GTU"};
college.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, list));
college.setThreshold(1); // suggest after 1 character
// TextWatcher on the name field: live character count
EditText name = findViewById(R.id.name);
TextView counter = findViewById(R.id.counter);
name.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s,int a,int b,int c){}
public void onTextChanged(CharSequence s,int a,int b,int c){
counter.setText(s.length() + " chars"); // runs each keystroke
}
public void afterTextChanged(Editable s){}
});Think first
Autocomplete vs a Spinner
Both an AutoCompleteTextView and a Spinner let a user choose a college from a list. Give one reason to prefer autocomplete for a long list, and one thing autocomplete allows that a Spinner does not.
Show the answer
For a long list (hundreds of colleges), autocomplete is faster: the user types a few letters to filter instantly, instead of scrolling a huge Spinner dropdown. And autocomplete allows free text: a student whose college is not in the list can just type it, whereas a Spinner restricts them to the predefined options. So autocomplete = filter-as-you-type plus the freedom to enter something new; a Spinner = pick strictly from a fixed set.
Quiz
You want to update a character counter every time the user types in the name field. Which mechanism runs your code on each keystroke?
- A TextWatcher attached with addTextChangedListener
- The Spinner's setOnItemSelectedListener
- A Toast shown in onCreate
- The button's onClick handler
Show the answer
A TextWatcher attached with addTextChangedListener
A TextWatcher (added via addTextChangedListener) fires its callbacks on every text change, so it can update a live counter as the user types (A). A Spinner's item-selected listener reacts to dropdown choices, not typing (B). A Toast in onCreate runs once at startup (C), and an onClick only fires when a button is tapped, not per keystroke (D). For live reactions to typing, a TextWatcher is the tool.
Watch out
Autocomplete and TextWatcher traps
1. No adapter on the AutoCompleteTextView: no suggestions appear. It needs an ArrayAdapter, like a Spinner.
2. Threshold confusion: setThreshold(1) shows suggestions after one character; the default is 2. If suggestions 'do not show', the threshold may be higher than what was typed.
3. Heavy work in onTextChanged: it runs on every keystroke, so avoid slow operations there; keep the callback light.
Formula
FestConnect Mobile is complete
Trace the build: Unit 1 explained Android and set up the tools, Unit 2 created the app (Activity, layout, manifest), Unit 3 taught the XML those files are written in, and Units 4-5 assembled the registration screen widget by widget, EditText and Button with a Toast, a ToggleButton, CheckBoxes, RadioButtons, a Spinner with an AlertDialog, and an AutoCompleteTextView with a TextWatcher. In an exam, being able to name each widget's right use (one vs many, transient vs modal, dropdown vs autocomplete) is worth more than any single method name.
Summary
Key takeaways
- An AutoCompleteTextView is a text field that suggests matches from an adapter as you type; setThreshold(n) sets after how many characters.
- It filters a list but still allows free text, good for long lists (better than a Spinner there).
- A TextWatcher, attached with addTextChangedListener, runs code on every keystroke.
- Its three callbacks are beforeTextChanged, onTextChanged, afterTextChanged; keep them light.
- Use a TextWatcher for live counts, validation, or enabling a button as the user types.
- Memory hook: a search box that reads your mind, with a helper counting each keystroke.