Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Created June 23, 2014 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cheesebaron/9f7cde5abe9db28214fa to your computer and use it in GitHub Desktop.
Save Cheesebaron/9f7cde5abe9db28214fa to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="App1.App1" android:versionCode="1"
android:versionName="1.0" android:installLocation="auto">
<application android:label="App1" android:icon="@drawable/Icon"></application>
<uses-permission android:name="android.permission.READ_CONTACTS" />
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Select Contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/contactButton" />
<TextView
android:text="Selected Contact"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/selectedContact"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
public class Frag : Fragment
{
private TextView _selectedContact;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.Frag, container, false);
var button = view.FindViewById<Button>(Resource.Id.contactButton);
button.Click += (s, e) =>
{
var intent = new Intent(Intent.ActionPick, ContactsContract.Contacts.ContentUri);
StartActivityForResult(intent, 9001);
};
_selectedContact = view.FindViewById<TextView>(Resource.Id.selectedContact);
return view;
}
public override void OnActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 9001 && resultCode == (int) Result.Ok)
{
if (data == null || data.Data == null)
return;
var addressBook = new Xamarin.Contacts.AddressBook(Activity)
{
PreferContactAggregation = false
};
var contact = addressBook.Load(data.Data.LastPathSegment);
_selectedContact.Text = contact.FirstName;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"/>
[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : FragmentActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content, new Frag())
.Commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment