A Toast is a short alert message shown on the Android screen for a short interval of time. Android Toast is a short popup notification which is used to display information when we perform any operation in our app. In this tutorial, we shall not just limit ourselves by creating a lame toast but also do some user-interactive stuff. First, we shall create a screen with an Edit Text (Text box to take input) and a Button.
Step By Step Implementation to Demonstrate
Step 1: Create a new project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note: Select Kotlin as the programming language.
Step 2: Working with activity_main.xml
In the project window on the left side, navigate to app > res > layout > activity_main.xml.
Here we are going to add an Edit Text and a button. So, remove the hello word Text View in the XML. Now your XML should look like this.
activity_main.xml:
XML <?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"> <EditText android:id="@+id/editText" android:layout_width="250dp" android:layout_height="wrap_content" android:hint="Message" app:layout_constraintBottom_toTopOf="@+id/button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_chainStyle="packed" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="Toast" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText" /> </androidx.constraintlayout.widget.ConstraintLayout>
Design UI:
Step 3: Working with MainActivity.kt
Now, Let’s go to the Kotlin class file and make the toast happen. Open the Kotlin class file in app > java > {package-name} > MainActivity.kt
Here, editText is the object for EditText we have created in the Layout file. To get the contents of EditText we have used the text property and stored it in a variable message.
Making a Toast -
Toast.makeText(this, editText.text, Toast.LENGTH_LONG).show()
Toast.makeText() creates a toast and returns it and we should pass three parameters: - context - The context to use. Usually your {@link android.app.Application}or {@link android.app.Activity} object.
- text - The text to show in String format.
- duration - How long to display the message. Either LENGTH_SHORT or LENGTH_LONG
Hiding the Android Soft Keyboard after clicking the Toast button to see a clear Toast
Kotlin // (OPTIONAL) Function to hide the keyboard private fun hideKeyboard(activity: Activity) { // Get the root view of the activity val view: View = activity.findViewById(R.id.main) // Get the InputMethodManager service val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager // Hide the keyboard imm.hideSoftInputFromWindow(view.windowToken, 0) }
Add the above method to the main activity class and call it before making toast.
MainActivity.kt:
MainActivity.kt package org.geeksforgeeks.demo import android.app.Activity import android.content.Context import android.os.Bundle import android.view.View import android.view.inputmethod.InputMethodManager import android.widget.Button import android.widget.EditText 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) // Get reference to the button with the ID "toastButton" val button: Button = findViewById(R.id.button) // Set an OnClickListener for the button button.setOnClickListener { val editText: EditText = findViewById(R.id.editText) // Create a toast message with the text from the EditText Toast.makeText(this, editText.text, Toast.LENGTH_LONG).show() // Call hideKeyboard function to hide the keyboard hideKeyboard(this) } } // (OPTIONAL) Function to hide the keyboard private fun hideKeyboard(activity: Activity) { // Get the root view of the activity val view: View = activity.findViewById(R.id.main) // Get the InputMethodManager service val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager // Hide the keyboard imm.hideSoftInputFromWindow(view.windowToken, 0) } }
Output: