Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🎨 UI – Views और Layouts  ·  Lesson 17

Toast, Snackbar और Dialogs

कौन सा use करें?

ComponentUser को रोकता है?किसके लिए
Toastनहींछोटी confirmation: "Saved"
SnackbarनहींMessage + एक action: "Deleted · UNDO"
AlertDialogहाँज़रूरी फ़ैसला: "हमेशा के लिए delete?"
Bottom sheetथोड़ाकई options: sort, share, filters
9:41▮▮ 85%Notes⋮Physics notesChapter 4"Maths homework" deleted UNDO9:41▮▮ 85%Notes⋮Maths homeworkDue tomorrowDelete note?This cannot be undone.CANCELDELETE
Action वाला Snackbar और confirm dialog

Toast

Kotlin
Toast.makeText(this, "Note saved", Toast.LENGTH_SHORT).show()          // in an Activity
Toast.makeText(requireContext(), R.string.saved, Toast.LENGTH_LONG).show() // in a Fragment

Action वाला Snackbar

Kotlin
// From Student Diary – swipe to delete with UNDO
viewModel.delete(note)
Snackbar.make(binding.root, "\"${note.title}\" deleted", Snackbar.LENGTH_LONG)
    .setAction("Undo") { viewModel.restore(note) }
    .show()

// show it above a FAB / bottom navigation
Snackbar.make(binding.root, "No internet", Snackbar.LENGTH_INDEFINITE)
    .setAnchorView(binding.fab)
    .setAction("Retry") { loadData() }
    .show()

Confirm dialog

Kotlin
MaterialAlertDialogBuilder(this)
    .setTitle("Delete note?")
    .setMessage("\"Maths homework\" will be deleted permanently.")
    .setIcon(R.drawable.ic_delete)
    .setNegativeButton("Cancel", null)                        // just closes
    .setPositiveButton("Delete") { dialog, _ ->
        deleteNote()
        dialog.dismiss()
    }
    .show()
Delete tapDialogDelete note?Cancelकुछ नहीं होताDeletedeleteNote()
Destructive actions हमेशा confirm कराएँ

List और single-choice dialogs

Kotlin
// simple list
val actions = arrayOf("Edit", "Share", "Delete")
MaterialAlertDialogBuilder(this)
    .setTitle("Note options")
    .setItems(actions) { _, which ->
        Toast.makeText(this, actions[which], Toast.LENGTH_SHORT).show()
    }
    .show()

// single choice (radio buttons)
val themes = arrayOf("Light", "Dark", "System default")
var selected = 2
MaterialAlertDialogBuilder(this)
    .setTitle("Choose theme")
    .setSingleChoiceItems(themes, selected) { _, which -> selected = which }
    .setPositiveButton("Apply") { _, _ -> applyTheme(selected) }
    .setNegativeButton("Cancel", null)
    .show()

// multi choice
val subjects = arrayOf("Maths", "Physics", "Chemistry")
val checked = booleanArrayOf(true, false, false)
MaterialAlertDialogBuilder(this)
    .setTitle("Subjects")
    .setMultiChoiceItems(subjects, checked) { _, which, isChecked -> checked[which] = isChecked }
    .setPositiveButton("OK", null)
    .show()

Custom layout वाला dialog

XML
<!-- res/layout/dialog_rename.xml -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingHorizontal="24dp"
    android:paddingTop="8dp">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilName"
        style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Subject name">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.google.android.material.textfield.TextInputLayout>
</FrameLayout>
Kotlin
val dialogBinding = DialogRenameBinding.inflate(layoutInflater)
MaterialAlertDialogBuilder(this)
    .setTitle("Add subject")
    .setView(dialogBinding.root)
    .setNegativeButton("Cancel", null)
    .setPositiveButton("Add") { _, _ ->
        val name = dialogBinding.etName.text?.toString()?.trim().orEmpty()
        if (name.isNotEmpty()) addSubject(name)
    }
    .show()

Bottom sheet

Kotlin
// res/layout/sheet_sort.xml has three TextViews: tvNewest, tvPriority, tvTitle
val sheet = BottomSheetDialog(this)
val sheetBinding = SheetSortBinding.inflate(layoutInflater)
sheet.setContentView(sheetBinding.root)
sheetBinding.tvNewest.setOnClickListener { sortBy("newest"); sheet.dismiss() }
sheetBinding.tvPriority.setOnClickListener { sortBy("priority"); sheet.dismiss() }
sheetBinding.tvTitle.setOnClickListener { sortBy("title"); sheet.dismiss() }
sheet.show()
9:41▮▮ 85%Notes⋮Maths homeworkHighSort byNNewest firstPPriorityTTitle A–Z
तीन options वाला BottomSheetDialog

आम गलतियाँ

  • Toast/Snackbar/dialog पर .show() भूलना।
  • Dialogs के लिए applicationContext → crash "Unable to add window"। Activity (this) या requireContext() use करें।
  • हर छोटे message के लिए dialog → परेशान करता है; Snackbar को प्राथमिकता दें।

सारांश

  • Toast = छोटा message; Snackbar = message + action (Undo)।
  • Confirm, list, single और multi choice, या custom view के लिए MaterialAlertDialogBuilder।
  • नीचे से option menus के लिए BottomSheetDialog।
✅ Practice

List screen में item पर long-press से list dialog (Edit/Share/Delete) दिखाइए। Delete confirmation माँगे, फिर Undo वाला Snackbar दिखाए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

मेरा Toast क्यों नहीं दिख रहा?
शायद आप आख़िर में .show() भूल गए, या background thread से call कर रहे हैं। Toasts main thread पर ही दिखते हैं।
Snackbar या Toast?
App के अंदर Snackbar को प्राथमिकता दें: यह Material design follow करता है, bottom bar के ऊपर आता है और Undo जैसा action रख सकता है। जब कोई view न हो तब बहुत छोटे messages के लिए Toast।
Rotation पर मेरा dialog गायब क्यों हो जाता है?
Builder से बना dialog Activity instance से जुड़ा होता है। जिन dialogs को rotation में बचना चाहिए उनके लिए DialogFragment use करें।
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicToolbar, Options Menu और Popup Menu आपकी प्रगति: 17 / 43 (40%) · Lesson 18 / 43 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें