Ticker

6/recent/ticker-posts

Toast in Android with Example


Android Toast is used as a small operational popup that displays some information for a small period. In a toast, it contains a message that can be displayed quickly and disappears after a timeout. Toast only fills the amount of space required for the message and the activity will remain visible for the user.

The android.widget.Toast class is the subclass of java.lang.Object class.

For example, when the user enters the app interface for the first time then display a "Welcome back" message in the toast message. Because it takes only a few seconds and it is temporary.



NOTE: Toasts are not clickable. If the user response to a status message is required, consider alternately using a Notification.

You can also create custom toast, for example, toast displaying an image. On the next page, we're going to discuss how to create a custom toast in android.


Toast Class

A Toast class is used to show notification for a particular interval of time. After sometimes it disappears. It doesn't interface the user interaction

There are two toast class in android:

  1. public static final int LENGTH_LONG: displays the toast message for a long duration of time
  2. public static final int LENGTH_SHORT: displays the toast message for a short duration of time

 

Toast class Methods:

  1. public static Toast makeText(Context context, CharSequence text, int duration): make the toast containing text and duration. 
  2. public void show(): for displaying the toast.
  3. public void setMargin (float horizontalMargin, float verticalMargin): use to changes the horizontal and vertical margin difference.

 


Android Toast Example:

 

Toast.makeText(getApplicationContext(),"Hello Javatpoint",Toast.LENGTH_SHORT).show()    


Other code:

Toast toast=Toast.makeText(getApplicationContext(),"Hello Javatpoint",Toast.LENGTH_SHORT);  

toast.setMargin(50,50);  

toast.show();  

Here, getApplicationContext() method returns the instance of Context.



Activity class displaying a Toast:

Let's see the code to display the toast.

 

toast_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".ToastActivtiy">

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="CLICK HERE TO DISPLAY TOAST"
       
android:layout_centerInParent="true"
       
android:padding="20dp"
       
android:onClick="displayToast"/>

</
RelativeLayout
>

 

 

ToastActivity.java

 

package com.codewithgolap.fragment;
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class ToastActivtiy extends AppCompatActivity {
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);         setContentView(R.layout.activity_toast_activtiy);
    }
   
public void displayToast(View view) {
        Toast.makeText(
this, "You Clicked Display Toast Button!", Toast.LENGTH_SHORT
).show();
    }
}
 

Output:



 



Post a Comment

0 Comments