How To Read Contacts In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop the cross-platform and multi-platform apps. (Ex. Windows phone, Android, iOS). In the Xamarin platform, the code sharing concept is used. In Xamarin studio, Visual Studio is also available. Viewing the contacts from your Android phone is possible, using this Xamarin Android app.

Prerequisites

  • Visual Studio 2015 Update 3

The steps are given below, which are required to be followed in order to view the contacts 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 clicking (Ctrl+Shift+N).



Step 2

After opening a 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.



Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and the sources in your project. Now, select Resource-->Layout-->double click to open main.axml page. To write XAML code, you need to select the source.

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



Step 4

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



Now, delete the Linear layout and default hello world button.

Go to the source panel and you can see the button coding. You need to  delete it.

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

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

Step 5

After deleting the code, you need to rename Main.axml file (rename is called ContactItemView.axml).



Step 6

In this step, write the TextView code in your ContactItemView.axml page, mentioned below.



CantactItemView.axml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    android:layout_width="fill_parent"  
  4.    android:layout_height="fill_parent"  
  5.    android:padding="15dip" />  
Step 7

In this step, go to MainActivity.cs page.

Write the two packages, mentioned below and use subclass ListActivity.

MainActivity.cs
  1. using Android.Provider;  
  2. using System.Collections.Generic;  
  3. public class MainActivity : ListActivity  


Step 8

In this step, go to MainActivity.cs page. Write the code, mentioned below.

MainActivity.cs 
  1. public class MainActivity: ListActivity {  
  2.     protected override void OnCreate(Bundle bundle) {  
  3.         base.OnCreate(bundle);  
  4.         var uri = ContactsContract.Contacts.ContentUri;  
  5.         string[] projection = {  
  6.             ContactsContract.Contacts.InterfaceConsts.Id,  
  7.             ContactsContract.Contacts.InterfaceConsts.DisplayName  
  8.         };  
  9.         var cursor = ManagedQuery(uri, projection, nullnullnull);  
  10.         var contactList = new List < string > ();  
  11.         if (cursor.MoveToFirst()) {  
  12.             do {  
  13.                 contactList.Add(cursor.GetString(cursor.GetColumnIndex(projection[1])));  
  14.             } while (cursor.MoveToNext());  
  15.         }  
  16.         ListAdapter = new ArrayAdapter < string > (this, Resource.Layout.ContactItemView, contactList);  
  17.     }  
  18. }  


Step 9

In this step, give the required permissions in your app.

Go to Solution Explorer--> Properties-->Right click-->Open.



Step 10

After opening the properties options, select Android Manifest-->Required Permissions-->Check READ_CONTACTS.



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 Run.



Output

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

You will see your mobile contacts in the List view.



Summary

This was the process of how to view the contacts in Xamarin Android app, using Visual Studio 2015.

Next Recommended Readings