Above output checked in Real Device.
xml files under values folder:
strings.xml:
<?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>
xml files under layout folder:
activity_main.xml:
<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>
text.xml:
<?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>
Java Code:
MainActivity.java:
package com.ram.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.text, contacts);
list.setAdapter(adapter);
}
}
}
AndroidManifest.xml:
<?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" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ram.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>