Introduction
Toast messages are short pop-up messages that show up for a few seconds and then fade away. Toast messages automatically disappear after a timeout.
The basic Toast message creation contains the following things,
- Context - usually the application or activity context
- Message - The text message to display. Use a CharSequence object or String
- Duration - The time the pop-up is visible. There are two time options:
- SHORT - Last forabout 2 seconds
- LONG - Last for about 4 seconds
To implement a Toast Message, let's create a new Xamarin project as follows.
Now select a Blank App as shown below,
In the reference add the following third party XLab dll for some third party controls .
Xamarin Forms Labs\XLab is an open source project that aims to provide a powerful and cross platform set of controls and helpers tailored to work with Xamarin Forms.
Now you will find the following Dll added to your project.
To use all the XLab controls, add the following assembly in the project.
- xmlns:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms"
Now use the XLab checkbox as follows,
- <StackLayout>
- <Entry Placeholder="FirstName" x:Name="FirstName"></Entry>
- <Entry Placeholder="LastName" x:Name="LastName"></Entry>
- <Entry Placeholder="Email" x:Name="Email" Keyboard="Email"></Entry>
- <controls:ExtendedLabel Text="Select your Hobbies" TextColor="Blue"></controls:ExtendedLabel>
- <StackLayout Orientation="Vertical">
- <controls:CheckBox x:Name="Chk_Cricket" DefaultText="Cricket" HorizontalOptions="FillAndExpand" CheckedChanged="Chk_Cricket_CheckedChanged"></controls:CheckBox>
- <controls:CheckBox x:Name="Chk_Music" DefaultText="Music" HorizontalOptions="FillAndExpand"></controls:CheckBox>
- <controls:CheckBox x:Name="Chk_Outing" DefaultText="Visiting" HorizontalOptions="FillAndExpand"></controls:CheckBox>
- </StackLayout>
- <Button Text="Next" x:Name="btn_submit"></Button>
- </StackLayout>
Here is how the app will be displayed.
Add a new class to implement Tost Message Logic.
- using Android.Widget;
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace TostMessage
- {
-
- public static class ToastNotification
- {
- public static void TostMessage(string message)
- {
- var context = Android.App.Application.Context;
- var tostMessage = message;
- var durtion = ToastLength.Long;
-
-
- Toast.MakeText(context,tostMessage,durtion).Show();
- }
- }
- }
Now call the Toast message function from the checked change.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
-
- namespace TostMessage
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- }
-
- private void Chk_Cricket_CheckedChanged(object sender, XLabs.EventArgs<bool> e)
- {
-
- string x = Chk_Cricket.Text;
- ToastNotification.TostMessage(x+" Selected");
- }
- }
- }
Now here is the Tost Message.
A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity (int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset. Thus in this way we can implement a Toast message.