
strings.xml
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Display All Contacts</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="btitle">Load Contacts</string> </resources> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/btitle" android:textSize="25sp" /> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:cacheColorHint="#ffccff" android:divider="#00ffff" android:dividerHeight="2sp" > </ListView> </LinearLayout> |
custom.xml
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="#ff0000" android:textSize="25sp" > </TextView> |
ActivityMain.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
package com.raj.displayallcontacts; import java.util.ArrayList; import android.app.Activity; import android.app.ProgressDialog; import android.database.Cursor; import android.os.AsyncTask; import android.os.Bundle; import android.provider.ContactsContract; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; public class MainActivity extends Activity { ListView list; LinearLayout ll; Button loadBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ll = (LinearLayout) findViewById(R.id.LinearLayout1); list = (ListView) findViewById(R.id.listView1); loadBtn = (Button) findViewById(R.id.button1); loadBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub LoadContactsAyscn lca = new LoadContactsAyscn(); lca.execute(); } }); } class LoadContactsAyscn extends AsyncTask<Void, Void, ArrayList<String>> { ProgressDialog pd; @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); pd = ProgressDialog.show(MainActivity.this, "Loading Contacts", "Please Wait"); } @Override protected ArrayList<String> doInBackground(Void... params) { // TODO Auto-generated method stub ArrayList<String> contacts = new ArrayList<String>(); Cursor c = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); while (c.moveToNext()) { String contactName = c .getString(c .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phNumber = c .getString(c .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); contacts.add(contactName + ":" + phNumber); } c.close(); return contacts; } @Override protected void onPostExecute(ArrayList<String> contacts) { // TODO Auto-generated method stub super.onPostExecute(contacts); pd.cancel(); ll.removeView(loadBtn); ArrayAdapter<String> adapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.custom, contacts); list.setAdapter(adapter); } } } |
Manifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ram.displayallcontacts" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <span style="color: #993300;"><uses-permission android:name="android.permission.READ_CONTACTS" /></span> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.raj.displayallcontacts.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Output