Toast Notifications4:11 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, and we're going to look at three simple, yet 0:11 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 a toast. 0:21 No, not that kind. 0:25 In Android, a toast is a small message that pops up on the screen for 0:27 a short time. 0:30 It's just a quick informational message that fades in and out automatically, and 0:32 it's only one line of code. 0:36 So they're easy to use while developing, to quickly check a value, or 0:38 something else about your app. 0:41 They're also convenient to display to the users something that doesn't require their 0:43 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:51 We might miss them if we look away from the phone for too long. 0:53 All right, let's take a look. 0:57 Let's start with a simple toast that pops up when 0:59 our FunFactsActivity is first created. 1:01 But first, let's collapse the mipmap folder, and 1:04 then hide the project pane to make everything a bit less cluttered. 1:07 Then let's head over to our FunFactsActivity class, and 1:12 in the on create method, let's add a line at the bottom and 1:17 then type toast with a capital T. 1:21 Hit Enter to import it, and then, .makeText, and 1:24 choose the version with the char sequence as the second parameter. 1:27 makeText has three parameters, a Context, a CharSequence, and 1:33 a duration for how long the toast will last. 1:37 For the context, let's type this. 1:40 We haven't talked about this yet. 1:44 But the this keyword refers to the current object. 1:46 In this case, it's referring to this instance of our FunFactsActivity class. 1:49 An activity is also a subclass of the context class. 1:54 So passing in the instance of our activity, using the this keyword, 1:58 is a good way to provide a context to this makeText method. 2:02 We'll often see the keyword this used to refer to the current context, 2:06 and we'll learn more about contexts in later videos. 2:10 The second parameter is a CharSequence for the text we'd like to show in our toast. 2:13 Remember, a CharSequence is interchangeable with the string. 2:18 So for our toast message, let's type in Yay! 2:22 Our activity was created. 2:26 Finally, the third parameter is the duration, and 2:32 it can be one of two public constants from the Toast class. 2:35 Constant is just another word for a static final variable. 2:40 If you'd like to read more about constants, 2:43 there's a short link in the teacher's notes. 2:46 The two constants we can pick from are toast.LENGTH_SHORT and toast.LENGTH_LONG. 2:48 LENGTH_SHORT will show our toast for two seconds, and LENGTH_LONG will show it for 2:54 three and half. 2:59 Constants like these provide a consistent user experience. 3:00 User expect toast and other things to work the same way across all apps, 3:04 restricting us to only two options for toast duration, 3:08 ensures that toast will have a consistent looking field across all Android apps. 3:11 All right, let's try that Toast.LENGTH_LONG for 3:17 the duration, and there we go. 3:22 Our toast is now finished and ready to go. 3:25 But before we test it, there's just one more thing we need to do. 3:27 The makeText method is only used to create toasts. 3:31 It doesn't show them. 3:34 So if we want to see our Toast, we'll need to call the show method on it, .show(). 3:35 Nice. 3:42 Let's run the app to see it in action. 3:42 And as soon as our app starts, we should see a toast. 3:47 Yay, our activity was created, awesome. 3:51 We can add simple toasts like this to any method, 3:54 to tell us when that method gets called. 3:57 Or they can show us the value of some variable that isn't visible on the screen. 3:59 Toasts are an easy way for us to check on our app while we're using it, or 4:04 to show our users a message in an unobtrusive manner. 4:07
You need to sign up for Treehouse in order to download course files.Sign up