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
22
votes
2 answers

Android: MVVM is it possible to display a message (toast/snackbar etc.) from the ViewModel

I want to know what is the best approach to display some sort of message in the view from the ViewModel. My ViewModel is making a POST call and "onResult" I want to pop up a message to the user containing a certain message. This is my…
user3182266
  • 1,270
  • 4
  • 23
  • 49
20
votes
2 answers

Simple Android toasts not aligning properly

I'm simply calling from my Activity: Toast.makeText(this, "This is a toast", Toast.LENGTH_SHORT).show() But the result is a text aligned on the top of the toast container, not centered inside as it should: Any ideas on what could be wrong?
Ricardo
  • 7,785
  • 8
  • 40
  • 60
18
votes
5 answers

Is there code for Snackbars in Android L or are we expected to implement them ourselves?

The Material design website mentions a new Toast-like element called a Snackbar: http://www.google.com/design/spec/components/snackbars-and-toasts.html The Android L preview SDK documentation (sorry can't link since it's only downloadable) doesn't…
Tim Trueman
  • 1,513
  • 3
  • 17
  • 28
15
votes
4 answers

Method Toast.setView is deprecated

While I'm doing custom toast on my app, I noticed that setView is deprecated. Does anyone have a solution for this? toast.setView(customView);
HasanToufiqAhamed
  • 751
  • 1
  • 5
  • 17
15
votes
3 answers

Android Toast moves around on screen

My android app displays several Toast messages. I recently installed it on a Galaxy S6, running Android 5.1.1 and noticed that the messages are displayed initially around the center of the screen, then they move to proper position (near bottom, if…
Dior DNA
  • 433
  • 3
  • 11
15
votes
5 answers

How to display Toast from a Service after main Activity finishes?

UPDATE: I don't agree that this is a duplicate - because I am seeking for a way to exit the main app and still show a Toast from the service. In a very simple test app I have 2 buttons: Clicking any of the buttons will run a service with a…
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
13
votes
1 answer

Show Toast over PhoneScreen in LockState

Our goal is to show a toast when an incomming call happens. This won't work when the device is locked and an incomming call occures. Then the toast is visible behind the "locked fullscreen incomming call view". We tried different approches with like…
12
votes
4 answers

Which context to use for Toast in Android?

I just learned that I can use either: Toast.makeText(MainActivity.this, R.string.some_string,Toast.LENGTH_SHORT).show(); or, Toast.makeText(getApplicationContext(), R.string.some_string,Toast.LENGTH_SHORT).show(); To display a Toast in…
user963241
  • 6,758
  • 19
  • 65
  • 93
12
votes
3 answers

How to only allow certain values as parameter for a method in Java?

I want to write a method that only takes certain values for a parameter, like f.e. in the Toast class in Android. You can only use Toast.LENGTH_SHORT or Toast.LENGTH_LONG as duration for the method makeText(Context context, int resId, int duration).…
kaolick
  • 4,817
  • 5
  • 42
  • 53
11
votes
1 answer

Toast.makeText is not showing in Android 11 (with SDK 30 emulator)

According to https://developer.android.com/about/versions/11/behavior-changes-11#toasts, Toasts should still work as normal for Android11 (only setView() was deprecated). Note that text toasts are still allowed; these are toasts created using…
Angel Koh
  • 12,479
  • 7
  • 64
  • 91
11
votes
2 answers

Toast.setGravity() does not work in my AVD Nexus 6 API 30

I set the gravity of my toast to make it show on the top of the screen, with code below: Toast toast = Toast.makeText(getActivity(), "邮箱地址不能为空!", Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP, 0, 0); toast.show(); it does not work in my AVD…
shopkeeper
  • 189
  • 1
  • 9
11
votes
2 answers

Error "None of the following functions can be called with the arguments supplied:" with Toast

I want to create a code to click on items of RecyclerView. I found one from Internet, however it keep getting this error: None of the following functions can be called with the arguments supplied: public open fun makeText(p0: Context!, p1:…
11
votes
3 answers

IllegalStateException: view has already been added to the window manager

I have spent hours trying to fix an app crash and I think it deserves a question: The Exception: java.lang.IllegalStateException: View android.widget.LinearLayout{41a97eb8 V.E..... ......ID 0,0-540,105 #7f0b020d app:id/toast_layout_root} has already…
xialin
  • 7,686
  • 9
  • 35
  • 66
11
votes
1 answer

How to alert the user using Toast that the OkHttp request returned something other than 200?

I'm using OkHttp and everything is working fine, however, I wanted to take into consideration the case where the DNS resolution is off, the server is down, slow, or simply returns something other than HTTP Status Code 200. I've tried to use Toast,…
Naphtali Gilead
  • 222
  • 4
  • 18
10
votes
1 answer

Toast: Difference between "this" and "getApplicationContext()"?

My device runs Android 5.1.1 and I found out that if I use Toast.makeText(this, "This is a toast", Toast.LENGTH_SHORT).show(); I got this: But if I use getApplicationContext() instead of this, Toast.makeText(getApplicationContext(), "This is a…
1
2
3
42 43