Lesson 9: Simple Notifications using the Toast class and example

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

Android allows you to display a temporary pop-up window that informs the user via a message that appears on the screen for a small amount of time (after a while the window disappears).

This window that overlays the interface that is being displayed at the moment is managed by a class called Toast.

Create a project called Project008.

Issue:

Generate a random number between 1 and 100000. Show the number by a pop-up window for a period of time. Then through an EditText (“Number”) control, ask the operator to enter the number you saw on the screen. When a button is pressed, control the randomly generated number with which the user entered and using another Toast to tell if he was right or not.

The first thing we do is create the following interface:

Lesson 9: Simple Notifications using the Toast class and example - Lesson 9: Simple Notifications using the Toast class and example - Lesson 9: Simple Notifications using the Toast class and example -

That is to say that we have a TextView, an EditText (“Number”) and a Button. We set the values “tv1”, “et1” and “button1” as id. Remember that in the “Component Tree” window we have the reference of all the objects that our interface contains.

Source code:

package com.coding180.project001


import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

class MainActivity: AppCompatActivity () {

    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)
// coding180.com
        val number = (Math.random () * 100_001) .toInt ()
        Toast.makeText (this, "Number to remember: ${number}", Toast.LENGTH_LONG) .show ()
        val et1 = findViewById (R.id.et1) as EditText
        val button1 = findViewById (R.id.button) as Button
        button1.setOnClickListener {
            if (number == et1.text.toString (). toInt ())
            Toast.makeText (this, "Well remembered the number shown.", Toast.LENGTH_LONG) .show ()
            else
            Toast.makeText (this, "I'm sorry but it's not the number I showed.", Toast.LENGTH_LONG) .show ()
        }
    }
}

Let’s analyze the source code of the application, the first thing we do is to generate a random value between 0 and 100000:

        val number = (Math.random () * 100_001) .toInt ()

We show through the Toast class a message with the random number generated:

        Toast.makeText (this, "Number to remember: $ {number}", Toast.LENGTH_LONG) .show ()

We get the reference of the EditText and the Button:

        val et1 = findViewById (R.id.et1) as EditText
        val button1 = findViewById (R.id.button1) as Button

We set the lambda that captures the click of the button where we check if the generated random value matches the value that the operator loads in the EditText:

        button1.setOnClickListener {
            if (number == et1.text.toString (). toInt ())
                Toast.makeText (this, "You remembered the number shown.", Toast.LENGTH_LONG) .show ()
            else
                Toast.makeText (this, "I'm sorry but it's not the number I showed.", Toast.LENGTH_LONG) .show ()
        }

Now let’s see what the new Toast class is, we have to create an object of the Toast class, for this we call the static makeText method of the Toast class. From the returned object we call the show method.

The makeText method has three parameters: the first refers to the window or Activity where it appears (this), the second is a String that will be displayed and finally is a constant that indicates that the notification will be displayed for a long time or short:

        Toast.makeText (this, "Number to remember: $ {number}", Toast.LENGTH_LONG) .show ()

This causes that immediately start the application will show the notification with the number to be memorized by the user:

Lesson 9: Simple Notifications using the Toast class and example - Lesson 9: Simple Notifications using the Toast class and example - Lesson 9: Simple Notifications using the Toast class and example -

After a few seconds, the screen notification disappears (in our program the random number disappears)

When the operator finishes loading the number and proceeds to press the “control” button the code that we put in the lambda is executed:

        button1.setOnClickListener {
            if (number == et1.text.toString (). toInt ())
                Toast.makeText (this, "You remembered the number shown.", Toast.LENGTH_LONG) .show ()
            else
                Toast.makeText (this, "I'm sorry but it's not the number I showed.", Toast.LENGTH_LONG) .show ()
        }

In this method we extract from the EditText control the number entered by the user:

            if (number == et1.text.toString (). toInt ())

And if we verify if it matches the random number entered by the operator, if it matches, we immediately create a new notification and proceed to show that it was successful:

            if (number == et1.text.toString (). toInt ())
                Toast.makeText (this, "You remembered the number shown.", Toast.LENGTH_LONG) .show ()

For the false by another notice we show that you did not enter the number correctly:

            else
                Toast.makeText (this, "I'm sorry but it's not the number I showed.", Toast.LENGTH_LONG) .show ()

If we want a random number to appear again we must close the program and re-execute it.

About The Author

Related posts

Leave a Reply