While developing any mobile app that is connected to the internet, we have to also handle the condition when the user is not connected to the internet. Sometimes we use Alert box or toast to prompt users that they are not connected to the internet, but it will be good if we can add some animation or images to display that message so that we can improve user experience.
I am going to display that how we can check internet connectivity status and display message accordingly to the user.
I will use Xamarin.Essentials and Lottie animation to implement this.
Let’s start with setup.
1. Create a new project, we don’t need to do much to setup Xamarin.Essentials as visual studio already provide this with basic setup in new project.
2. We have to add “AccessNetworkState” permission in Android to access device network state, we can implement this in either AssemblyInfo.cs or in Android Manifest file.
If you want to add permission into AssemblyInfo.cs, add below link to the file,
Or, if you want to add in Android Manifest file you can add below line,
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
3. Setup Lottie:
1. Install Lottie nuget package in all your project named “Com.Airbnb.Xamarin.Forms.Lottie”.
2. We have to initialize lottie on iOS platform so we have to add AnimationViewRenderer.Init(); before LoadApplication(new App()) ,
We don’t need to do any additional setup for Android platform.
4. Now, let’s create sample UI to display animation,
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:forms="clr-namespace:Lottie.Forms;assembly=Lottie.Forms"
x:Class="LottieSample.MainPage">
<StackLayout>
<forms:AnimationView
x:Name="animationView"
Loop="True"
AutoPlay="True"
Animation="connected.json"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand" />
</StackLayout>
</ContentPage>
We have to add namespace xmlns:forms="clr-namespace:Lottie.Forms;assembly=Lottie.Forms" (highlighted in yellow background) to use AnimationView control.
5. Choose Animation: You can easily get beautiful animations from Lottie website (https://www.lottiefiles.com) or you can create your own using Adobe After Effects and export it to JSON file using an After Effects extension called Bodymovin.
6. Add animation json to project:
iOS: In iOS add it into your project and make sure you have the build action in Bundle Resource.
Android: Put it in your Assets folder and make sure you have the build action in Android Asset.
7. Now let’s add code to detect network connectivity status.
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
}
private void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
{
if (Connectivity.NetworkAccess != NetworkAccess.Internet)
{
animationView.Animation = "nointernet.json";
}
else
animationView.Animation = "connected.json";
}
}
And that’s it.
Output Screen: