Xamarin.Forms How to: Display Android’s Toast & SnackBar in PCL project

Below is some code to help you to display Toast & SnackBar pop up ONLY in your Xamarin.Forms Android project and calling the pop-up in PCL code.

Prerequisite

We first need to declare a DependencyService interface so we could call this from our PCL project.

<script src="https://gist.github.com/xyfoo/fb95ad9bf0a1f98a403402c5516d9edc.js">

Next step would be implement the interface in Android project

<script src="https://gist.github.com/xyfoo/216be13936f9f92c15ec98e921812577.js">

Last step would be calling the code in PCL project. We limit the execution to run on Android only by querying the platform in runtime.

<script src="https://gist.github.com/xyfoo/5c676c073417b11cd630bcbc27a4f427.js">

End Result

EndResult{:class="blogimg"}

Full source code can be found here

Note: If you’re using shared project, I supposed you could just directly call the notification in the code using the compiler directive

#if __ANDROID__
..
#endif