उपयोगकर्ता को आपके मैसेज का जवाब देने की सुविधा देने के लिए, किसी Snackbar में कार्रवाई जोड़ी जा सकती है. ऐसा करने पर, Snackbar मैसेज के टेक्स्ट के बगल में एक बटन डालता है. उपयोगकर्ता, बटन पर टैप करके आपकी कार्रवाई को ट्रिगर कर सकता है. उदाहरण के लिए, कोई ईमेल ऐप्लिकेशन, "ईमेल संग्रहित किया गया" मैसेज पर पहले जैसा करें बटन डाल सकता है. अगर उपयोगकर्ता वापस करें बटन पर टैप करता है, तो ऐप्लिकेशन उस ईमेल को संग्रह से वापस ले लेता है.
पहला डायग्राम.Snackbar, जिसमें 'पहले जैसा करें' ऐक्शन बटन है. इसकी मदद से,
हटाए गए आइटम को वापस लाया जा सकता है.
Snackbar मैसेज में कोई कार्रवाई जोड़ने के लिए, ऐसा लिसनर ऑब्जेक्ट तय करें जो View.OnClickListener इंटरफ़ेस को लागू करता हो. अगर उपयोगकर्ता मैसेज ऐक्शन पर टैप करता है, तो सिस्टम आपके दर्शक के onClick()
तरीके को कॉल करता है. उदाहरण के लिए, यह स्निपेट, पहले जैसा करने की कार्रवाई के लिए एक Listener दिखाता है:
Kotlin
classMyUndoListener:View.OnClickListener{funonClick(v:View){// Code to undo the user's last action.}}
Java
publicclassMyUndoListenerimplementsView.OnClickListener{@OverridepublicvoidonClick(Viewv){// Code to undo the user's last action.}}
Snackbar से दर्शकों को जोड़ने के लिए, setAction() के इनमें से किसी एक तरीके का इस्तेमाल करें. show() को कॉल करने से पहले, लिसनर को अटैच करें, जैसा कि इस कोड सैंपल में दिखाया गया है:
इस पेज पर मौजूद कॉन्टेंट और कोड सैंपल कॉन्टेंट के लाइसेंस में बताए गए लाइसेंस के हिसाब से हैं. Java और OpenJDK, Oracle और/या इससे जुड़ी हुई कंपनियों के ट्रेडमार्क या रजिस्टर किए हुए ट्रेडमार्क हैं.
आखिरी बार 2025-07-27 (UTC) को अपडेट किया गया.
[[["समझने में आसान है","easyToUnderstand","thumb-up"],["मेरी समस्या हल हो गई","solvedMyProblem","thumb-up"],["अन्य","otherUp","thumb-up"]],[["वह जानकारी मौजूद नहीं है जो मुझे चाहिए","missingTheInformationINeed","thumb-down"],["बहुत मुश्किल है / बहुत सारे चरण हैं","tooComplicatedTooManySteps","thumb-down"],["पुराना","outOfDate","thumb-down"],["अनुवाद से जुड़ी समस्या","translationIssue","thumb-down"],["सैंपल / कोड से जुड़ी समस्या","samplesCodeIssue","thumb-down"],["अन्य","otherDown","thumb-down"]],["आखिरी बार 2025-07-27 (UTC) को अपडेट किया गया."],[],[],null,["# Add an action to a message\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add notifications in Compose. \n[Snackbar →](/develop/ui/compose/components/snackbar) \n\nYou can add an action to a\n[Snackbar](/reference/com/google/android/material/snackbar/Snackbar)\nto let the user respond to your message. When you do this, the\n`Snackbar` puts a button next to the message text, and the user can\ntrigger your action by tapping the button. For example, an email app might put\nan *undo* button on its \"email archived\" message. If the user taps the\n*undo* button, the app takes the email back out of the archive.\n**Figure 1.** A `Snackbar` with an undo action button that restores a removed item.\n\nTo add an action to a `Snackbar` message, define a listener object\nthat implements the\n[View.OnClickListener](/reference/android/view/View.OnClickListener)\ninterface. The system calls your listener's\n[onClick()](/reference/android/view/View.OnClickListener#onClick(android.view.View))\nmethod if the user taps the message action. For example, this snippet shows a\nlistener for an undo action: \n\n### Kotlin\n\n```kotlin\nclass MyUndoListener : View.OnClickListener {\n\n fun onClick(v: View) {\n // Code to undo the user's last action.\n }\n}\n```\n\n### Java\n\n```java\npublic class MyUndoListener implements View.OnClickListener {\n\n @Override\n public void onClick(View v) {\n\n // Code to undo the user's last action.\n }\n}\n```\n\nUse one of the\n[setAction()](/reference/com/google/android/material/snackbar/Snackbar#setAction(int, android.view.View.OnClickListener))\nmethods to attach the listener to your `Snackbar`. Attach the\nlistener before you call\n[show()](/reference/com/google/android/material/snackbar/BaseTransientBottomBar#show()),\nas shown in this code sample: \n\n### Kotlin\n\n```kotlin\nval mySnackbar = Snackbar.make(findViewById(R.id.myCoordinatorLayout),\n R.string.email_archived, Snackbar.LENGTH_SHORT)\nmySnackbar.setAction(R.string.undo_string, MyUndoListener())\nmySnackbar.show()\n```\n\n### Java\n\n```java\nSnackbar mySnackbar = Snackbar.make(findViewById(R.id.myCoordinatorLayout),\n R.string.email_archived, Snackbar.LENGTH_SHORT);\nmySnackbar.setAction(R.string.undo_string, new MyUndoListener());\nmySnackbar.show();\n```\nIf you are using [Jetpack Compose](/jetpack/compose), you can show a [SnackbarHost](/reference/kotlin/androidx/compose/material/package-summary#SnackbarHost(androidx.compose.material.SnackbarHostState,androidx.compose.ui.Modifier,kotlin.Function1)), as shown in the following example: \n\n### Kotlin\n\n```kotlin\n override fun onCreate(savedInstanceState: Bundle?) {\n\n super.onCreate(savedInstanceState)\n\n setContent {\n DACPlaygroundTheme {\n val snackbarHostState = remember { SnackbarHostState() }\n val scope = rememberCoroutineScope()\n Scaffold(\n snackbarHost = { SnackbarHost(snackbarHostState) },\n content = { padding -\u003e\n Button(\n modifier = Modifier.padding(padding),\n onClick = {\n scope.launch {\n snackbarHostState.showSnackbar(\n message = \"1 item removed\",\n actionLabel = \"UNDO\",\n duration = SnackbarDuration.Short\n ).run {\n when (this) {\n Dismissed -\u003e Log.d(\"SNACKBAR\", \"Dismissed\")\n ActionPerformed -\u003e Log.d(\"SNACKBAR\", \"UNDO CLICKED\")\n }\n }\n }\n }\n ) { Text(\"Show snackbar\") }\n }\n )\n }\n }\n }\n \n```\n| **Note:** A `Snackbar` automatically goes away after a short time, so the user might not see the message or have a chance to tap the button. For this reason, offer other ways to perform `Snackbar` actions."]]