Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to get the contacts first name,middle name,last name instead of having display name in xmarine?

Hi,
I am working in c# xmarine android application, I am able to get phone contacts display name but I am not able to get contacts first name,middle name,last name instead of having display name? how to resolve?

Best Answer

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    The code to get contact information

         ICursor cursor = ContentResolver.Query(ContactsContract.Data.ContentUri, null, null, null);
    
            int indexGivenName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GivenName);
            int indexFamilyName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FamilyName);
            int indexDisplayName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName);
    
    
            while (cursor.MoveToNext())
            {
                String given = cursor.GetString(indexGivenName);
                String family = cursor.GetString(indexFamilyName);
                String display = cursor.GetString(indexDisplayName);
            }
    

    The code above worked ever ,but now we can only get DisplayName , i think the api is deprecated and broken though i didn't find the related documentation .

    Workaround

    DisplayName(full name) is consists of first name and last name , and contains an space character between them , so split the full name by space character , we can get first name and last name .

        String display = cursor.GetString(indexDisplayName);
        string firstName = display.Split(" ", StringSplitOptions.None)[0];
                string lastName = display.Split(" ", StringSplitOptions.None)[1];
    

    PS : This workaround only applys for the firstName(lastName ) without space character .

  • soumenhaldersoumenhalder Member ✭✭✭

    Hi @ColeX
    I tried with the above code, but i am facing exception "Java.Lang.NoSuchMethodError:" now how to resolve?

  • soumenhaldersoumenhalder Member ✭✭✭
    edited February 26

    Hi @ColeX
    got your code spinet but I am facing the same problem. I am attaching the code here.Also I added permission in Manifest file. I am working in a environment like visual studio 2015.

    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_CONTACTS" />

    using System;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android;
    using Android.Content.PM;
    using Android.Provider;
    using Android.Database;
    
    
    protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.Contact);  
    
                ICursor cursor = ContentResolver.Query(ContactsContract.Data.ContentUri, null, null, null); // this line is giving error
    
                int indexGivenName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GivenName);
                int indexFamilyName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FamilyName);
                int indexDisplayName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName);
    
    
                while (cursor.MoveToNext())
                {
                    String given = cursor.GetString(indexGivenName);
                    String family = cursor.GetString(indexFamilyName);
                    String display = cursor.GetString(indexDisplayName);
                }
    
            }
    
  • ColeXColeX Member, Xamarin Team Xamurai

    sorry , i forgot we have to check Permission at runtime in android 6.0 , take a look at https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows#runtime-permission-checks-in-android-60 .

    Add the following code into your project .

    protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);
    
            Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);
    
            FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
            fab.Click += FabOnClick;
    
    
            if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadContacts) == (int)Permission.Granted)
            {
    
            }
            else
            {
                if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.ReadContacts))
                {
    
                    var requiredPermissions = new String[] { Manifest.Permission.ReadContacts };
    
                }
                else
                {
                    ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadContacts }, 1);
                }
            }
        }
    
  • soumenhaldersoumenhalder Member ✭✭✭
    edited February 26

    hi @ColeX
    Thanks for the support,but same issue exist, Code is as follows.

    using System;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android;
    using Android.Content.PM;
    using Android.Provider;
    using Android.Database;
    using Android.Support.V4.Content;
    using Android.Support.V4.App;
    
    namespace RunTimePerm
    {
        [Activity(Label = "ContactActivity", MainLauncher = true)]
        public class ContactActivity : ListActivity
        {
    
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);            
    
                SetContentView(Resource.Layout.Main);       
    
                if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadContacts) == (int)Permission.Granted)
                {
                }
                else
                {
                    if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.ReadContacts))
                    {
                        var requiredPermissions = new String[] { Manifest.Permission.ReadContacts };
                    }
                    else
                    {
                        ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadContacts }, 1);
                    }
                }
    
                ICursor cursor = ContentResolver.Query(ContactsContract.Data.ContentUri, null, null, null);
    
                int indexGivenName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GivenName);
                int indexFamilyName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FamilyName);
                int indexDisplayName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName);
    
                while (cursor.MoveToNext())
                {
                    String given = cursor.GetString(indexGivenName);
                    String family = cursor.GetString(indexFamilyName);
                    String display = cursor.GetString(indexDisplayName);
                }
    
            }
    
        }
    }
    
  • ColeXColeX Member, Xamarin Team Xamurai
    edited February 26

    Move the logic of Read contact into FabOnClick method .

    Uninstall the application on your android simulator .

  • ron123ron123 Member

    @ColeX said:

    The code to get contact information

       ICursor cursor = ContentResolver.Query(ContactsContract.Data.ContentUri, null, null, null);
    
            int indexGivenName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GivenName);
            int indexFamilyName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FamilyName);
            int indexDisplayName = cursor.GetColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.DisplayName);
    
    
            while (cursor.MoveToNext())
            {
                String given = cursor.GetString(indexGivenName);
                String family = cursor.GetString(indexFamilyName);
                String display = cursor.GetString(indexDisplayName);
            }
    

    @ColeX : Hi, I saw your note that this API might have been deprecated. Can you guide about any other way to get first name, middle name, last name, suffix from contacts?

Sign In or Register to comment.