Building a Conact-List Caller Application
What is this: This tutorial shows how to create a ListActivity-Application, where your contact list is loaded to the List-Based View and when you click one of the contacts an Intent is sent to call that person.
What you learn: You will learn how easy it is, to create Intents and how to use ListActivities.
Difficulty: 1 of 5

What it will look like:

And this video shows you how to accomplish it:
Here is the source-code created during the video-tutorial:
File: HelloAndroid.java @ package: org.anddev.android.hello
Using java Syntax Highlighting
- package org.anddev.android.hello;
- import android.app.ListActivity;
- import android.content.ContentUris;
- import android.content.Intent;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.provider.Contacts.People;
- import android.view.View;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.SimpleCursorAdapter;
- public class HelloAndroid extends ListActivity {
- private ListAdapter mAdapter;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
- startManagingCursor(c);
- String[] columns = new String[]{People.NAME}; // Comment
- int[] names = new int[]{R.id.row_entry};
- mAdapter = new SimpleCursorAdapter(this, R.layout.main, c, columns, names);
- this.setListAdapter(mAdapter);
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id){
- super.onListItemClick(l, v, position, id);
- Intent i = new Intent(Intent.CALL_ACTION);
- Cursor c = (Cursor) mAdapter.getItem(position);
- long phoneID = c.getLong(c.getColumnIndex(People.PREFERRED_PHONE_ID));
- i.setData(ContentUris.withAppendedId(
- android.provider.Contacts.Phones.CONTENT_URI, phoneID));
- this.startActivity(i);
- }
- }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Main.xml @ folder: res.layout
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Name: "
- />
- <TextView
- android:id="@+id/row_entry"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
AndroidManifest.xml @ root-folder
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="org.anddev.android.hello">
- <uses-permission android:name="android.permission.READ_CONTACTS"/>
- <uses-permission android:name="android.permission.CALL_PHONE"/>
- <application android:icon="@drawable/icon">
- <activity android:name=".HelloAndroid" 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>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Regards,
plusminus









