Tuesday 22 May 2018

How to make phone call, send SMS, send Email in Xamarin Forms without using additional nuget package.

Hi All, I hope you are doing good.

In this post I am gonna be explaining about following.
  • How to open text through “Messages” application once we click on Message button.
  • How to open “dialer with phone number” once we tap/click on Dial button.
  • How to open mail application with populated data once we provide all to mail(s), Email subject and Email body on taping/clicking Mail button.

First create Xamarin Forms project.

In MainPage.xaml, place the following code inside content page.

<ScrollView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <StackLayout Padding="0,20,0, 0">
            <Label Text="Open number in dialer"
               FontAttributes="Bold"
               FontSize="Medium"
               HorizontalOptions="Center" />
            <Grid Padding="10,5,10,30">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Entry x:Name="entryPhoneNumber"
               Placeholder="Phone number"
               Keyboard="Numeric"
               TextColor="Blue"
               Grid.Row="0" Grid.Column="0"></Entry>

                <Button Text="Call"
                TextColor="White"
                BackgroundColor="Blue"
                HorizontalOptions="FillAndExpand"
                Clicked="ButtonCallClicked"
                Grid.Row="1" Grid.Column="0"></Button>
            </Grid>


            <Label Text="Open text in Message app"
               FontAttributes="Bold"
               FontSize="Medium"
               HorizontalOptions="Center" />
            <Grid  Padding="10,5,10,30">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Entry x:Name="entryMessgeTo"
                   Placeholder="Phone number"
                   Keyboard="Numeric"
                   TextColor="Blue"
                   Grid.Row="0" Grid.Column="0"></Entry>
                <Entry x:Name="entryMessageText"
                   Placeholder="Message body"
                   TextColor="Blue"
                   Grid.Row="1" Grid.Column="0"></Entry>
                <Button Text="SMS"
                    TextColor="White"
                    BackgroundColor="Blue"
                    HorizontalOptions="FillAndExpand"
                    Clicked="ButtonSMSClicked"
                    Grid.Row="2" Grid.Column="0"></Button>
            </Grid>


            <Label Text="Open Mail"
               FontAttributes="Bold"
               FontSize="Medium"
               HorizontalOptions="Center" />
            <Grid  Padding="10,5,10,30">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Entry x:Name="entryEmail"
                   Placeholder="To Email"
                   Keyboard="Email"
                   TextColor="Blue"
                   Grid.Row="0" Grid.Column="0"></Entry>
                <Entry x:Name="entryEmailSubject"
                   Placeholder="Subject"
                   TextColor="Blue"
                   Grid.Row="1" Grid.Column="0"></Entry>
                <Editor x:Name="editorEmailBody"
                        HeightRequest="50"
                        Text="Email body text it is..."
                        TextColor="Blue"
                        Grid.Row="2" Grid.Column="0"></Editor>
                <Button Text="Mail"
                    TextColor="White"
                    BackgroundColor="Blue"
                    HorizontalOptions="FillAndExpand"
                    Clicked="ButtonMailClicked"
                    Grid.Row="3" Grid.Column="0"></Button>
            </Grid>

        </StackLayout>
    </ScrollView>

In MainPage.xaml.cs, the code will be like below
public partial class MainPage : ContentPage
       {
              public MainPage()
              {
                     InitializeComponent();
              }

        private void ButtonCallClicked(object sender, EventArgs e)
        {
            string phoneNumber = entryPhoneNumber.Text;

            if (string.IsNullOrEmpty(phoneNumber))
            {
                return;
            }

            // Following line used to display given phone number in dialer
            Device.OpenUri(new Uri(String.Format("tel:{0}", phoneNumber)));
        }

        private void ButtonSMSClicked(object sender, EventArgs e)
        {
            string smsPhoneNumber = entryMessgeTo.Text;
            string smsText = entryMessageText.Text;

            if (string.IsNullOrEmpty(smsPhoneNumber))
            {
                return;
            }

            // Following line used to open Messages app and populate below given details
            if (Device.RuntimePlatform == Device.iOS)
            {
                Device.OpenUri(new Uri(String.Format("sms:{0}&body={1}", smsPhoneNumber, smsText)));
            }
            else if (Device.RuntimePlatform == Device.Android)
            {
                Device.OpenUri(new Uri(String.Format("sms:{0}?body={1}", smsPhoneNumber, smsText)));
            }
        }

        private void ButtonMailClicked(object sender, EventArgs e)
        {
            string toEmail = entryEmail.Text;
            string emailSubject = entryEmailSubject.Text;
            string emailBody = editorEmailBody.Text;

            if (string.IsNullOrEmpty(toEmail))
            {
                return;
            }

            Device.OpenUri(new Uri(String.Format("mailto:{0}?subject={1}&body={2}", toEmail, emailSubject, emailBody)));
        }
    }
To view full source code please click here

Now, I am running the application in Android device. The following is the output.



Call



SMS



Email

Please write your doubts in comments section if you have any.

Thanks for reading the article!!

6 comments:

  1. Thanks nice post !!SMS plugins are one of the best ways to send SMS directly from the browser. SMS plugins help you to create a buzz in the market and making your marketing much easier than other traditional means of communication.

    ReplyDelete
  2. Thanks for an interesting blog. What else may I get that sort of info written in such a perfect approach? I have an undertaking that I am just now operating on, and I have been on the lookout for such info. KVB Customer Care

    ReplyDelete
  3. I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious... Allahabad bank customer care

    ReplyDelete
  4. Your work is very good and I appreciate you and hopping for some more informative posts. Thank you for sharing great information to us. receive SMS free

    ReplyDelete
  5. Nowadays, you can go to every corner of the world with a wireless phone that fits in your hand and works the minute you arrive overseas. Whether it is a business trip or a leisure travel, you are assured the best cellular phone rental for the amount of time you need. Cellular phone rental is the cheapest way for the casual traveler to have the latest phone with the full assurance that it works in the country of his destination. 뽐뿌

    ReplyDelete
  6. It was wondering if I could use this write-up on my other website, I will link it back to your website though.Great Thanks. https://zetamatic.com/downloads/woocommerce-mailchimp-newsletter-discount/

    ReplyDelete

Intoduction to Flutter

Hi All, I hope every one is doing good In this article, we will know about the introduction of Flutter.