AutoCompleteTextView, TextWatcher to EditText

બીજી pass, deeper: AutoCompleteTextView નું adapter + threshold machinery, અને TextWatcher ના 3 callbacks dissected: before, during અને after દરેક keystroke, mobile live search ને power કરતા.

12 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

તમે આ widget ને meet કર્યું છે; હવે તેના machinery ને meet કરો

syllabus તમને BCA305-02 ના acquaintance પાસે back મોકલે છે: AutoCompleteTextView અને TextWatcher એ FestConnect Mobile-1 ને close કર્યું. revisit એ rerun નથી: last year તમે તેમને WIRED કર્યું; this year તમે machinery ને explain કરી શકવા જોઈએ અને deeper questions ને answer કરી શકવા જોઈએ.

અને તમારી પાસે new context છે: BCA405-01 માં તમે web પર live search ને keyup અને AJAX સાથે build કર્યું. today નું lesson એ same pattern નું mobile twin છે, અને parallel એ બંને ને master કરવાનો fastest રસ્તો છે.

Theory

AutoCompleteTextView: recap, tightened

AutoCompleteTextView એ EditText subclass છે જે user types કરે તેમ filtered suggestions ને drop down કરે છે:

  • suggestions ArrayAdapter થી આવે છે (ફરીથી ListView lesson નો middleman), stock row simple_dropdown_item_1line
  • setThreshold(n): suggestions appear થાય તે પહેલાં કેટલા characters; default 2 છે, અને 1 ની નીચેની values ને 1 તરીકે treat કરવામાં આવે છે
  • તે suggests કરે છે, ક્યારેય restrict નથી કરતું: free text typeable રહે છે, તેથી validate getText() જો ફક્ત listed values legal હોય

એક suggestion દીઠ એક string = stock adapter; richer dropdown rows = custom adapter: ListView decision rule, verbatim.

Theory

TextWatcher: એક keystroke ના 3 moments

TextWatcher (જે addTextChangedListener સાથે attached થાય છે) દરેક text change ને 3 methods માંથી સાંભળે છે, strict order માં:

  • beforeTextChanged(s, start, count, after): OLD text, change થાય તેના instant પહેલાં: s હજુ "Garb" વાંચે છે
  • onTextChanged(s, start, before, count): NEW text, just landed: s "Garba" વાંચે છે: live-search moment
  • afterTextChanged(Editable s): last, અને એકમાત્ર જેને Editable handed થાય છે જેને તમે modify કરી શકો

મોટાભાગનું real work onTextChanged (react) અથવા afterTextChanged (edit) માં lives કરે છે; before એ old ને new સાથે compare કરવા માટે છે.

Practical

search box suggestions સાથે AND live counter

public class SearchActivity extends AppCompatActivity {
    String[] events = { "Garba Night", "Coding Contest", "Robo Race",
                        "Rangoli", "Quiz Bowl" };

    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_search);

        AutoCompleteTextView box = findViewById(R.id.searchBox);
        box.setAdapter(new ArrayAdapter<>(this,
                android.R.layout.simple_dropdown_item_1line, events));
        box.setThreshold(1);                    // letter 1 થી suggest કરો

        box.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) { }

            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                int hits = 0;                   // live results counter
                for (String e : events)
                    if (e.toLowerCase().contains(s.toString().toLowerCase()))
                        hits++;
                lblCount.setText(hits + " matching events");
            }

            public void afterTextChanged(Editable s) { }
        });
    }
}

Quiz

box "Garb" ને show કરે છે અને user a ને types કરે છે. કયા TextWatcher method માં s પ્રથમ "Garba" ને read કરે છે, જેને live search માટે right hook બનાવે છે?

  1. beforeTextChanged: તે first fires થાય છે, તેથી તેનામાં newest text છે
  2. onTextChanged: તે new text જેમ lands થાય તેમ fires થાય છે; beforeTextChanged હજુ "Garb" ને saw કર્યું હતું
  3. afterTextChanged: ફક્ત તે જ ever new text ને see કરે છે
  4. બધા 3 "Garba" ને receive કરે છે; ફક્ત તેમનું timing differs થાય છે
Show the answer

onTextChanged: તે new text જેમ lands થાય તેમ fires થાય છે; beforeTextChanged હજુ "Garb" ને saw કર્યું હતું

