Android enables you to browse and search for a list of contacts _android

Source: Internet
Author: User
Tags gettext

With this article, I would like to explain how to create a searchable "contact list" Android application. With this application, users can browse through all saved contacts and search for contacts by their contact names by using the navigation buttons. The application can also display a photo of the contact, if available.

To browse the contact list, you can use the <<,<,> , and >> buttons.

Users who want to search for contacts type the name of the contact in the Search Name text box, and then click the Search button. Click the "Clear Search" button to clear the Search Name text box and display the contact that was last viewed before starting the search.

Because the application reads contacts from the device, the following entries need to be in the Androidmanifest.xml file to allow permission to apply the Read contact:
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>

The following code creates a table layout to display contacts:

<tablelayout xmlns:android= "<a href=" http://schemas.android.com/apk/res/android "rel=" nofollow "target=" _ Blank ">http://schemas.android.com/apk/res/android" </a> android:layout_height= "Match_parent" Android: Layout_width= "350DP" > <TableRow> <textview android:id= "@+id/txtid" android:width= "175DP" android:text= " Contact Id: "/> <textview android:id=" @+id/txtidval "android:width=" 175DP "/> </TableRow> <tablerow&
 Gt <textview android:id= "@+id/txtdisplayname" android:width= "175DP" android:text= "Contact Name:"/> <textview Android:id= "@+id/txtdisplaynameval" android:width= "175DP"/> </TableRow> <TableRow> <textview Android:id= "@+id/txtphoneno" android:width= "175DP" android:text= "Phone number:"/> <textview android:id= "@+id/ Txtphonenoval "android:width=" 175DP "/> </TableRow> <TableRow> <textview android:id=" @+id/txtphoto "Android:width=" 175DP "android:text=" Photo: "/>; ImageView android:id= "@+id/imgphoto" android:width= "175DP"/> </TableRow> <TableRow> <button Android:id= "@+id/btnfirst" android:width= "175DP" android:text= "<<" android:onclick= "a"/> <button Android:id= "@+id/btnprevious" android:width= "175DP" android:text= "<" android:onclick= "previous"/> </ tablerow> <TableRow> <button android:id= "@+id/btnnext" android:width= "175DP" android:text= ">" Android : onclick= "Next"/> <button android:id= "@+id/btnlast" android:width= "175DP" android:text= ">>" Android:o nclick= "Last"/> </TableRow> <TableRow> <textview android:id= "@+id/txtsearch" android:width= "175DP "Android:text=" Search Name: "/> <autocompletetextview android:id=" @+id/txtsearchval "android:width=" 175DP "/ > </TableRow> <TableRow> <button android:id= "@+id/btnsearch" android:width= "175DP" android:text= "S Earch "android:onclick=" search "/> <button android:id= "@+id/btnclearsearch" android:width= "175DP" android:text= "clear Search" android:onclick= "ClearSearch"/> </

 Tablerow> </TableLayout>

Retrieve the address of the image and use the following command to access the contact:
Uri Contacts=contactscontract.contacts.content_uri;
Next, we create a Cursorloader object that loads all the contacts in ascending order by contact name, as follows:
Cursorloader loader=new
Cursorloader (this,contacts,null,null,null,contactscontract.contacts.display_name+ "ASC");
the Cursorloader construct takes the following parameters:
· Context context
· URI URI
· String projection
· String selection
· String Selectionargs
· String SortOrder

The following code populates the string array with the contact name:

C=loader.loadinbackground ();
Names=new String<span>[</span>c.getcount ()];
int ctr=0;
while (C.movetonext ())

{
 names<span>[</span>ctr]=c.getstring (C.getcolumnindex) ( ContactsContract.Contacts.DISPLAY_NAME));
 ctr++;

}

In the above code, the contact is loaded into the cursor object, which is a Cursorloader class that uses the Loadinbackground () method. All contact names are stored in a string array, and all contacts are browsed using the MoveToNext () method in the cursor class.

A arrayadapter is then used to bind the contact name to Autocompletetextview as follows:

public void Showcontact (Cursor c) {String id=c.getstring (C.getcolumnindex (contactscontract.contacts._id));
 String displayname=c.getstring (C.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME));
 Bitmap photo; InputStream Stream=contactscontract.contacts.opencontactphotoinputstream (Getcontentresolver (),
 Contenturis.withappendedid (Contactscontract.contacts.content_uri,long.parselong (id));
 if (stream!=null) {photo=bitmapfactory.decodestream (stream);
 Imgphoto.setimagebitmap (photo);
 else {imgphoto.setimagebitmap (null); Cursor phonecursor=getcontentresolver (). Query (Contactscontract.commondatakinds.phone.content_uri,null,
 contactscontract.commondatakinds.phone.contact_id+ "=" +id,null,null);

 String number= "";
 if (Phonecursor.getcount () >0) {Phonecursor.movetofirst ();
 Number=phonecursor.getstring (Phonecursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)); while (Phonecursor.movetonext ()) {number+= "," +phonecursor.getstring (Phonecursor.getcolUmnindex (ContactsContract.CommonDataKinds.Phone.NUMBER));
 } phonecursor.close ();
 Txtidval.settext (ID);
 Txtdisplaynameval.settext (DisplayName);
 Txtphonenoval.settext (number);

Enabledisablebuttons ();

 }

The code above uses the cursor parameter to get the contact ID, display name, photo, and phone number. It uses the Opencontactphotoinputstream () method to read the photos by returning the input stream and the Decodestream () method to the photo. It then uses the Setimagebitmap () method to display the contact photos on the ImageView. When the information is stored in another table, we must use another query in order to display the phone number.

The following code enables and disables navigation buttons based on the query results:

public void Enabledisablebuttons ()

{
 if (C.isfirst () &&c.islast ())
 {

 btnfirst.setenabled ( FALSE);
 Btnprevious.setenabled (false);
 Btnnext.setenabled (false);
 Btnlast.setenabled (false);

 else if (C.isfirst ())

 {
 btnfirst.setenabled (false);
 Btnprevious.setenabled (false);
 Btnnext.setenabled (true);
 Btnlast.setenabled (true);

 else if (C.islast ())

 {

 btnfirst.setenabled (true);
 Btnprevious.setenabled (true);
 Btnnext.setenabled (false);
 Btnlast.setenabled (false);

 else

 {

 btnfirst.setenabled (true);
 Btnprevious.setenabled (true);
 Btnnext.setenabled (true);
 Btnlast.setenabled (True);

 }



Click on the Search button to allow you to search the search text box based on the name of the contact method, as follows:

public void Search (View v)

{
 position=c.getposition ();
 if (Txtsearchval.gettext (). toString (). Trim (). Length () >0)
 {

 Uri contacts= ContactsContract.Contacts.CONTENT_URI;
 Cursorloader loader=new cursorloader
 (this,contacts,null,contactscontract.contacts.display_name+ "= '" + Txtsearchval.gettext (). toString () + "'", NULL,
 contactscontract.contacts.display_name+ "ASC");
 C=loader.loadinbackground ();
 if (C.getcount () >0)

 {

 c.movetofirst ();

 }

 }

 else

 {

 Uri Contacts=contactscontract.contacts.content_uri;
 Cursorloader loader=new cursorloader
 (this,contacts,null,null,null,contactscontract.contacts.display_name+ " ASC ");
 C=loader.loadinbackground ();
 C.move (position);
 C.movetonext ();
 }

 if (C.getcount () ==0)

 {

 Toast.maketext (this, "No results found to contact" +txtsearchval.gettext (). toString (), Toast.length_short). Show ();
 ShowAll ();
 return;

 }

 Showcontact (c);

}

The code above enables you to find a way to contact someone by name.

Click to clear the Search text box to execute the following code:

public void ClearSearch (view view)

{
 showall ();
 Txtsearchval.settext ("");

}
The ShowAll () method displays all contacts, as follows: public
void ShowAll ()

{

 Uri Contacts=contactscontract.contacts.content_uri;
 Cursorloader loader=new Cursorloader (this,contacts,null,null,null,contactscontract.contacts.display_name+ "ASC");
 C=loader.loadinbackground ();
 C.move (position);
 C.movetonext ();
 Showcontact (c);

}

The following code can navigate using the navigation buttons:

public void A (View v)

{
 c.movetofirst ();
 Showcontact (c);

}

public void Previous (View v)

{
 c.movetoprevious ();
 Showcontact (c);

}

public void Next (View v)

{
 c.movetonext ();
 Showcontact (c);
}

public void Last (View v)

{
 c.movetolast ();
 Showcontact (c);

}

Effect Chart:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.