1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. User Interactions in Android App Development

User Interactions in Android App Development

Contents

keyboard_tab

The course is part of this learning path

Toast Messages in Android App Development
Overview
Difficulty
Intermediate
Duration
31m
Students
6
Description

This course covers the different types of messages that you can use in your Android app, namely toast messages, snackbar messages, and dialog messages.

Intended Audience

This course is intended for anyone who wants to learn how to start building their own apps on Android.

Prerequisites

To get the most out of this course, you should have some basic knowledge of the fundamentals of Android.

Resources

Course GitHub repo: https://github.com/OakAcademy/android-app-development-with-kotlin/tree/main/Section%205%20-%20User%20Interactions/UserInteractions 

Transcript

Hello everyone. So, I want to tell you about toast messages in this particular video. You know what a toast message is, right?

It is used to show a brief bit of information to the user. So, when the user clicks a button, or opens up a new window, or let's say maybe you want to give some information to the user how to proceed, you can always use a toast message. So, I want to go to android studio, and we'll have a little bit of practice before we get too far ahead of ourselves and lose any more time. Alright? So, here you can see I've created a new project for this particular lesson. Name of this project is User Interactions. So, you can also create a new project, continue the previous project, you know the drill. But before we get started, I do want to show you what this toast message is all about. So, if you see here, when I click on a button, there you get a toast message right, displays right here. And this message of course is practical. This is a toast message. So, after a while the toast message is gone. Now, you can show this toast message whenever you want. So, on display it, when the user clicks the button here. But of course, you could always display it when the user clicks an image, or perhaps download something. That would be entirely up to you.

Alright, so, let's see what we got. Here's how to do it. First of all, I will add a button to the design area, and also like the button from the palette, drag and drop it onto the design area. So, I am going to change the text of the button, and you can too, if you want. I am just going to write show toast message, and that goes in the text section. So, now we're going to determine the idea of the button. So, your idea of the button can be button show toast. Then finally, since our main layout is the constraint layout, we must also specify the constraint values of the button. So, here I'll just right-click the button with my mouse, and select center horizontally, and that way, it's centered horizontally. Alright? It is determined by the constraint values on the horizontal plane. So, let's go ahead and say that constraint value for the vertical plane. I will set the top constraint value to 75 and there you go. We're not going to really have to do anything else in this design area. We're going to do all of our necessary operation to create a toast message in the main activity .kt file. So, let's go ahead and open up the main activity .kt file.

Alright, so, first of all I got to create a variable, and the name of this variable is going to be well, I am just going to call it show toast. Now, after the colon, all right, button. Now, in the on create method, I will match the ID of the button with a variable that I created here. It's all right, showToast = findViewById (R.id.buttonShowToast). Cool. So, what I want is when the user clicks a show toast button, a toast message will be displayed. Alright, to click listener to the button. And I will write inside the on crate method, showToast.setOnClickListener. Right? I'll then write the code required to create the toast message between the curly brackets. Hopefully, you're right there with me or a step ahead. Right? You know this. So, write Toast.makeText. And after the make text function, I should write three parameters inside the parentheses. Right? First parameter is, context. You can write the application context as context, but you know what, let's take a moment to talk about context briefly here. Context really refers to a part of the activity, or the application that you're in. Alright? So, it's where you're at. So, by using the context parameter, you determine which activity will run the operation you want to do. Right?

Alright, so, context has three different uses. The first and pretty much the most comprehensive is the application context statement which I just gave you. So, it references the application directly. The other one is, to use the 'this' keyword. You might have seen me do that before, but you should be careful when you're using the 'this' keyword, because the 'this' keyword references the activity that the method is in. Alright? So, therefore using 'this' as your keyword directly in the method, well, you overwrite from different classes and it may throw up an error. So, what's the third one there? Reference the activity directly by typing this@mainActivity. But I'll tell you, what we're usually going to use the application context parameter for toast messages. But now you know the concept and usage. Alright? Let's go on. So, we're talking about parameters here, the second one is the message itself. So, I'll just write a message inside quotation marks. So, what I write inside the quotation marks, that's going to be shown as my message, Message can be, 'This is a toast message' for now. That's what I'm going to write. You can write whatever you want.

Alright so, the last parameter is how long will that toast message be shown? So, of course there are a couple options here, Toast.LENGTH_LONG and Toast.LENGTH_SHORT. Alright So, I'm going to select the first one to be easy, and after the parentheses, I am going to write .show. Follow Cool. So, that's it for that. A toast message is ready. So, let's run it and make sure it works as expected. The application opens. So, when I click on the button, toast message is displayed. Alright? Then after a short while, it is, Poof, gone, alright? That's enough of that. But I want to see you in the next video because we're going to continue.

 

About the Author
Students
170
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics