Working With Xamarin Toast Message For Android

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,
  1. Context - usually the application or activity context
  2. Message - The text message to display. Use a CharSequence object or String
  3. Duration - The time the pop-up is visible. There are two time options:

    1. SHORT - Last forabout 2 seconds 
    2. LONG - Last for about 4 seconds
To implement a Toast Message, let's create a new Xamarin project as follows.

Xamarin Toast Message For Android

Now select a Blank App as shown below,

Xamarin Toast Message For Android

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.

Xamarin Toast Message For Android 

Now you will find the following Dll added to your project.

Xamarin Toast Message For Android 

To use all the XLab controls, add the following assembly in the project.
  1. xmlns:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms"  
Now use the XLab checkbox as follows,
  1. <StackLayout>  
  2.        <Entry Placeholder="FirstName" x:Name="FirstName"></Entry>  
  3.        <Entry Placeholder="LastName" x:Name="LastName"></Entry>  
  4.        <Entry Placeholder="Email" x:Name="Email" Keyboard="Email"></Entry>  
  5.        <controls:ExtendedLabel Text="Select your Hobbies" TextColor="Blue"></controls:ExtendedLabel>  
  6.        <StackLayout Orientation="Vertical">  
  7.            <controls:CheckBox x:Name="Chk_Cricket" DefaultText="Cricket" HorizontalOptions="FillAndExpand" CheckedChanged="Chk_Cricket_CheckedChanged"></controls:CheckBox>  
  8.            <controls:CheckBox x:Name="Chk_Music" DefaultText="Music" HorizontalOptions="FillAndExpand"></controls:CheckBox>  
  9.            <controls:CheckBox x:Name="Chk_Outing" DefaultText="Visiting" HorizontalOptions="FillAndExpand"></controls:CheckBox>  
  10.        </StackLayout>  
  11.        <Button Text="Next" x:Name="btn_submit"></Button>  
  12.    </StackLayout>  

 Here is how the app will be displayed.

Xamarin Toast Message For Android
 
Add a new class to implement Tost Message Logic.
  1. using Android.Widget;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5.   
  6. namespace TostMessage  
  7. {  
  8.       
  9.     public static class ToastNotification  
  10.     {  
  11.         public static void TostMessage(string message)  
  12.         {  
  13.             var context = Android.App.Application.Context;  
  14.             var tostMessage = message;  
  15.             var durtion = ToastLength.Long;  
  16.               
  17.   
  18.             Toast.MakeText(context,tostMessage,durtion).Show();  
  19.         }  
  20.     }  
  21. }   
Now call the Toast message function from the checked change. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7.   
  8. namespace TostMessage  
  9. {  
  10.     public partial class MainPage : ContentPage  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.         }  
  16.   
  17.         private void Chk_Cricket_CheckedChanged(object sender, XLabs.EventArgs<bool> e)  
  18.         {  
  19.   
  20.             string x = Chk_Cricket.Text;  
  21.             ToastNotification.TostMessage(x+" Selected");  
  22.         }  
  23.     }  
  24. }  
Now here is the Tost Message.

Xamarin Toast Message For Android 

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.


Similar Articles