Introduction

In this post, I will be explaining the following.

Prerequisites

How can we achieve this functionality?

We can achieve this functionality using Device.OpenUri() method.

Explanation

  1. In the Start screen, launch Visual Studio. This opens the start page of Visual Studio like below.

    Xamarin
  1. In Visual Studio, click "Create new project…" to create a new project.
    Xamarin

  2. In the New Project dialog, click Cross-Platform, select the Mobile App (Xamarin.Forms) template, set the Name and Solution name to PhoneDialerSample, choose a suitable location for the project, and click the OK button.

    Xamarin

    Now, the project has been created.
  1. In PCL/NET Standard project, open XAML and paste the following code inside content page.
    1. <ScrollView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
    2. <StackLayout Padding="0,20,0, 0">
    3. <Label Text="Open number in dialer"
    4. FontAttributes="Bold"
    5. FontSize="Medium"
    6. HorizontalOptions="Center" />
    7. <Grid Padding="10,5,10,30">
    8. <Grid.RowDefinitions>
    9. <RowDefinition Height="*" />
    10. <RowDefinition Height="*" />
    11. </Grid.RowDefinitions>
    12. <Grid.ColumnDefinitions>
    13. <ColumnDefinition Width="*" />
    14. </Grid.ColumnDefinitions>
    15. <Entry x:Name="entryPhoneNumber"
    16. Placeholder="Phone number"
    17. Keyboard="Numeric"
    18. TextColor="Blue"
    19. Grid.Row="0" Grid.Column="0"></Entry>
    20. <Button Text="Call"
    21. TextColor="White"
    22. BackgroundColor="Blue"
    23. HorizontalOptions="FillAndExpand"
    24. Clicked="ButtonCallClicked"
    25. Grid.Row="1" Grid.Column="0"></Button>
    26. </Grid>
    27. <Label Text="Open text in Message app"
    28. FontAttributes="Bold"
    29. FontSize="Medium"
    30. HorizontalOptions="Center" />
    31. <Grid Padding="10,5,10,30">
    32. <Grid.RowDefinitions>
    33. <RowDefinition Height="*" />
    34. <RowDefinition Height="*" />
    35. <RowDefinition Height="*" />
    36. </Grid.RowDefinitions>
    37. <Grid.ColumnDefinitions>
    38. <ColumnDefinition Width="*" />
    39. </Grid.ColumnDefinitions>
    40. <Entry x:Name="entryMessgeTo"
    41. Placeholder="Phone number"
    42. Keyboard="Numeric"
    43. TextColor="Blue"
    44. Grid.Row="0" Grid.Column="0"></Entry>
    45. <Entry x:Name="entryMessageText"
    46. Placeholder="Message body"
    47. TextColor="Blue"
    48. Grid.Row="1" Grid.Column="0"></Entry>
    49. <Button Text="SMS"
    50. TextColor="White"
    51. BackgroundColor="Blue"
    52. HorizontalOptions="FillAndExpand"
    53. Clicked="ButtonSMSClicked"
    54. Grid.Row="2" Grid.Column="0"></Button>
    55. </Grid>
    56. <Label Text="Open Mail"
    57. FontAttributes="Bold"
    58. FontSize="Medium"
    59. HorizontalOptions="Center" />
    60. <Grid Padding="10,5,10,30">
    61. <Grid.RowDefinitions>
    62. <RowDefinition Height="*" />
    63. <RowDefinition Height="*" />
    64. <RowDefinition Height="*" />
    65. <RowDefinition Height="*" />
    66. </Grid.RowDefinitions>
    67. <Grid.ColumnDefinitions>
    68. <ColumnDefinition Width="*" />
    69. </Grid.ColumnDefinitions>
    70. <Entry x:Name="entryEmail"
    71. Placeholder="To Email"
    72. Keyboard="Email"
    73. TextColor="Blue"
    74. Grid.Row="0" Grid.Column="0"></Entry>
    75. <Entry x:Name="entryEmailSubject"
    76. Placeholder="Subject"
    77. TextColor="Blue"
    78. Grid.Row="1" Grid.Column="0"></Entry>
    79. <Editor x:Name="editorEmailBody"
    80. HeightRequest="50"
    81. Text="Email body text it is..."
    82. TextColor="Blue"
    83. Grid.Row="2" Grid.Column="0"></Editor>
    84. <Button Text="Mail"
    85. TextColor="White"
    86. BackgroundColor="Blue"
    87. HorizontalOptions="FillAndExpand"
    88. Clicked="ButtonMailClicked"
    89. Grid.Row="3" Grid.Column="0"></Button>
    90. </Grid>
    91. </StackLayout>
    92. </ScrollView>
  1. Open xaml.cs and paste the below code in it.
    1. public partial class MainPage : ContentPage
    2. {
    3. public MainPage()
    4. {
    5. InitializeComponent();
    6. }
    7. private void ButtonCallClicked(object sender, EventArgs e)
    8. {
    9. string phoneNumber = entryPhoneNumber.Text;
    10. if (string.IsNullOrEmpty(phoneNumber))
    11. {
    12. return;
    13. }
    14. // Following line used to display given phone number in dialer
    15. Device.OpenUri(new Uri(String.Format("tel:{0}", phoneNumber)));
    16. }
    17. private void ButtonSMSClicked(object sender, EventArgs e)
    18. {
    19. string smsPhoneNumber = entryMessgeTo.Text;
    20. string smsText = entryMessageText.Text;
    21. if (string.IsNullOrEmpty(smsPhoneNumber))
    22. {
    23. return;
    24. }
    25. // Following line used to open Messages app and populate below given details
    26. if (Device.RuntimePlatform == Device.iOS)
    27. {
    28. Device.OpenUri(new Uri(String.Format("sms:{0}&body={1}", smsPhoneNumber, smsText)));
    29. }
    30. else if (Device.RuntimePlatform == Device.Android)
    31. {
    32. Device.OpenUri(new Uri(String.Format("sms:{0}?body={1}", smsPhoneNumber, smsText)));
    33. }
    34. }
    35. private void ButtonMailClicked(object sender, EventArgs e)
    36. {
    37. string toEmail = entryEmail.Text;
    38. string emailSubject = entryEmailSubject.Text;
    39. string emailBody = editorEmailBody.Text;
    40. if (string.IsNullOrEmpty(toEmail))
    41. {
    42. return;
    43. }
    44. Device.OpenUri(new Uri(String.Format("mailto:{0}?subject={1}&body={2}", toEmail, emailSubject, emailBody)));
    45. }
    46. }
  1. Now, set either Android or iOS project as StartUp project and run the application. The output will be like below.

    Xamarin

Call

Xamarin

SMS

Xamarin

Email

Xamarin

To view full source code, please click here.

Thanks for reading the article!!