🎨 UI – Views और Layouts · Lesson 17
Toast, Snackbar और Dialogs
कौन सा use करें?
| Component | User को रोकता है? | किसके लिए |
|---|---|---|
| Toast | नहीं | छोटी confirmation: "Saved" |
| Snackbar | नहीं | Message + एक action: "Deleted · UNDO" |
| AlertDialog | हाँ | ज़रूरी फ़ैसला: "हमेशा के लिए delete?" |
| Bottom sheet | थोड़ा | कई options: sort, share, filters |
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()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()आम गलतियाँ
- 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 करें।
