Toast Notifications4:42 with Ben Deitch
In Android, a "Toast Notification" is a message that pops up on the screen for a limited time. They’re easy to use while developing to quickly check a value or something else about the app, as well as display non-essential information to the user. In this video we'll see how we can write one line of code to display some text in a Toast.
Things to Consider
Context - In Android, the Context is the current state of the application (or object). It's important because oftentimes an object or method needs to know about the state of the application to behave appropriately. It would be awfully hard to decide what to wear if you didn't know what country you we're in! In an Activity, the context can usually be represented using the this keyword. Since this refers to the current class (which is an Activity), and Activities are children (a subclass) of the Context class, that means that wherever we see a Context we can use an Activity instead.
Constants - In Java, a constant is a value that does not change. Using constants makes things easier to read and maintain and provides consistency when used in multiple places.
[MUSIC] 0:00 One of the most important skills you'll need when making Android apps is 0:04 the ability to troubleshoot when something goes wrong. 0:08 There are a number of ways to do this. 0:11 And we're going to look at three simple, yet 0:13 powerful ways to ask our apps, hey, what's going on? 0:15 What are you doing in there? 0:19 The first method we'll look at is a very simple pop up message called Toast. 0:21 No, not that kind, in Android, 0:25 a Toast is a small message that pops up on the screen for a short time. 0:27 It's just a quick informational message, that fades in and out automatically. 0:31 And it's only one line of code, so they're easy to use while developing to quickly 0:35 check a value or something else about your app. 0:39 They're also convenient to display to the user something that doesn't require their 0:42 input, and that won't cause any problems if the user doesn't see it. 0:46 That's the downside of using Toasts, 0:49 we might miss them if we look away from the phone for too long. 0:51 All right, [SOUND] let's take a look. 0:54 [SOUND] Let's start with a simple Toast that pops up when our Fun Facts activity 0:56 is first created. 1:00 But first, I'll hide the project for you to make things easier to see. 1:01 Then, let's head over to our FunFactsActivity class. 1:05 And in the onCreate method, let's add a line at the bottom. 1:09 And type Toast with a capital T, 1:14 and then use autocomplete to select the Create a new Toast option. 1:16 Then let's take a minute to talk about what's going on here. 1:23 I'll scroll down, so it's a bit easier to see. 1:26 Let's click on makeText, and then hit Ctrl+Q on Windows, or F1 on Mac, 1:29 to bring up the quick documentation. 1:34 Now we can see that makeText is a static method of the Toast class, 1:39 which is why we can call it directly from the class. 1:44 We also see that makeText returns a Toast object, 1:48 makeText also has three parameters. 1:51 A context, a CharSequence and a duration for how long this Toast will last. 1:54 For the context, we have this, we haven't talked about this yet, but 2:02 the this cue word refers to the current object. 2:06 In this case, it's referring to this instance of our FunFactsActivity class. 2:09 An activity is also a subclass of the context class. 2:15 So passing in an instance of our activity, using the this keyword, 2:19 is a good way to provide a context to this makeText method. 2:24 We'll often see the this keyword used to refer to the current context, and 2:28 we'll learn more about contexts in later videos. 2:32 The second parameter is a CharSequence for the text we'd like to show in our post. 2:35 Remember, a CharSequence is pretty much interchangeable with a string. 2:40 The third parameter is the duration, and 2:44 it can be one of two public constants from the Toast class. 2:46 Constant is just another word for a static final variable. 2:50 If you'd like to read more about constants, 2:53 there's a link in the teachers notes. 2:55 The two constants we can pick from are Toast.LENGTH_SHORT and Toast.LENGTH_LONG. 2:57 LENGTH_SHORT will show our toast for 3:03 two seconds and LENGTH_LONG will show it for three and half. 3:05 Constants like these provide a consistent user experience. 3:09 Users expect Toasts and other things to work the same way across all apps. 3:13 Restricting us to only two options for Toast duration ensures the Toasts 3:18 will have a consistent look and feel across all android apps. 3:22 All right, that red box around this 3:26 means that autocomplete is waiting on us to either change this value or accept it. 3:29 Let's hit Enter to accept it, now we're setting our Toast's message. 3:34 Let's type, "Yay! 3:40 Our Activity was created!", And hit Enter. 3:41 Our toast is now finished and ready to go. 3:48 But before we test it, let's quickly talk about this show method. 3:51 Calling show on a Toast object is how we display our Toasts. 3:55 Remember that the makeText method returns a Toast object, so adding .show 4:00 after the makeText method let's us immediately show the Toast we created. 4:04 We don't even need to create a variable to store our Toast, cool, right? 4:10 Let's run the app to see it in action. 4:15 And as soon as our app starts, we should be able to see our Toast. 4:18 Yay! 4:22 Our activity was created! 4:23 Awesome, we can add simple Toasts like this to any method 4:25 to tell us when that method gets called, or 4:28 they can show us the value of some variable that isn't visible on the screen. 4:31 Toasts are an easy way for us to check on our app while we're using it, or 4:35 to show our users a message in an unobtrusive manner. 4:39
You need to sign up for Treehouse in order to download course files.Sign up