[VIDEO-Tut] Building an Contact-Caller App in Minutes

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[VIDEO-Tut] Building an Contact-Caller App in Minutes

Postby plusminus » Thu Nov 15, 2007 5:08 pm

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.

:idea: Designed/Tested with sdk-version: m5-rc14

Difficulty: 1 of 5 :D

What it will look like:
Image Image

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
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.hello;
  2.  
  3. import android.app.ListActivity;
  4. import android.content.ContentUris;
  5. import android.content.Intent;
  6. import android.database.Cursor;
  7. import android.os.Bundle;
  8. import android.provider.Contacts.People;
  9. import android.view.View;
  10. import android.widget.ListAdapter;
  11. import android.widget.ListView;
  12. import android.widget.SimpleCursorAdapter;
  13.  
  14. public class HelloAndroid extends ListActivity {
  15.    
  16.      private ListAdapter mAdapter;
  17.      
  18.       /** Called when the activity is first created. */
  19.     @Override
  20.     public void onCreate(Bundle icicle) {
  21.         super.onCreate(icicle);
  22.         Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
  23.         startManagingCursor(c);
  24.        
  25.         String[] columns = new String[]{People.NAME}; // Comment
  26.         int[] names = new int[]{R.id.row_entry};
  27.        
  28.         mAdapter = new SimpleCursorAdapter(this, R.layout.main, c, columns, names);
  29.        
  30.         this.setListAdapter(mAdapter);
  31.     }
  32.    
  33.     @Override
  34.     protected void onListItemClick(ListView l, View v, int position, long id){
  35.      super.onListItemClick(l, v, position, id);
  36.      
  37.      Intent i = new Intent(Intent.CALL_ACTION);
  38.      
  39.      Cursor c = (Cursor) mAdapter.getItem(position);
  40.      long phoneID = c.getLong(c.getColumnIndex(People.PREFERRED_PHONE_ID));
  41.      
  42.      i.setData(ContentUris.withAppendedId(
  43.           android.provider.Contacts.Phones.CONTENT_URI, phoneID));
  44.      
  45.      this.startActivity(i);
  46.     }
  47. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Main.xml @ folder: res.layout
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="horizontal"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="wrap_content"
  6.    >
  7. <TextView  
  8.    android:layout_width="wrap_content"
  9.    android:layout_height="wrap_content"
  10.    android:text="Name: "
  11.    />
  12. <TextView  
  13.    android:id="@+id/row_entry"
  14.    android:layout_width="wrap_content"
  15.    android:layout_height="wrap_content"
  16.    />
  17. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


AndroidManifest.xml @ root-folder
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    package="org.anddev.android.hello">
  4.     <uses-permission android:name="android.permission.READ_CONTACTS"/>
  5.     <uses-permission android:name="android.permission.CALL_PHONE"/>
  6.     <application android:icon="@drawable/icon">
  7.         <activity android:name=".HelloAndroid" android:label="@string/app_name">
  8.             <intent-filter>
  9.                 <action android:name="android.intent.action.MAIN" />
  10.                 <category android:name="android.intent.category.LAUNCHER" />
  11.             </intent-filter>
  12.         </activity>
  13.     </application>
  14. </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Last edited by plusminus on Sat Feb 23, 2008 12:57 pm, edited 5 times in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby Marek » Thu Nov 15, 2007 8:26 pm

Nice one. Thanks for posting the tutorial.
Marek
Freshman
Freshman
 
Posts: 2
Joined: Thu Nov 15, 2007 8:23 pm

Postby mrocket » Thu Nov 15, 2007 11:23 pm

Huh, i like these tutorials, got so many pictures and comments!

Even I can understand them :roll:

Cya...
mrocket
Junior Developer
Junior Developer
 
Posts: 10
Joined: Thu Nov 15, 2007 7:28 pm

I like it.

Postby Mark » Fri Nov 16, 2007 4:56 pm

I really like this style of tutorials.

: ) good~ :D
Mark
Freshman
Freshman
 
Posts: 2
Joined: Fri Nov 16, 2007 4:53 pm

Names won't display

Postby atr » Mon Nov 19, 2007 11:40 am

i know the tutorial is really easy. I get it run, but there are no names, no numbers. it's just white blank display. I done copy paste the exact same source code. any idea? did i miss something?
thank you for suggestion. :?
atr
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 19, 2007 11:36 am
Location: Singapore

Re: Names won't display

Postby plusminus » Mon Nov 19, 2007 3:11 pm

atr wrote:i know the tutorial is really easy. I get it run, but there are no names, no numbers. it's just white blank display. I done copy paste the exact same source code. any idea? did i miss something?
thank you for suggestion. :?

Hi atr,

seems like you didn't add contacts in the emulator, i wondered too on my first try.
Simply select the Contacts-Icon in the Home-Window (the very first with the mountain in the background) and add some Contacts there.

Think that helped...?

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Done

Postby atr » Tue Nov 20, 2007 1:22 pm

Yep its done! Thank you :D
atr
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 19, 2007 11:36 am
Location: Singapore

Postby Ghalya » Mon Feb 11, 2008 8:22 pm

Hi Mr. PLusMiuns,

Thx for ur nice tutorial when I implement it everything go smoothly UNTIL I decide to change the GUI in order to understand the ActivityList and how the ListAdapter work.

See this is the idea to improve the prototype

Image

