• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Adding an Action to a Message
2page.tags="Snackbar" "action" "popup"
3helpoutsWidget=true
4trainingnavtop=true
5
6@jd:body
7
8<div id="tb-wrapper">
9  <div id="tb">
10
11<!--
12    <h2>This lesson teaches you to</h2>
13
14    <ol>
15      <li>
16        <a href="#id">heading</a>
17      </li>
18
19      <li>
20        <a href="#id">heading</a>
21      </li>
22    </ol>
23-->
24
25    <h2>See Also</h2>
26    <ul>
27      <li><a href="{@docRoot}guide/topics/ui/ui-events.html">
28          Input Events</a></li>
29    </ul>
30
31
32  </div>
33</div>
34
35<p>
36  You can add an action to a {@link android.support.design.widget.Snackbar},
37  allowing the user to respond to your message. If you add an action to a
38  {@link android.support.design.widget.Snackbar}, the
39  {@link android.support.design.widget.Snackbar} puts a button
40  next to the message text. The user can trigger your action by pressing the
41  button. For example, an email app might put an <em>undo</em> button on its
42  "email archived" message; if the user clicks the <em>undo</em> button, the
43  app takes the email back out of the archive.
44</p>
45
46<img src="{@docRoot}images/training/snackbar/snackbar_undo_action_2x.png"
47  srcset="{@docRoot}images/training/snackbar/snackbar_undo_action.png 1x,
48      {@docRoot}images/training/snackbar/snackbar_undo_action_2x.png 2x"
49  width="400" alt="">
50
51<p class="img-caption">
52  <strong>Figure 1.</strong> This Snackbar has an <strong>Undo</strong>
53  button, which restores the item that was just removed.
54</p>
55
56<p>
57  To add an action to a {@link android.support.design.widget.Snackbar} message,
58  you need to define a listener object that implements the {@link
59  android.view.View.OnClickListener} interface. The system calls your
60  listener's {@link android.view.View.OnClickListener#onClick onClick()} method
61  if the user clicks on the message action. For example, this snippet shows a
62  listener for an undo action:
63</p>
64
65<pre>public class MyUndoListener implements View.OnClickListener{
66
67    &amp;Override
68    public void onClick(View v) {
69
70        // Code to undo the user's last action
71    }
72}</pre>
73
74<p>
75  Use one of the
76  {@link android.support.design.widget.Snackbar#setAction(int, android.view.View.OnClickListener)
77  SetAction()} methods to attach the listener to your {@link
78  android.support.design.widget.Snackbar}. Be sure to attach the listener
79  before you call {@link android.support.design.widget.Snackbar#show show()},
80  as shown in this code sample:
81</p>
82
83<pre>Snackbar mySnackbar = Snackbar.make(findViewById(R.id.myCoordinatorLayout),
84                                R.string.email_archived, Snackbar.LENGTH_SHORT);
85<strong>mySnackbar.setAction(R.string.undo_string, new MyUndoListener());</strong>
86mySnackbar.show();</pre>
87
88<p class="note">
89  <strong>Note:</strong> A {@link android.support.design.widget.Snackbar}
90  automatically goes away after a short time, so you can't count on the user
91  seeing the message or having a chance to press the button. For this reason,
92  you should consider offering an alternate way to perform any {@link
93  android.support.design.widget.Snackbar} action.
94</p>
95