Questions tagged [android-toast]

A Toast is a momentary popup in Android which provides simple feedback about an operation.

Summary

A Toast is a momentary popup which provides simple feedback about an operation1. Toasts are added to an application's WindowManager and are not bound to an activity's UI; therefore, a Toast can remain visible after an Activity is navigated away from.

Toasts are typically displayed by using the makeText() method:

   // also supports Toast.LENGTH_LONG
Toast.makeText(getApplicationContext(), "some message", Toast.LENGTH_SHORT).show();

Sample Toast:-

enter image description here

It is important to note that the duration parameter requires either the LENGTH_LONG or LENGTH_SHORT . A custom millisecond value is not a valid duration parameter.

Custom Toasts

Toasts can use a custom View by using the setView() method3.

Usage

Toast messages should be as unobtrusive as possible4. Toasts should be used in situations that necessitate the user to be notified of certain background tasks such as a setting or draft being saved. Generally, Toasts should not exceed a single line of text.

Further reading

Toast overview

Toast developer guide

Toast code

Image Source and Brief guide

References:

1http://developer.android.com/guide/topics/ui/notifiers/toasts.html [Accessed on March 10, 2015]

2http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/widget/Toast.java#Toast.0LENGTH_SHORT [Accessed on March 10, 2015]

3http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView [Accessed on March 10, 2015]

4http://developer.android.com/reference/android/widget/Toast.html [Accessed on March 10, 2015]

639 questions
5
votes
0 answers

setGravity method is no-op - when called on text toasts

In the official android docs, it is mentioned that the setGravity method on toasts will be no-ops, when called on text toasts - with apps targeting Android Build.VERSION_CODES#R, My own understanding from above is the text toasts with setGravity…
AADProgramming
  • 6,077
  • 11
  • 38
  • 58
5
votes
1 answer

Why Toast keeps showing even when the app called onDestroy()?

Let's say I have this code in onCreate() for (int i = 0; i < 20; i++) { Toast.makeText(MainActivity.this, "Toast "+i, Toast.LENGTH_SHORT).show(); } when I launch the app, Toasts start to pop up. now, when I press the back…
Makarand
  • 983
  • 9
  • 27
5
votes
2 answers

IllegalStateException of toast View on Android P preview

While trying to release my app for production, the pre-launch report notified me of an error on Pixel 2 Android P Preview device. The error is related to a custom toast message I have, saying that a View "has already been added to the window…
5
votes
2 answers
5
votes
0 answers

Why are my Toast messages overlapping?

Concurrent Toasts have always appeared consecutively, but on my new device running SDK 27 (8.1.1) they appear concurrently, overlapping. I believe it changed in Android SDK 27 but I can't find any documentation to support it. Can anyone confirm or…
Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135
5
votes
3 answers

How to display a message when clicked on disable button in android?

I want to display a Toast message when clicked on disable button. button.setEnable(false); …
Anjali Patel
  • 807
  • 2
  • 11
  • 23
5
votes
2 answers

Why Toast.setGravity() is not working

Hello guys I have 2 versions of Toast like this version 1: Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_LONG).setGravity(Gravity.CENTER,0,0).show(); version 2: Toast t =…
Grumpy Cat
  • 1,219
  • 2
  • 11
  • 17
5
votes
2 answers

Showing toasts from a non-activity class. Ploblem setting the context to the toast

Spoiler: this post may contain something stupid due to being used to C and new to Java programming There is an activity MainActivity and a public non-activity class containing a number of methods. I need to show toast alerts for some of them Current…
Alex
  • 1,165
  • 2
  • 9
  • 27
5
votes
1 answer

Toast and Progress dialog edges cut off

I tested my app on various phones and native stuff like progress dialog and Toast messages look normal, but when I started testing on the phones with Android 5.0, edges of progress dialog/Toast got cut off. What am I doing wrong? On every phone…
4
votes
2 answers

android Espresso- toast message assertions not working with sdk 30

this simple check is working on sdk 29 but not on sdk 30: onView(withText(R.string.text)).inRoot( withDecorView(not(mActivityRule.activity.window.decorView))) .check(matches(isDisplayed())) I get androidx.test.espresso.NoMatchingRootException. can…
Dana
  • 109
  • 1
  • 6
4
votes
1 answer

ERROR: Failed to resolve: com.github.hatamiarash7:RTL-Toast:1.3

This error only happening in android studio 3.5. I don't know why because I have used this same library in my other projects but after upgrading to the latest android studio which is 3.5, I am having this issue. I have checked and tried some answers…
4
votes
1 answer

WindowManager$BadTokenException on toast and activity change

I am showing a toast for already processed items from my utils file. The scenario I am facing is that if I scan 10 items, and I change my screen, the toasts are still in process and my app crashes with Fatal Exception:…
The Bat
  • 1,085
  • 1
  • 13
  • 31
4
votes
3 answers

Android app Toast confusion

My app is a quiz app, in it there's a part that spits out a percent of questions the user got correct after answering all the questions as a Toast. The toast is showing up but the percentage is always coming up as 0. I have some log messages just…
4
votes
2 answers

Create a global Toast method to use it across all activities?

I want to create a global shortToast and longToast method to use it dynamically in all other activities I have, so I don't have to define the Toast method in every activity. I've tried this, but Android Studio tells me that this is a memory…
AlexioVay
  • 4,338
  • 2
  • 31
  • 49
4
votes
1 answer

Unit test including a Toast in production code fails

@Rule public ActivityTestRule mLoginActivityTestRule = new ActivityTestRule<>(LoginTestActivity.class); @Test public void loginWithTwitter() throws Exception { mLoginActivityTestRule.getActivity().performLogin(); } The…
Daniel Wilson
  • 18,838
  • 12
  • 85
  • 135