Notification (Android)

A Simple App! (Part 3)

Posted on Updated on

Hi everyone!

Let’s continue from where we left! So, I used Toasts in the previous post. Were you able to figure out what this does? I hope so!

It is basically used to show notifications on the screen. If you are an Android User, you must have encountered them while using Messages or Contacts application. We had also seen the code for the same. Let’s revisit it!

Toast.makeText(getApplicationContext(), “Hello World!”,Toast.LENGTH_SHORT).show();

Simply, Toast.makeText(params).show() is the function which is used to display a Toast on the screen. It’s parameters are used to define it’s various properties.

The first parameter is used to pass the context to the function. Mostly, we will be using getApplicationContext() as the value of this parameter since this provides the context required to display the Toast on the screen in our app (We will be discussing it in detail later).

The second parameter defines the text to be displayed in the notification. Here, Hello World! will appear on the screen. A string type variable can also be used here!

The third parameter simply defines the time for which the notification will be displayed. It can have two values:

  1. Toast.LENGTH_SHORT: Used to display the notification for a short period of time.
  2. Toast.LENGTH_LONG: Used to display the notification for a long period of time.
Now, I think we have understood how to use Toasts in our application.

So, we have completed the development of our first app! Let’s look at all the steps again:

  • We had changed the value of the default TextView to Developing Android Applications with Nikhil Gupta in the post titled Laying Out the Screen!
  • We added a Button to the layout in the post titled A Simple App! (Part 1).
  • We discussed the use of ID and its usage as an attribute in the layout and as a reference in the JAVA code in the post titled A Simple App! (Part 2) .
  • We finally discussed the use of Toasts in this post.

We will start with the development of a new and interesting app in the next post!

Till then,  BYE!