« Back

QuickContactBadge

In this blog post we will be discussing an Android view type called a QuickContactBadge. A QuickContactBadge is an Android widget that shows an image and, when clicked, provides the user with a collection of different ways to communicate with that contact based on what contact information you have available for that contact. For instance, if only a contact's phone number is listed, your only option that pops up may be to call that person. However, if you have that contact's phone number, email address, and IM handle, then options for interacting with that person on all those platforms will be listed. Creating a QuickContactBadge is done in the layout XML like with any view type:

1<QuickContactBadge 
2android:id="@+id/myQuickContactBadge"
3android:layout_width="wrap_content" 
4android:layout_height="wrap_content" 
5android:src="@drawable/default_contact">
6</QuickContactBadge>

In order for there to be something for the user to click on, the QuickContactBadge needs to have an image associated with it. Whatever image you choose will need to be defined in the "android:src" parameter, as shown above. This image can be changed on the fly based on the selected contact if you so choose, and that will be explained later. First things first, though, is to define this QuickContactBadge in your code, as so:

1QuickContactBadge badgeLarge = (QuickContactBadge) findViewById(R.id.myQuickContactBadge);
2badgeLarge.setMode(ContactsContract.QuickContact.MODE_LARGE);

This will define your QuickContactBadge and set its type as a Large QuickContactBadge, which includes an image of the contact if present like this:



Next you need to select the contact you want to associate with your QuickContactBadge. This can be done in one of two ways. First, if you only know the phone number or email address of your contact, you can use the functions "assignContactFromEmail(String emailAddress, boolean lazyLookup)" or "assignContactFromPhone(String phoneNumber, boolean lazyLookup)". For example, if you only know a contact's email address is mike@gmail.com, you can assign the contact by email using myQuickContactBadge.assignContactFromEmail("mike@gmail.com", true). The second way would be to set the associated contact using a Uri. This Uri can be queried for in a number of ways from Android Contact's content provider, but in this example we are going to launch a contact picker intent and select the contact manually.

 1public void onPickContact(View view) { 
 2        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, 
 3                ContactsContract.Contacts.CONTENT_URI); 
 4        startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT); 
 5    }
 6    @Override 
 7    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
 8        if (resultCode == RESULT_OK) { 
 9            switch (requestCode) { 
10            case CONTACT_PICKER_RESULT:            
11                Uri contactUri = data.getData(); 
12                long id=Long.parseLong(contactUri.getLastPathSegment());
13                badgeLarge.assignContactUri(contactUri);
14                Bitmap image=loadPhoto(id);
15                if(image!=null)
16                {
17                    badgeLarge.setImageBitmap(loadPhoto(id));
18                }
19                else
20                {
21                    badgeLarge.setImageResource(R.drawable.default_contact);
22                }
23                break; 
24            } 
25        } 
26    }
27    private Bitmap loadPhoto(long id) {
28         Uri photoUri =
29            ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
30         InputStream photoInput = ContactsContract.Contacts
31            .openContactPhotoInputStream(this.getContentResolver(), photoUri);
32         if (photoInput != null)
33         {
34             return BitmapFactory.decodeStream(photoInput);
35         }
36         return null;
37    }

After selected a contact and using the returned Uri to populate the QuickContactBadge, you should now have a functioning QuickContactBadge that displays that contact's image, and when pressed will provide the user with options for ways to contact that contact based on the information stored for that contact. An example of your completed QuickContactBadge is shown below

Comments