[MUSIC] In this lesson, we're going to talk about notifications. Notifications are basically messages, sometimes quite rich. That applications show to users outside the normal user interface of the application. For example, suppose you have an application that can download an e-book from over the internet. In that case, you probably want to let the user select a book to download. But then continue to use the application, or even exit it all together, while the book is downloading. And, if so, you'll probably also want to let the user know when the download finishes. And if it was successful. To do that, you need to do a few things, you need to figure out when the download finishes. And then you need to display some kind of message to give the user that information. In this lesson, I'll talk about two different kinds of user notifications that Android supports. The first kind are toast messages. Which, I suppose get their name from the fact that they pop up on the screen. We've actually already seen toast messages many times in previous lessons. For example, we've used them quite a bit to demonstrate that something was happening. Whenever we press the button. The second kind are notification area or status bar notifications. These notifications are visible and can be accessed in the system controlled notification area at the top of the device. And again, you've already seen examples of this. For instance, in the application fundamentals lesson, I showed how a device was receiving an incoming MMS message. And, you might remember, that when the MMS message actually arrived, an icon appeared at the top left of the phone. And the user could then pull down on the notification area to expose more information about that incoming MMS message. Okay, so let's recap this. Android provides several different kinds of user notifications. And these notifications are messages to the user that are sent on an as needed basis. And that appeared outside the normal user interface of the application. So, and edit text of you in which you type and mms message, or the button that you hit to send the resulting sms message. Both of which are always visible when you composes the message. Those are not user notifications. But a message that pops up a few seconds after you hit the send button, that tells you, for example, the message was sent successfully. Or an icon that appears at the top of your device, indicating that an MMS message has arrived. Even though you're currently reading a web page with the browser application. Those things are user notifications, and those are the things that we're going to talk about today. Now, some Android user notifications exist to provide feedback to the user about something that they're doing. And one such mechanism are the toasts messages that we talked about. Another are dialogues. Now we went over dialogs in quite a bit of detail during the lesson on user interface classes, so I'm not going to talk about dialogs in this lesson. Another kind of user notification are the notification area notifications. And these are typically used to announce unpredictable events to the user, and to do it in a way that doesn't interrupt. Whatever else they happen to be doing at that moment. And again, think of the incoming SMS message situation. Putting an icon in the notification area tells you that a message has arrived. But it doesn't do it in an obtrusive or peremptory way. The message is there, and you're free to deal with it. Whenever it's convenient for you. Toast messages, as you've seen, are temporary messages that pop up on the display. For instance, to let the user know that an operation has completed successfully. Toasts automatically fade in and fade out of view. And their job is to provide information to the user, but they're not meant to gather information that's going to get sent back to the hosting application. You can create toast messages using the toast classes make text method. That method takes a couple parameters, including the text that you want to display. And the amount of time over which you want that text to be visible. And after you've created the toast you can then display it by calling the show method on the toast you've just created. So let's take a look at an example application that uses toast messages. Here I'm starting up the notification Toast application. This application displays a single button labeled Show Toast, which is exactly what'll it do when I press it. So I'll press that button now. And there near the bottom of the screen you see a small pop up window that says, you're toast. Now if we open up the application source code in the IDE, we'll see how this is implemented. Here I'm highlighting the button listener for the show toast button. Inside, you see a call to the makeText method, passing in the text. And passing in the constant Toast.LENGTH_LONG, which ends up making the text visible for about three and a half seconds. And at the end of that line. There's a call to the show method that actually displays the toast. Now if you want to fancier toast message, you can also create a custom view for your toast. For example, you can create a custom layout in XML, inflate it. And attach the inflated view to the toast message with the call to the setView method. Let's see an example. And now I'll start up a notification toast with Custom View application. Like the simple toast example. This application displays one button labeled show Toast. And I can press that button to display the Toast message. In this case however, when I do press the button you'll see a custom Toast view rather than the simple gray pop-up that we saw before. So here we go. And there's your custom toast message with a text, your toast and an extra eyeball to help drive home the message. So let's open up the main activity for this application and look at how we created the toast message. Now as you can see when the show toast button is pressed, the code first creates a new toast object. The next two lines set the location of the toast on the display. And specify the length of time for which it will be visible. Then there's a call to set view, in which the first parameter is the result of inflating the XML layout that's in the custom underscore toast dot XML file. Let's open that file. Now here we can see that the custom view is a relativeLayout containing two children. The first child is the image view that holds the eyeball. The second child is a text view that displays the text, You're Toast! And then, back in the main activity, there's a final line that calls the show method to display the toast.