How To Open A Phone Number In The Dialer In Xamarin Forms Application Using Xamarin Essentials For Android And UWP

The PhoneDialer class is available at Xamarin.Essentials API. It enables an application to open a phone number in the dialer. Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C# leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.

Reading this article, you can learn how to open a phone number in the dialer in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in a cross-platform application development.

The following important tools are required for developing Xamarin Forms App,

  1. Windows 10 (Recommended)
  2. Visual Studio 2017
  3. Android API 19 or higher and UWP 10.0.16299.0 or higher.

Now, we can discuss step-by-step app development.

Step 1

Open Visual Studio 2017 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms)-> Give the Suitable Name for your App (XamFormDialer) ->OK

How To Open A Phone Number In The Dialer In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 2

Select the Cross-Platform template as a Blank APP ->Set Platform as Android and UWP and code sharing strategy as .NET standard, Afterwards, Visual Studio creates 3 projects (Portable, Android, UWP)

How To Open A Phone Number In The Dialer In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 3

For adding a reference,

RightClick Your solution (XamFormDialer) and Select Manage NuGet Packages.

For adding Xamarin.Essentials Reference, Choose Browse and Search Xamarin.Essentials, select the package and select all the projects (portable, Android, UWP) and install it.

How To Open A Phone Number In The Dialer In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 4

Add the Label, Entry and Button controls in Mainpage.Xaml for displaying Title, phone number and call now.

  1. <Label FontAttributes="Bold" Text="How to Open a phone number in the dialer in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>  
  2. <Entry x:Name="txtNum" Placeholder="Phone Number"></Entry>  
  3. <Button x:Name="btnCall" Text="Call Now" Clicked="btnCall_Click"/>  

Step 5

Add the following code in the MainActivity.cs in XamFormDialer.Android project,

In onCreate() method,

  1. Xamarin.Essentials.Platform.Init(this, savedInstanceState);  

Also add the below method,

  1. publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {  
  2.     Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  3.     base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  4. }  

Step 6

Add the following namespace and code in Mainpage.Xaml.cs

  1. using Xamarin.Essentials;  
  2. protectedoverridevoid OnAppearing() {  
  3.     base.OnAppearing();  
  4. }  
  5. asyncvoid btnCall_Click(object sender, System.EventArgs e) {  
  6.     if (!string.IsNullOrEmpty(txtNum.Text)) {  
  7.         await Call(txtNum.Text);  
  8.     }  
  9. }  
  10. publicasync Task Call(string number) {  
  11.     try {  
  12.         PhoneDialer.Open(number);  
  13.     } catch (FeatureNotSupportedException ex) {  
  14.         txtNum.Text = "Phone Dialer is not supported on this device.";  
  15.     } catch (Exception ex) {  
  16.         // Other error has occurred.  
  17.     }  
  18. }  

Note
Automatically the following code will be generated in XAML code view, while we are done in the design view.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamFormDialer" x:Class="XamFormDialer.MainPage">  
  3.     <StackLayout>  
  4.         <Label FontAttributes="Bold" Text="How to Open a phone number in the dialer in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />  
  5.         <Entry x:Name="txtNum" Placeholder="Phone Number"></Entry>  
  6.         <Button x:Name="btnCall" Text="Call Now" Clicked="btnCall_Click" />  
  7.     </StackLayout>  
  8. </ContentPage>  

Step 7

Deploy your App in UWP and Android, the output of the XamFormDialer App is,

 How To Open A Phone Number In The Dialer In Xamarin Forms Application Using Xamarin Essentials For Android And UWP
 
How To Open A Phone Number In The Dialer In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
Before clicking the call now Button- 

How To Open A Phone Number In The Dialer In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

How To Open A Phone Number In The Dialer In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
After Clicking the call now Button,
 
How To Open A Phone Number In The Dialer In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
How To Open A Phone Number In The Dialer In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
Summary

Now you have successfully opened the dialer in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.


Similar Articles