How To Make A Phone Dialer Use An Intent In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example,  Windows phone, Android, iOS). In the Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Phone dialer is used to make a phone call, which uses an intent in Xamarin Android app.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, given below needs to be followed in order to make a Phone Dialer, which uses an intent in Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio or click (Ctrl+Shift+N).

Xamarin

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.

Xamarin

Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and source in your project.

Select Resource-->Layout-->double click to open main.axml page. You need to select the source to write XAML code.

If you want to design, choose the designer Window and you can design your app.

Xamarin

Step 4

After opening the main.axml, the file will open the main page designer. Design the page, as per your wish.

Now, delete the Linear layout and default hello world button. Go to the source panel, where you can see the button coding. You need to delete it.

After deleting the XAML code, delete C# button action code.

Go to the MainActivity.cs page. You need to delete the button code.

Xamarin

Step 5

Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls.You need to go to the toolbox Window. Now, scroll down and you will see all the tools and controls.

Now, you need to drag and drop the button.

Xamarin

Step 6

Now, go to the properties Window. You need to edit the button's Id value and text value (Ex:android:id="@+id/myButton" android:text="@string/hello").

Xamarin

Step 7

In this step, go to the Main.axml page Source Panel. Note, the button's Id value.

Xamarin

Main.axml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">  
  2.     <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />  
  3. </LinearLayout>  
Step 8

In this step, open the String.xml page. Go to Solution Explorer-->Resource-->values-->String.xml.

Xamarin

Step 9

After opening String.xml file, write XML code, given below.

String.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Phone Dialer</string>  
  4.     <string name="ApplicationName">phonedialer</string>  
  5. </resources>  
Xamarin

Step 10

Now, go to the MainActivity.cs page. Write the code, given below between the OnCreate() method.

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     // Set our view from the "main" layout resource  
  4.     SetContentView(Resource.Layout.Main);  
  5.     Button button = FindViewById < Button > (Resource.Id.myButton);  
  6.     button.Click += delegate {  
  7.         var uri = Android.Net.Uri.Parse("tel:9944085453");  
  8.         var intent = new Intent(Intent.ActionDial, uri);  
  9.         StartActivity(intent);  
  10.     };  
  11. }  
Xamarin

Step 11

If you have an Android virtual device, run the app on it, else connect your Android phone and run the app on it.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

Xamarin

Output

After a few seconds, the app will start running on your phone.

If you click Phone Dialer button, open the dialer.

Xamarin

You can see the Dialer with your phone number in your phone and make a call, as it will work.

Xamarin

Summary

Hence, this was the process of how to make a phone dialer, which uses an intent in Xamarin Android app, using Visual Studio 2015.


Similar Articles