Checking Battery Status Using Xamarin.Forms

Introduction

This article demonstrates how to check battery status using C# and XAML in Xamarin.Forms.


Step 1

Open Visual Studio and go to New Project >> Installed >> Visual C# >> Cross Platform.

Select Cross-Platform App, Then, give your project a Name and Location; click “OK”.



Step 2

After Project Creation, add the following NuGet Packages to your project.

  • Xam.Plugin.Battery 

Go to Solution Explorer and select your solution. Right-click the Dialogue Box, Open and Select “Manage NuGet Packages for Solution”.



Now, select the following NuGet Package and select your project to install the NuGet Package.
 
 

Step 2

Open Solution Explorer >> Project Name (Portable) >> MainPage.xaml. Double Click Open the Design View of this page. This page is DesignPage or Frontend.



The code is given below.
 
 

Xaml Code

 We are creating Button and Label inside the StackLayout. Assign the name as per your wish.

Step 4

Open Solution Explorer >> Project Name (Portable) >> MainPage.xaml.cs. Double-click to open the backend view of this page. This page is backend.

 

The code is given below.
 
 

C# Code

 We created the Button in Design Page, the Button click now results in the Battery Status. Button Name is BATTERY.

  1. public partial class MainPage : ContentPage  
  2.    {  
  3.        public MainPage()  
  4.        {  
  5.            InitializeComponent();  
  6.        }  
  7.   
  8.        private void btn_Clicked(object sender, EventArgs e)  
  9.        {  
  10.            if (CrossBattery.IsSupported)  
  11.            {  
  12.                var falta = CrossBattery.Current.RemainingChargePercent;  
  13.                Lb1.Text = $"PERSENTAGE: {falta.ToString()}%\n";  
  14.   
  15.                var status = CrossBattery.Current.Status;  
  16.                Lb1.Text += $"STATUS:{status.ToString()}\n";  
  17.   
  18.                var source = CrossBattery.Current.PowerSource;  
  19.                Lb1.Text += $"MEDIUM: {source.ToString()}\n\n";  
  20.   
  21.                CrossBattery.Current.BatteryChanged -=Current_BatteryChanged;  
  22.                CrossBattery.Current.BatteryChanged += Current_BatteryChanged;  
  23.            }  
  24.        }  
  25.   
  26.        private void Current_BatteryChanged(object sender, Plugin.Battery.Abstractions.BatteryChangedEventArgs e)  
  27.        {  
  28.            LblStatus.Text = "STATUS NOW:" + e.Status.ToString()+"\n";  
  29.            LblStatus.Text += "BATTERY:" + e.IsLow.ToString();  
  30.        }  
  31.    }  
Step 5

In this step, select Build & deploy option, followed by clicking "Start your application".

Now, go to the Run option, choose Debug. From the list of an Android Emulators or simulators, you can choose any API (Android Program Interface) Level Emulator and Simulator to run it.

Output

After a few seconds, you will see your app working.

When you connect your USB charger and click on the "BATTERY" button, this updates your application status.

 
 
 

Finally, we have successfully created an app for checking battery status in Xamarin.Forms.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now