Xamarin.Forms - How To Make SnackBar Using DependencyService step by step
Tags: androidsnackbar, snackbar, xamarin, xamarinforms
Published: 2019-11-13 17:18:10

---
title: Xamarin.Forms - How To Make SnackBar Using DependencyService step by step
published: true
date: 2018-08-21 18:04:00 UTC
tags: Android SnackBar,SnackBar,Xamarin,Xamarin.Forms
canonical_url: https://logeshpalani.blogspot.com/2018/08/xamarinforms-how-to-make-snackbar-using.html
---

 **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**

  

  

| [![Xamarin](https://2.bp.blogspot.com/-WEtVS6tgZas/W3xL9tJi7XI/AAAAAAAAKuc/DZZvGDobWRwV3Dve6bl7qES-edPx1p2FQCLcBGAs/s320/Outout.gif "Xamarin.Forms")](https://2.bp.blogspot.com/-WEtVS6tgZas/W3xL9tJi7XI/AAAAAAAAKuc/DZZvGDobWRwV3Dve6bl7qES-edPx1p2FQCLcBGAs/s1600/Outout.gif) |
| Xamarin.Droid |

**Prerequisites**
    

- [Visual Studio 2017](https://www.visualstudio.com/)
- 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)

  

  

| [![Xamarin](https://2.bp.blogspot.com/-0AfRG6fpR_Y/W3xL9SeUWNI/AAAAAAAAKug/v0TYjhZHmuw4wnhOQoPeEPalmDhpks5KgCEwYBhgL/s640/New%2Bapp.png "Xamarin.Forms")](https://2.bp.blogspot.com/-0AfRG6fpR_Y/W3xL9SeUWNI/AAAAAAAAKug/v0TYjhZHmuw4wnhOQoPeEPalmDhpks5KgCEwYBhgL/s1600/New%2Bapp.png) |
| fig 1 New Project |

  

  
   

**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**

  

  

<script src="https://gist.github.com/logeshpalani98/36d6be3f9e4bdbf8707a4fa92e09f1c5.js"></script>  

**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**

  

<script src="https://gist.github.com/logeshpalani98/7fecd3c0b1e92fbf3025c02df080b1fa.js"></script>  

  
   
 **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**  

  
 
<script src="https://gist.github.com/logeshpalani98/e149b93291992fb12e1b562d49bdb532.js"></script>  

**Step 5**  
  
 Afterwards, add the following NuGet Package to our Xamarin.Android project.

  

- Plugin.CurrentActivity

| [![Xamarin](https://3.bp.blogspot.com/-uZMHeUEr9KA/W3xL9BEoaKI/AAAAAAAAKuk/gYDfB_KU06o1iTA5Fzp0gzHavd9JOAnTwCEwYBhgL/s1600/Nuget%2Bpackage%2BAndroid.png "Xamarin.Forms")](https://3.bp.blogspot.com/-uZMHeUEr9KA/W3xL9BEoaKI/AAAAAAAAKuk/gYDfB_KU06o1iTA5Fzp0gzHavd9JOAnTwCEwYBhgL/s1600/Nuget%2Bpackage%2BAndroid.png) |
| Fig 2 Nuget Package |

  

  

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**  
  
  
<script src="https://gist.github.com/logeshpalani98/57f65b16d797806d4eeccb84d06ec7b0.js"></script>  

  
 **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**  

  
 
<script src="https://gist.github.com/logeshpalani98/11bf69e28deeb1c1058ea5cda4639e73.js"></script>  

**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.  

| [![Xamarin](https://3.bp.blogspot.com/-WEtVS6tgZas/W3xL9tJi7XI/AAAAAAAAKuk/PtkDCV5YW9Us41ecdvsPkKqPffGWGFU-gCEwYBhgL/s320/Outout.gif "Xamarin.Forms")](https://3.bp.blogspot.com/-WEtVS6tgZas/W3xL9tJi7XI/AAAAAAAAKuk/PtkDCV5YW9Us41ecdvsPkKqPffGWGFU-gCEwYBhgL/s1600/Outout.gif) |
| Output |

  

  

End result will look like below.

* * *

Full Source Code can be found [here](https://github.com/logeshpalani98/SnackBar).