Sometimes we have to provide a small notification to the user to tell them that something has happened. At the same time, we don't want to interrupt the user's task in any way. That's where toast notifications come in. We can use toast notifications to display subtle notifications in our app.
We can create toast notifications with the Toast
type as follows:
MakeText()
method, as shown here:using (var toast = Toast.MakeText( this, "This is a toast...", ToastLength.Short)) { toast.Show(); }
using (var toast = Toast.MakeText( this, "This is another toast...", ToastLength.Short)) { toast.SetGravity(GravityFlags.Center, 0, 0); toast.SetMargin(24, 24); toast.Show(); }
View
property:using (var toast = new Toast(ApplicationContext)) using (var image = new ImageView(this)) { image.SetImageResource(Resource.Drawable.Icon) toast.Duration = ToastLength.Short; toast.View = image; toast.Show(); }
Often an app needs to inform the user that something has happened, but the occurrence is not important enough to merit a disruption or interruption of normal use. If we want to let the user know about something, we can display a small text message somewhere in case the user is interested.
Usually, these types of notifications do not require any response from the user and thus have no intractable elements. For example, if the user is saving some settings, we don't need to pop up a dialog to inform the user that the settings have been saved. We would use a small notification that can be ignored.
We do this by using the toast notification system. One of the simplest ways is to simply invoke the MakeText()
method on the Toast
type, through which we obtain a toast. To display it, we invoke the Show()
method on the result.
The MakeText()
method requires the context, the text message, and the duration of the toast. For additional properties, we can use the various setter methods. For example, the SetGravity()
and SetMargin()
methods specify where on the screen and how large the toast will be.
If we really need more advanced toasts, we can instantiate a Toast
type through the constructor. With this object, we specify a custom view with the View
property. This could be a simple view, such an ImageView
instance, or it could be an entire view structure that was inflated from a resource.
Regardless of what is displayed, we have to remember that the toast cannot be interacted with and is timed. Also, it needs to be small and not invade the user's current task.
Toasts can be displayed from anywhere as they don't require a User Interface (UI) to be present. That means that they can be shown from a service or broadcast receiver. However, they have to be displayed from the UI thread.
If we are working in the background, we can show a toast by using the Task Parallel Library (TPL). First, we need to capture the UI scheduler when we are in the UI thread, say in the OnStartCommand()
method of a service:
uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Then, when we want to show the toast from the background, we start a Task
instance using this scheduler:
new Task(() => { // show toast }).Start(uiScheduler);