How To Update Contact Profile In Xamarin Android Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS).

In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, mentioned below are required to be followed in order to update the contact profile 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

Go to Solution Explorer. In Solution Explorer, get all the files and sources in your project.

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

You can design your app, if you want to, design and choose the Designer Window.

Xamarin

Step 4

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

Now, delete the 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 the MainActivity.cs page and 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.

You need to drag and drop the two buttons.

Xamarin

Step 6

Now, go to the properties Window. You need to edit the first button's Id value and text value.

(EX:android:id="@+id/getContactsButton"android:text="@string/getContacts" ).

Xamarin

Step 7

Now, go to the properties Window. You need to edit the second button's Id value and text value.

(EX:android:id="@+id/updateProfileButton"android:text="@string/updateProfile").

Xamarin

Step 8

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

Main.axml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">  
  2.     <Button android:id="@+id/getContactsButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/getContacts" />  
  3.     <Button android:id="@+id/updateProfileButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/updateProfile" />  
  4. </LinearLayout>  
Xamarin

Step 9

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

Xamarin

Step 10

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

String.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="getContacts">Get Contacts</string>  
  4.     <string name="updateProfile">Update Profile</string>  
  5.     <string name="ApplicationName">contactinfo</string>  
  6. </resources>  
Xamarin

Step 11

In this step, go to the MainActivity.cs page from Solution Explorer. Add one Namesapce and its name is called the provider.

MainActivity.cs

Use Android.Provider, as mentioned below.

Xamarin

Step 12

Now, write the code, mentioned below from MainActivity.cs page.

MainActivity.cs
  1. public class MainActivity: Activity {  
  2.     protected override void OnCreate(Bundle bundle) {  
  3.         base.OnCreate(bundle);  
  4.         // Set our view from the "main" layout resource  
  5.         SetContentView(Resource.Layout.Main);  
  6.         var getContactsButton = FindViewById < Button > (Resource.Id.getContactsButton);  
  7.         getContactsButton.Click += delegate {  
  8.             GetContacts();  
  9.         };  
  10.         var updateProfileButton = FindViewById < Button > (Resource.Id.updateProfileButton);  
  11.         updateProfileButton.Click += delegate {  
  12.             UpdateProfile();  
  13.         };  
  14.     }  
  15.     void GetContacts() {  
  16.         var uri = ContactsContract.Contacts.ContentUri;  
  17.         string[] projection = {  
  18.             ContactsContract.Contacts.InterfaceConsts.Id,  
  19.             ContactsContract.Contacts.InterfaceConsts.DisplayName  
  20.         };  
  21.         var cursor = ManagedQuery(uri, projection, nullnullnull);  
  22.         if (cursor.MoveToFirst()) {  
  23.             do {  
  24.                 Console.WriteLine("Contact ID: {0}, Contact Name: {1}",  
  25.                     cursor.GetString(cursor.GetColumnIndex(projection[0])),  
  26.                     cursor.GetString(cursor.GetColumnIndex(projection[1])));  
  27.             } while (cursor.MoveToNext());  
  28.         }  
  29.     }  
  30.     void UpdateProfile() {  
  31.         // write to the profile  
  32.         var values = new ContentValues();  
  33.         values.Put(ContactsContract.Contacts.InterfaceConsts.DisplayName, "John Doe");  
  34.         ContentResolver.Update(ContactsContract.Profile.ContentRawContactsUri, values, nullnull);  
  35.         // read the profile  
  36.         var uri = ContactsContract.Profile.ContentUri;  
  37.         string[] projection = {  
  38.             ContactsContract.Contacts.InterfaceConsts.DisplayName  
  39.         };  
  40.         var cursor = ManagedQuery(uri, projection, nullnullnull);  
  41.         if (cursor.MoveToFirst()) {  
  42.             Console.WriteLine(cursor.GetString(cursor.GetColumnIndex(projection[0])));  
  43.         }  
  44.         // navigate to the profile in the people app  
  45.         var intent = new Intent(Intent.ActionView, ContactsContract.Profile.ContentUri);  
  46.         StartActivity(intent);  
  47.     }  
  48. }  
Xamarin

Step 13

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

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

Xamarin

Step 14

After opening the properties options, select Android Manifest-->Required Permissions-->Check WRITE_CONTACTS and WRITE_PROFILE.

Xamarin

Step 15

Select Android Manifest-->Required Permissions-->Check READ_CONTACTS and READ_PROFILE.

Xamarin

Step 16

If you have 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 few seconds, the app will start running on your phone. You will see Update profile button.

Xamarin

Now, click Update Profile Button. It will show the Contact Profile and you can click edit option.

Xamarin

You will edit the profile and click save.

Xamarin

Summary

This was the process of how to update the contact profile in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all