Android Toast in Kotlin

A Toast alert message appears on the Android screen for a brief period of time. Android Toast is a quick popup notification that displays information during app operations.
In this tutorial, we will not only limit ourselves to making a lousy toast, but we will also include some user interaction.
First, we'll develop a screen with an Edit Text  and a Button.

There are many way to used Toast in Mobile application

  •   when user register successfully or failed
  •   when user login successfully or failed
  •   when user not found in database and man
Step-by-Step Implementation

Step 1: Create a New Project in Android Studio (File >new  project). 

Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

Implement the same in​voke the following code inside activity_main.xml file. 

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btnsubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="submit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Create an MainActivity.kt file in your package folder .

Implement the same invoke the fol​lowing code inside MainActivity.kt file.

 import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnsubmit = findViewById<Button>(R.id.btnsubmit)
btnsubmit.setOnClickListener {
Toast.makeText(this, "Hello, Toast example ", Toast.LENGTH_SHORT).show()
}
}
}
Step 4 : Output of above example

                                                                                 Happy coding!

365Bloggy July 10, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive