Android Toast

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

A Toast provides simple feedback about an operation in a small popup and automatically disappears after a timeout. It only fills the amount of space required for the message and the current activity remains visible and interactive.

Syntax

  • Toast makeText (Context context, CharSequence text, int duration)
  • Toast makeText (Context context, int resId, int duration)
  • void setGravity(int gravity, int xOffset, int yOffset)
  • void show()

Parameters

ParameterDetails
contextThe context to display your Toast in. this is commonly used in an Activity and getActivity() is commonly used in a Fragment
textA CharSequence that specifies what text will be shown in the Toast. Any object that implements CharSequence can be used, including a String
resIdA resource ID that can be used to provide a resource String to display in the Toast
durationInteger flag representing how long the Toast will show. Options are Toast.LENGTH_SHORT and Toast.LENGTH_LONG
gravityInteger specifying the position, or "gravity" of the Toast. See options here
xOffsetSpecifies the horizontal offset for the Toast position
yOffsetSpecifies the vertical offset for the Toast position

Remarks

A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive.

A more recent alternative to Toast is SnackBar. SnackBar offers an updated visual style and allows the user to dismiss the message or take further action. See the SnackBar documentation for details.

Official Documentation:

https://developer.android.com/reference/android/widget/Toast.html



Got any Android Question?