So based on that, I change the main.xml file to:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="wrap_content"
  6.    >
  7.  
  8. <TextView  id="@+id/row_entry"
  9.    android:layout_width="wrap_content"
  10.    android:layout_height="wrap_content"
  11.    />
  12. <TextView  id="@+id/location"
  13.    android:layout_width="wrap_content"
  14.    android:layout_height="wrap_content"
  15.       android:visibility="gone" />
  16. <TextView  id="@+id/number"
  17.    android:layout_width="wrap_content"
  18.    android:layout_height="wrap_content"
  19.   android:foreground="#7B7C7D"
  20.      android:visibility="gone"
  21.    /
  22. </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


And change the onCreate method in the java file to:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     @Override
  2.     public void onCreate(Bundle icicle) {
  3.         super.onCreate(icicle);
  4.        
  5.         Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
  6.         startManagingCursor(c);
  7.        
  8.         String[] columns = new String[]{People.NAME,People.USER_STATUS, People.NUMBER};
  9.         int[] names = new int[]{R.id.row_entry,R.id.location,R.id.number};
  10.  
  11.         mAdapter = new SimpleCursorAdapter(this, R.layout.main, c, columns,names );
  12.        
  13.         this.setListAdapter(mAdapter);        
  14.     }    
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Notice I got problem in getting the correct value for contact location.

Also I override onKeyDown and onKeyUp method to implement on select item. I know by using set visiabilty I can make it. But the problem I dunno how to get the specific view from ListAdapter in order to change it. I tried getitem(), findViewbyID and many other ways, but all failed >.<

Therefore, I counting in you to help me in solving this issue. It may seem easy to u, cause u already knowledgeable enough with this platform =P

Thanks in advance.

Best Regards,
Ghalya
Ghalya
Freshman
Freshman
 
Posts: 9
Joined: Sat Feb 09, 2008 4:07 pm
Location: Dubai, UAE

Postby code » Mon Feb 11, 2008 9:59 pm

First, thanks Mr. PLusMiuns for this tutorial, it works fine with me and I learned from it :)

Regarding the problem that was posted by Ms. Ghalya, I tried to find a solution for it. Here is what I have added to your code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                @Override
  2.         public boolean onKeyDown(int keyCode, KeyEvent event) {
  3.                 if (selectedTV != null)
  4.                         selectedTV.setVisibility(View.GONE);
  5.  
  6.                 View vv = this.getListView().getSelectedView();
  7.                 TextView tv = (TextView) vv.findViewById(R.id.number);
  8.                 tv.setVisibility(View.VISIBLE);
  9.                 selectedTV = tv;
  10.                 return true;
  11.         }
  12.  
  13.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


BUT :( I do not know how to do that for the selected list item when the application starts. I tried many ways to get the first list item, but it always give me NullPointerException :(
Am a beginner in Android, so could someone tell me how can I get the selected list item when the application start??
User avatar
code
Junior Developer
Junior Developer
 
Posts: 21
Joined: Sat Feb 09, 2008 6:24 pm
Location: UAE

Postby plusminus » Mon Feb 11, 2008 10:13 pm

Hello code,

did you place your tries behind this line:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.setListAdapter(mAdapter);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


I haven't tested it, but it still could not work, because perhaps we just set the Adapter and when when code returns from the onCreate to whatever is above (we cannot know, because the sources are not open :( ) the List gets filled.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby code » Mon Feb 11, 2008 10:43 pm

Yes all my tries were placed after this line :(
So, where could I place it??
User avatar
code
Junior Developer
Junior Developer
 
Posts: 21
Joined: Sat Feb 09, 2008 6:24 pm
Location: UAE

Postby Ghalya » Tue Feb 12, 2008 12:30 pm

thx CODE for ur solution :D
Ghalya
Freshman
Freshman
 
Posts: 9
Joined: Sat Feb 09, 2008 4:07 pm
Location: Dubai, UAE

Postby plusminus » Tue Feb 12, 2008 1:47 pm

Hello code,

perhaps a bit delayed, when application is fully created, i.e. using a
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. new Handler().postDelayed(Runnable r, long ms)
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby code » Tue Feb 12, 2008 3:07 pm

I tried what you suggest plusminus, and this here where I added it:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                                 this.setListAdapter(mAdapter);
  3.  
  4.                
  5.  
  6.                 new Handler().postDelayed(null, 100000);
  7.  
  8.                
  9.  
  10.                 View vv = (View) this.getListView().getSelectedView();
  11.  
  12.                 TextView tv = (TextView) vv.findViewById(R.id.row_entry);
  13.  
  14.                 tv.setVisibility(View.VISIBLE); selectedTV = tv;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


but the same exception :( NullPointer

Did I added the delay in wrong way?? or in worng place??

Thanks plusminus for your help

regards,
code
User avatar
code
Junior Developer
Junior Developer
 
Posts: 21
Joined: Sat Feb 09, 2008 6:24 pm
Location: UAE

Postby plusminus » Tue Feb 12, 2008 3:32 pm

Hello code,

sorry I should have specified further. I meant like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 new Handler().postDelayed(new Runnable(){
  2.  
  3.                         @Override
  4.  
  5.                         public void run() {
  6.  
  7.                                 /* Create an Intent that will start the Menu-Activity. */
  8.  
  9.                                 Intent mainIntent = new Intent(Splash.this,Menu.class);
  10.  
  11.                                 Splash.this.startActivity(mainIntent);
  12.  
  13.                                 Splash.this.finish();
  14.  
  15.                         }
  16.  
  17.                 }, SPLASH_DISPLAY_LENGHT);
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


it will cause the GUI-Thread to execute the Runnable after X milliseconds.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests