Xamarin Android: Create Android App Contact Picker and Send SMS

Step 1: Open Visual Studio, New Project, Templates, Visual C#, Android, then Blank App (Android).

Select Blank App. Then give Project Name and Project Location.

Step 2: Next go to Solution Explorer-> Project Name-> Components then Right Click to Get More Components then open new Dialog box. This dialog box to search the Xamarin.Mobile then Click Add App.

Step 3: Next to Open Solution Explorer, Project Name, Properties, AndroidManifest.xml Open the Xml page then give permission for READ_CONTACTS and SEND_SMS.

Step 4: Next to Open Solution Explorer, Project Name, Resources, layout, then Main.axml Click Open Design View and give the following code.

XML Code:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lellayout" android:background="#3E4095">  
  4.   
  5.     <RelativeLayout android:orientation="horizontal" android:layout_width="match_parent" android:id="@+id/linearLayout3" android:layout_height="wrap_content">  
  6.   
  7.         <TextView android:text="TO" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView1" android:gravity="top" android:paddingTop="20dp" />  
  8.   
  9.         <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:id="@+id/txtmobilenum" android:layout_marginLeft="20dp" android:layout_marginRight="35dp" />  
  10.   
  11.         <Button android:background="@drawable/addusergroup_48" android:layout_width="42.7dp" android:layout_alignParentRight="true" android:layout_height="wrap_content" android:id="@+id/btncontact" android:layout_marginLeft="20dp" android:layout_marginRight="2dp" />  
  12.   
  13.     </RelativeLayout>  
  14.   
  15.     <LinearLayout android:orientation="vertical" android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/linearLayout3" android:id="@+id/linearLayout6">  
  16.   
  17.         <EditText android:inputType="textMultiLine" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtbodymsg" />  
  18.   
  19.     </LinearLayout>  
  20.   
  21.     <LinearLayout android:orientation="vertical" android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/linearLayout6" android:id="@+id/linearLayout7">  
  22.   
  23.         <TableLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_below="@id/linearLayout3" android:stretchColumns="*" android:layout_height="wrap_content" android:id="@+id/linearLayout5">  
  24.   
  25.             <TableRow android:minWidth="25px" android:minHeight="25px" android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/tableRow1">  
  26.   
  27.                 <Button android:text="Send" android:layout_width="wrap_content" android:layout_height="46.0dp" android:id="@+id/btnsmssend" android:layout_column="2" />  
  28.   
  29.             </TableRow>  
  30.   
  31.         </TableLayout>  
  32.   
  33.     </LinearLayout>  
  34.   
  35. </RelativeLayout>  

Step 5: Next to Open Solution Explorer-> Project Name->MainActivity.cs.open c# Code Window. First give name space for using System.Linq;

Step 6: Next to declare Button and EditText Events and following to create Button Click Events.

C# Code:

  1. using System;  
  2. using Android.App;  
  3. using Android.Content;  
  4. using Android.Runtime;  
  5. using Android.Views;  
  6. using Android.Widget;  
  7. using Android.OS;  
  8. using System.Linq;  
  9. namespace SMS  
  10. {  
  11.     [Activity(Label = "SMS", MainLauncher = true, Icon = "@drawable/icon")]  
  12.     public class MainActivity: Activity  
  13.     {  
  14.         Button btncontact;  
  15.         Button btnsmssend;  
  16.         EditText txtmobilenum;  
  17.         EditText txtbodymsg;  
  18.         protected override void OnCreate(Bundle bundle)  
  19.         {  
  20.             base.OnCreate(bundle);  
  21.             // Set our view from the "main" layout resource  
  22.             SetContentView(Resource.Layout.Main);  
  23.             txtbodymsg = FindViewById < EditText > (Resource.Id.txtbodymsg);  
  24.             txtmobilenum = FindViewById < EditText > (Resource.Id.txtmobilenum);  
  25.             btncontact = FindViewById < Button > (Resource.Id.btncontact);  
  26.             btnsmssend = FindViewById < Button > (Resource.Id.btnsmssend);  
  27.             btncontact.Click += Btncontact_Click;  
  28.             btnsmssend.Click += Btnsmssend_Click;  
  29.         }  
  30.     }  
  31. }  

Step 7: Next to Pick the Contact Number in Contact List.so here create Contact Picker Button Click Event and then its passing Intent Value in OnActivityResult().

C# Code: 

  1. private void Btncontact_Click(object sender, EventArgs e)  
  2. {  
  3.     var contactPickerIntent = new Intent(Intent.ActionPick,  
  4.         Android.Provider.ContactsContract.Contacts.ContentUri);  
  5.     StartActivityForResult(contactPickerIntent, 101);  
  6. }  
  7. protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)  
  8. {  
  9.     if (requestCode == 101 && resultCode == Result.Ok)  
  10.     {  
  11.         //Ensure we have data returned  
  12.         if (data == null || data.Data == null)  
  13.             return;  
  14.         var addressBook = new Xamarin.Contacts.AddressBook(this);  
  15.         addressBook.PreferContactAggregation = false;  
  16.         //Load the contact via the android contact id  
  17.         // in the last segment of the Uri returned by the  
  18.         // android contact picker  
  19.         var contact = addressBook.Load(data.Data.LastPathSegment);  
  20.         //Use linq to find a mobile number  
  21.         var mobile = (from p in contact.Phones  
  22.             where  
  23.             p.Type == Xamarin.Contacts.PhoneType.Mobile  
  24.             select p.Number).FirstOrDefault();  
  25.         //See if the contact has a mobile number  
  26.         if (string.IsNullOrEmpty(mobile))  
  27.         {  
  28.             Toast.MakeText(this"No Mobile Number for contact!",  
  29.                 ToastLength.Short).Show();  
  30.             return;  
  31.         }  
  32.         txtmobilenum.Text = mobile;  
  33.     }  
  34. }  

Step 8: Finally w Send the sms on given selected Number so here to create Send Button Event.

C# code:

  1. private void Btnsmssend_Click(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         if (string.IsNullOrWhiteSpace(txtbodymsg.Text))  
  6.         {  
  7.             Toast.MakeText(this"SMS Text Body Is Empty", ToastLength.Long).Show();  
  8.         }  
  9.         else if (string.IsNullOrWhiteSpace(txtmobilenum.Text))  
  10.         {  
  11.             Toast.MakeText(this"Enter Mobile Number", ToastLength.Long).Show();  
  12.         }  
  13.         else  
  14.         {  
  15.             Toast.MakeText(this"Sending SMS... Please wait...", ToastLength.Short).Show();  
  16.             //Send SMS!  
  17.             var smsMgr = Android.Telephony.SmsManager.Default;  
  18.             smsMgr.SendTextMessage(txtmobilenum.Text, null, txtbodymsg.Text, nullnull);  
  19.             Toast.MakeText(this"SMS successfully sent", ToastLength.Short).Show();  
  20.             txtbodymsg.Text = "";  
  21.             txtmobilenum.Text = "";  
  22.         }  
  23.     }  
  24.     catch (Exception ex)  
  25.     {  
  26.         Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();  
  27.     }  
  28. }  

Step 9: Press F5 or Build and Run the Application.

Next Recommended Reading Creating ListView In Xamarin Android