What is Toast and How to use it with Examples

What is Toast and How to use it with Examples

In Android, a Toast provides a simple mechanism to show a brief, non-intrusive message to the user. It appears above all other UI elements for a short time, then fades away automatically. The primary purpose of Toast is to give feedback or information to the user.

How to Use:

  • Using the Default Toast Lengths:

Android provides two default durations for a Toast: Toast.LENGTH_SHORT and Toast.LENGTH_LONG.

Toast.makeText(context, "This is a short Toast!", Toast.LENGTH_SHORT).show() Toast.makeText(context, "This is a long Toast!", Toast.LENGTH_LONG).show() 
  • Positioning a Toast:

By default, a Toast appears near the bottom of the screen, centered horizontally. However, you can change its position using the setGravity method:

val toast = Toast.makeText(context, "Toast at the top!", Toast.LENGTH_SHORT) toast.setGravity(Gravity.TOP, 0, 0) toast.show() 
  • Custom Toast with Custom View:

You can customize the appearance of a Toast by setting your own layout.

First, create an XML layout for the Toast:

res/layout/custom_toast.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#55000000" android:padding="8dp" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_notification" android:layout_marginEnd="8dp"/> <TextView android:id="@+id/customToastText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Custom Toast" android:textColor="#FFFFFF"/> </LinearLayout> 

Then, in your Kotlin code:

val layoutInflater = LayoutInflater.from(context) val customToastRoot = layoutInflater.inflate(R.layout.custom_toast, null) val customToastText: TextView = customToastRoot.findViewById(R.id.customToastText) customToastText.text = "Your custom message here!" val customToast = Toast(context) customToast.view = customToastRoot customToast.duration = Toast.LENGTH_LONG customToast.show() 

Notes:

  • Toasts should be used for non-critical notifications since there's no guarantee the user will notice them, especially if they're looking away from the screen.
  • For more important feedback or action-based messages, consider using a Snackbar, which is another feedback mechanism provided by Android, or a custom dialog/notification.
  • Since a Toast is displayed for only a short duration, avoid using it for complex messages that require the user's full attention to comprehend.

Examples

  1. Introduction to Toast in Android:

    • Description: Toasts are simple pop-up messages used to display information briefly on an Android application. They are non-intrusive and fade away after a short duration.
    • Code:
      Toast.makeText(getApplicationContext(), "Hello, Toast!", Toast.LENGTH_SHORT).show(); 
  2. Using Toast messages in Android explained:

    • Description: Toasts are commonly used for displaying notifications, acknowledgments, or simple messages to users without requiring user interaction.
    • Code:
      Toast.makeText(getApplicationContext(), "Message to display", Toast.LENGTH_SHORT).show(); 
  3. Toast.makeText() method in Android with examples:

    • Description: Toast.makeText() is a method to create and display a toast. It requires the application context, message to display, and duration (either Toast.LENGTH_SHORT or Toast.LENGTH_LONG).
    • Code:
      Toast.makeText(getApplicationContext(), "Short Duration Toast", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "Long Duration Toast", Toast.LENGTH_LONG).show(); 
  4. Different types of Toast messages in Android:

    • Description: Toasts can be customized with different layouts and styles. You can create custom Toasts using LayoutInflater for more complex messages.
    • Code:
      Toast toast = Toast.makeText(getApplicationContext(), "Custom Toast", Toast.LENGTH_SHORT); View toastView = toast.getView(); // Customize toastView as needed toast.show(); 
  5. Duration of Toast messages in Android:

    • Description: Toasts can have short or long durations. Toast.LENGTH_SHORT typically lasts around 2 seconds, while Toast.LENGTH_LONG lasts around 3.5 seconds.
    • Code:
      Toast.makeText(getApplicationContext(), "Short Duration Toast", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "Long Duration Toast", Toast.LENGTH_LONG).show(); 
  6. Positioning Toast messages on the screen in Android:

    • Description: Toasts can be positioned using setGravity() method to specify the location on the screen. Common positions include Gravity.TOP, Gravity.BOTTOM, and Gravity.CENTER.
    • Code:
      Toast toast = Toast.makeText(getApplicationContext(), "Custom Position Toast", Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP | Gravity.START, 0, 0); // Adjust position as needed toast.show(); 
  7. Handling click events on Toast in Android examples:

    • Description: To handle click events on Toast, you can set a custom view using setView() method and attach an OnClickListener.
    • Code:
      Toast toast = Toast.makeText(getApplicationContext(), "Clickable Toast", Toast.LENGTH_SHORT); View toastView = toast.getView(); toastView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Handle click event Toast.makeText(getApplicationContext(), "Toast Clicked!", Toast.LENGTH_SHORT).show(); } }); toast.show(); 

More Tags

bash popover python-logging arithmetic-expressions kendo-dropdown concatenation fs resolution apache-httpcomponents window-size

More Programming Guides

Other Guides

More Programming Examples