Theory
वह tap जिसने कुछ भी visible नहीं किया
पिछले lesson में, Register tap करना नाम पढ़ता था पर user को कुछ नहीं दिखाता था। उन्हें पता नहीं चलता यह काम कर गया। आप चाहते हैं एक जल्दी वाला 'Welcome, Aditi' flash हो और fade हो जाए।
Android में सबसे simple feedback Toast है: एक छोटा grey message जो bottom के पास दिखता है, एक-दो second रहता है, और अपने आप ग़ायब हो जाता है, कोई button नहीं, कोई interruption नहीं। यह लिखने की दो lines हैं, और उनमें से एक line है जो हर कोई भूल जाता है।
Theory
Toaster से bread
नाम literal है: जैसे toast एक toaster से pop होता है, message pop up होता है, थोड़ी देर बैठता है, और चला जाता है, आप इसे dismiss नहीं करते, यह अपने आप निकल जाता है। वह transience ही point है: एक Toast एक जल्दी वाला 'done!' या 'saved' के लिए है, किसी ऐसी चीज़ के लिए नहीं जो user को ध्यान से पढ़नी या act करनी चाहिए। उनके लिए आपको एक dialog चाहिए, जो बाद में आता है।
Theory
makeText, फिर show
दो steps:
- इसे बनाइए:
Toast.makeText(context, message, duration)। context: आमतौर पर Activity,this(या एक listener के अंदरMainActivity.this)।message: text (एक String)।duration:Toast.LENGTH_SHORT(लगभग 2s) याToast.LENGTH_LONG(लगभग 3.5s)।- इसे दिखाइए: `.show()` chain कीजिए।
.show() skip कीजिए और toast बनता है पर कभी display नहीं होता, सबसे common Toast mistake।
Practical
FestConnect tap confirm करता है
register.setOnClickListener(v -> {
String typed = name.getText().toString();
// build AND show (the .show() is essential)
Toast.makeText(
MainActivity.this,
"Welcome, " + typed,
Toast.LENGTH_SHORT
).show();
});
// Without .show(), makeText builds a Toast that never appears.
// Toast.LENGTH_LONG would keep it on screen a bit longer.Think first
कुछ नहीं दिखा। क्यों?
एक student onClick में Toast.makeText(this, "Hi", Toast.LENGTH_SHORT); लिखता है और कोई message कभी नहीं दिखता, कोई error भी नहीं। क्या एक चीज़ missing है?
Show the answer
`.show()` call। Toast.makeText(...) सिर्फ़ Toast object बनाता है; यह इसे display नहीं करता। .show() chain किए बिना, toast बनता है और तुरंत discard हो जाता है, silently, यही वजह है कोई error और कोई message नहीं। Fix है Toast.makeText(this, "Hi", Toast.LENGTH_SHORT).show();। यह number-one Toast bug है, तो show() को एक reflex बनाइए।
Quiz
एक Toast के बारे में कौन सा statement सही है?
- यह एक छोटा message दिखाता है जो auto-dismiss होता है; इसे display करने के लिए आपको show() call करना ज़रूरी है
- यह screen पर तब तक रहता है जब तक user इसे बंद करने के लिए एक button tap न करे
- यह status bar में एक permanent icon की तरह दिखता है
- यह dismiss होने तक app को block करता है
Show the answer
यह एक छोटा message दिखाता है जो auto-dismiss होता है; इसे display करने के लिए आपको show() call करना ज़रूरी है
एक Toast एक छोटा, non-interactive message दिखाता है जो अपने आप fade हो जाता है, और यह सिर्फ़ तभी दिखता है जब आप makeText के बाद show() call करें (A)। इसका कोई dismiss button नहीं है और यह user का इंतज़ार नहीं करता (वह एक AlertDialog है) (B)। यह एक persistent status-bar notification नहीं है (वह NotificationManager के ज़रिए एक Notification है) (C), और यह app को block नहीं करता (D)। Transient, non-blocking, show()-required: यही एक Toast है।
Watch out
Toast traps
1. .show() भूलना: makeText बनाता है, show() दिखाता है। कोई show() नहीं, कोई toast नहीं, और क्यों hint करने को कोई error नहीं।
2. Toast को Notification से confuse करना: एक Toast transient feedback है; एक status-bar Notification (NotificationManager के ज़रिए) persistent और interactive है। अलग classes, अलग uses।
3. मायने रखने वाले errors के लिए toasts का overuse: एक toast miss हो सकता है (यह fade होता है)। किसी ऐसी चीज़ के लिए जो user को acknowledge करनी ज़रूरी है, इसके बजाय एक AlertDialog इस्तेमाल कीजिए।
Theory
अगला: एक two-state button
FestConnect अब feedback देता है। अगला widget ToggleButton है: दो states वाला एक button, on और off, textOn/textOff labels के साथ और इसकी state पढ़ने और set करने के methods, एक 'Notifications: On/Off' setting के लिए handy। यह एक widget की state पढ़ना introduce करता है, जिस पर Unit 5 में CheckBox और RadioButton build करते हैं।
Summary
Key takeaways
- एक Toast एक छोटा, non-interactive message है जो pop up होता है और auto-dismiss होता है।
- Toast.makeText(context, message, duration) से बनाइए; context आमतौर पर Activity है (this)।
- इसे display करने के लिए आपको .show() chain करना ज़रूरी है (इसे भूलना top Toast bug है)।
- Duration Toast.LENGTH_SHORT (~2s) या Toast.LENGTH_LONG (~3.5s) है।
- एक Toast persistent status-bar Notification नहीं है और modal AlertDialog नहीं है।
- Memory hook: toaster से toast जैसा, यह pop up होता है और अपने आप निकल जाता है।