Displaying Notification: Toast Class, displaying message on Toast

एक Toast एक छोटा message है जो थोड़ी देर के लिए pop up होता है और अपने आप fade हो जाता है, Toast.makeText(context, message, duration) से बनाया गया और .show() chain करके दिखाया गया, और वह .show() call भूल जाना ही वजह है beginners कुछ भी दिखते नहीं देखते।

7 min read · 9 cards · 2 checks

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


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 सही है?

  1. यह एक छोटा message दिखाता है जो auto-dismiss होता है; इसे display करने के लिए आपको show() call करना ज़रूरी है
  2. यह screen पर तब तक रहता है जब तक user इसे बंद करने के लिए एक button tap न करे
  3. यह status bar में एक permanent icon की तरह दिखता है
  4. यह 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 होता है और अपने आप निकल जाता है।

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 Android Widgets (UI)

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

Displaying Notification: Toast Class, displaying message on Toast · Mobile Application Development - 1 (option B) · Gri-Learn