3 methods એ એક keystroke ને old, new અને settled માં split કરે છે: beforeTextChanged first fires થાય છે પણ pre-change text ને carry કરે છે ("Garb"): firing order અને text freshness એ different axes છે, જે exactly confusion છે જે આ question hunt કરે છે. onTextChanged new text ને deliver કરે છે જે moment તે lands થાય છે: live-search hook, જે role keyup એ BCA405-01 ના web version માં play કર્યો તે. afterTextChanged પણ "Garba" ને see કરે છે (option C ની half-truth) પણ last fires થાય છે અને EDITING માટે exists કરે છે તેના Editable દ્વારા, first reaction માટે નહીં.

Think first

watcher જે forever itself ને watch કરતું હતું

classmate typed text ને auto-capitalised કરવા માંગે છે, તેથી afterTextChanged ની અંદર તે s.replace(...) ને capitalised version સાથે call કરે છે: અને app freeze થાય છે. trace કરો કે શા માટે, using શું watcher watch કરે છે, પછી guard ને name કરો.

Show the answer

તેનું replace() text ને CHANGES કરે છે: જે text change છે: જે watcher ને ફરીથી fires કરે છે: જે ફરીથી replace કરે છે: infinite loop of self-triggered callbacks જે main thread ને freeze કરે છે. guard: edit ને conditional બનાવો જેથી already-capitalised text ને untouched leave કરવામાં આવે (જો first letter already uppercase હોય, તો return): પછી second pass કંઈ change નથી કરતું અને cycle dies થાય છે. general law: કોઈ પણ watcher જે તેને modify કરે છે જેને તે watch કરે છે તે idempotent હોવું જોઈએ: તેના own output પર running તે no-op હોવું જોઈએ. આ THE classic TextWatcher exam trap છે.

Watch out

Second-pass traps (deeper set)

બધા 3 methods હોવા જોઈએ: TextWatcher એ interface છે; implement even those જેને તમે empty leave કરો છો, અથવા તે compile નહીં થાય.

afterTextChanged ની બહાર Editing: ફક્ત તેનું Editable તમારું છે modify કરવા માટે; બાકીના 2 ના CharSequences read-only views છે.

Suggestion એ validation નથી: free text હજુ AutoCompleteTextView માં enter થાય છે; final value ને list સાથે check કરો જો તે match થવું જોઈએ.

Heavy work per keystroke: onTextChanged constantly fires થાય છે; slow code ત્યાં typing lag બનાવે છે: તેને light રાખો.

Theory

એક pattern, બે platforms

twins ને side by side set કરો: web live search = keyup event + AJAX + list ને repaint કરો; mobile = onTextChanged + filter + label ને update કરો. same architecture, different dialect: અને જ્યારે Flutter next unit માં arrive થાય, ત્યારે TextField નું onChanged એ SAME hook હશે ત્રીજી વાર. Unit 1 ની પાસે 2 widgets બાકી છે: image showmanship (ImageSlider, ImageSwitcher, SearchView) અને પછી tabbed layout જે આખા app ને reorganise કરે છે.

Summary

Key takeaways

  • AutoCompleteTextView = EditText + adapter-fed dropdown; setThreshold(n) gates કે જ્યારે suggestions appear થાય છે (minimum 1).
  • તે suggests કરે છે, ક્યારેય restrict નથી કરતું: validate getText() જ્યારે ફક્ત listed values legal હોય.
  • TextWatcher ના 3 moments: beforeTextChanged (old text), onTextChanged (new text: live-search hook), afterTextChanged (Editable, may modify).
  • Firing order અને text freshness differ થાય છે: before first fires થાય છે પણ old text ને see કરે છે.
  • Watchers જે તેમના own text ને edit કરે છે તે idempotent હોવા જોઈએ અથવા તેઓ forever loop થાય છે.
  • same pattern જેમ web live search (keyup + AJAX): એક architecture, બે platforms.
  • Memory hook: before old ને see કરે છે, on new ને see કરે છે, after may edit કરે છે, edits ને self-stop થવું જોઈએ.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Basic Attributes and Events of Important Android Widgets (UI)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

AutoCompleteTextView, TextWatcher to EditText · Mobile Application Development - 2 (option B) · Gri-Learn