Xamarin.Forms - How To Make SnackBar Using DependencyService

In this article, we will learn how to use SnackBar using dependency service in Xamarin.Forms application.

Introduction

This article demonstrates how to make SnackBar in Android applications using DependencyService in Xamarin.Forms applications. It supports only Android devices and can't support IOS applications.
 
NuGet Package - search Xamarin.Android
  • Plugin.CurrentActivity
Xamarin.Android Output
 
 
 
Prerequisites
  • Visual Studio 2017
  • This sample project is targeted only for Android and tested Android devices 
Let's start!

Step 1

You can create a new Xamarin.Forms application by going to File >> New >>Visual C# >> Cross-platform >> Cross-platform App (Xamarin.Forms or Xamarin.Native) and give solution name, then click OK .
 
(Eg - Project name - SnackBarDemo)
 
 
 
Step 2

After the project creation, declare a DependencyService Interface in Xamarin.Forms PCL project, we could call this interface from our PCL projects. For that, go to Solution Explorer >> click SnackBarDemo(PCL) project >>right-click and select add >>followed by select New Item>> In the popup window, scroll down and select Interface >> give the name as SnackInterface then click OK. Here is the code for this interface.
 
C# Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.   
  5. namespace SnackBarDemo  
  6. {  
  7.     public interface SnackInterface  
  8.     {  
  9.         void SnackbarShow(string message);  
  10.     }  
  11. }  
Step 3

Now, we are going to design a Button with a clicked event in MainPage.Xaml page. For that, go to Solution Explorer >>SnackBarDmeo(PCL) >> click open MainPage.Xaml and add the following code.
 
Xaml Code
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:SnackBarDemo"  
  5.              x:Class="SnackBarDemo.MainPage"  
  6.              Title="SnackBar Demo Page"  
  7.              BackgroundColor="AliceBlue"  
  8.              >  
  9.   
  10.     <Button Text="Show SnackBar!"   
  11.             VerticalOptions="Center"   
  12.             HorizontalOptions="Center"   
  13.             Clicked="Button_OnClicked"/>  
  14.   
  15. </ContentPage>  
Step 4

The next step would implement the DependencyService Interface in PCL project. For that, go to Solution Explorer >>SnackBarDemo(PCL) >> click open MainPage.xaml.cs and add the following code.

C# Code
  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 SnackBarDemo  
  9. {  
  10.     public partial class MainPage : ContentPage  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.         }  
  16.   
  17.         private void Button_OnClicked(object sender, EventArgs e)  
  18.         {  
  19.             DependencyService.Get<SnackInterface>().SnackbarShow("Am SnackBar in Xamarin.Android");  
  20.         }  
  21.     }  
  22. }  
Step 5

Afterwards, add the following NuGet Package to our Xamarin.Android project.
  • Plugin.CurrentActivity
For that, open Solution Explorer and select your Xamarin.Andriod project solution. Right-click and select "Manage nuget Package". In the popup window, navigate "Browse" tab and browse "Plugin.CurrentActivity" and select following nuget package then install it.
 
 
 
Step 6

Next, we implement the interface in Xamarin.Android project. For that, go to Solution Explorer >> select SnackbarDemo.Android project and right-click and select Add Followed by selecting New Item. In the popup window, select class and give the name as SnackBar_Android.cs then add the following assembly and code.

C# Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. using Android.App;  
  7. using Android.Content;  
  8. using Android.OS;  
  9. using Android.Runtime;  
  10. using Android.Support.Design.Widget;  
  11. using Android.Views;  
  12. using Android.Widget;  
  13. using Plugin.CurrentActivity;  
  14. using SnackBarDemo.Droid;  
  15. [assembly:Xamarin.Forms.Dependency(typeof(SnackBar_Android))]  
  16.   
  17. namespace SnackBarDemo.Droid  
  18. {  
  19.     public class SnackBar_Android : SnackInterface  
  20.     {  
  21.         public void SnackbarShow(string message)  
  22.         {  
  23.             Activity activity = CrossCurrentActivity.Current.Activity;  
  24.             Android.Views.View view = activity.FindViewById(Android.Resource.Id.Content);  
  25.             Snackbar.Make(view,message,Snackbar.LengthLong).Show();  
  26.         }  
  27.     }  
  28. }  
Step 7

Now, add another class named MainApplication and here is the code. In this code, the definition overrides the OnCreate method and calls "Init" method.

C# Code
  1. using System;  
  2. using Android.App;  
  3. using Android.Runtime;  
  4. using Plugin.CurrentActivity;  
  5.   
  6. namespace SnackBarDemo.Droid  
  7. {  
  8.     [Application]  
  9.     public class MainApplication : Application  
  10.     {  
  11.         public MainApplication(IntPtr handle, JniHandleOwnership transer)  
  12.             : base(handle, transer)  
  13.         {  
  14.         }  
  15.   
  16.         public override void OnCreate()  
  17.         {  
  18.             base.OnCreate();  
  19.             CrossCurrentActivity.Current.Init(this);  
  20.         }  
  21.   
  22.     }  
  23. }  
Step 8

Finally, go to "Build" menu and click "Configure Manager". Here, configure your startup projects. Click "F5" or start to build and run your application.

After few seconds, the app will start runing on Android simulator or emulator and we will see the app working successfully.
 
End result will look like below.
 
 

Full Source Code can be found here.