Android Development Demo: “DialANumber” – How to write your own very first Android program – Made Simple!

While I think the Android development examples are not really good and straight forward perhaps some of you come along with them. However, I would like to provide you a very very simple example here on how to write your own first Android program.

This is technically very easy. Once you got Eclipse and Android installed and running on your computer (I use a Mac OS Leopard powered MacBook but you can use a Microsoft Windows XP/Vista/etc. PC as well) you just start Eclipse. Now you just go to “New » Project... » Android » Android Project...” and enter the required data. Once you are done the result might look like this:

Android Development Demo: “DialANumber”

Now you can start writing and customize the default “Hello World” a.k.a. “Hello Android” app which just shows a text field. If you are really really lazy you could also copy/paste the following code:


package com.DialANumber;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class DialANumber extends Activity {
  EditText mEditText_number = null;
  LinearLayout mLinearLayout_no_button = null;
  Button mButton_dial = null;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mLinearLayout_no_button = new LinearLayout(this);

    mEditText_number = new EditText(this);
    mEditText_number.setText("5551222");
    mLinearLayout_no_button.addView(mEditText_number);

    mButton_dial = new Button(this);
    mButton_dial.setText("Dial!");
    mLinearLayout_no_button.addView(mButton_dial);
    mButton_dial.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        performDial();
      }
    });

    setContentView(mLinearLayout_no_button);
  }

  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_CALL) {
      performDial();
      return true;
    }
    return false;
  }

  public void performDial(){
    if(mEditText_number!=null){
      try {
        startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mEditText_number.getText())));
      } catch (Exception e) {
        e.printStackTrace();
      }
    }//if
  }
}

That was quick! 🙂

Now you should create a debug runtime configuration which can be done at “Run » Debug Configurations…“. Here you select “Android Application” and press “New“. Enter a name and select the “DialANumber” project. Launching the default Activity might be a good idea as we not know what the f*** it means right now. 🙂 So just hit “Apply” and “Debug” and the program will be started in the Android simulator. To give you a clue how that looks like here are some screen shots:

Android Development Demo: “DialANumber”

Android Development Demo: “DialANumber”

As there is an EditField for the phone number which can be modified you can also enter a new phone number in there. An example of this comes here:

Android Development Demo: “DialANumber”

Android Development Demo: “DialANumber”

As everyone can see we are able to dial the number in two different ways:

  1. Hit the “Dial!” button next to the input field.
  2. Hit the “green dial” button on the bottom of the device.

Here is an illustration of this:

Android Development Demo: “DialANumber” - How to write your own very first Android program - Made Simple!

That’s it. Pretty easy, huh? 🙂

I am looking forward to find some time in the near future to pimp this example a bit. I am thinking of a number field filter adding (which looks a bit confusing in the API documentation) or something else. Suggestions are welcome!

In case someone is really lazy interested you can download the whole example project here: dialanumber.zip.

Please follow and like us:

34 thoughts on “Android Development Demo: “DialANumber” – How to write your own very first Android program – Made Simple!”

  1. Sir, m new to programming on Android platform.

    i tried to run HelloAndroid program in Eclipse(Ganymede) IDE and the console showed the following message:

    [2009-02-10 09:10:32 – HelloAndroid] ——————————
    [2009-02-10 09:10:32 – HelloAndroid] Android Launch!
    [2009-02-10 09:10:32 – HelloAndroid] adb is running normally.
    [2009-02-10 09:10:32 – HelloAndroid] Could not find HelloAndroid.apk!

    m not able to fix the error. could u please help?

    what are the enviroment variables to be set? should i set any env var of the jdk to anything particular?

    could you please help and thanks.

  2. what are the enviroment variables to be set? should i set any env var of the jdk to anything particular?

    Standard!

  3. hi, i have to write a program for school, that uses the gps to find my location and then tells me which restuarants are near by and then pulls up thier menus and nutritional information and then it keeps track of how many calories i have eaten for the day. I am totally lost and have no idea what i am doing. I need help!

  4. Hi rebeka!
    I cant give you concrete information just some tips, i hope you are familiar with java-programming. If not you should read the book “hello android” by ed burnette. After that you you know how to get your GPS-Position and using also the Maps from Google, its all built-in in the SDK. Then you should find a way how to get Information about near Reastaurant that are close to your GPS-Position, i think google-maps is also providing functions for that. Then you must make those Restaurant-icons clickable, so that a new Menu shows up where the menus are listed like “Rice 10cal, Sprite 10cal etc.” I would use Dummy-Data for that, cause how should you know what menu they have and how you should get that information. You can make that menu from the type “spinner” and scroll through it and then select a item and then click on a button eat. In the background you are adding the cal values to an Integer or float-Variable. With the “Go back” button you came back to the map. I think you can overlay the number of calories on the map, so you can always see it. Gud luck!

  5. Hi, i just tried your piece of code and when i push the button “DIAL” the call doesn’t start.
    In the console windows i got a red Warning as follow “ActivityManager: Warning: Activity not started, its current tast has been brought to the front”

    What ‘s wrong?

  6. Please, try to dial “911”, i think it doesn’t allow to make it.
    But I’ve found following permissions (How can I make a dial “without going through the Dialer user interface”?):

    String CALL_PHONE Allows an application to initiate a phone call without going through the Dialer user interface for the user to confirm the call being placed.
    String CALL_PRIVILEGED Allows an application to call any phone number, including emergency numbers, without going through the Dialer user interface for the user to confirm the call being placed.

  7. Thanks for this….. I’m new to programming as such and I am looking at using this code for switching on a multi sim… eg send 001 / 002 etc…. I’ll let you know if I get any where…..

    Terran

  8. I tried your tutorial and I get a lot of it but it does not work on my side. I tried different numbers but cannot make it to work.

    Any suggestions why dialing does not work? Please email me back

    Thanks.

  9. Hey,

    Thanks for the code snippet! I’ve actually started a blog devoted to code snippets like these and thought I’d reach out and maybe you could check it out?

    http://thinkandroid.wordpress.com

    It’s a pretty young blog (~ 3 weeks old now) but it’s doing pretty well… getting about 200-300 hits a day. And yea I’ve wanted to reach out to other blogs/sites like mine to help connect my site with others and ultimately help developers reach my site for potentially useful tutorials or examples.

    But yes let me know what you think! Hope to hear back!

  10. sir,
    i try to workout the code,but it’s not working after clicking the dial button.i am new in the android platform.can you please suggest a text for refer ‘android’ .can u please give suggestion through mail. Thanks.

  11. To all those having issues with this demo app… take a look at this video from Google .. they explain how to do this in a few lines of code.

  12. hi we are doing project on beagle board we ported android OS on it we connected LCD to the s-video of beagle board after porting android OS on it on lCD we got icons like contacts, brouser,etc but when connected my gprs modem to the beagle board it is not responding for that there will be one application is there my question is

    how we build the gprs app in mobile android OS please reply me as fast as possible.

  13. For those of you, who have trouble geting it to actually dial.
    What is missing is that you need to add a permission in the AndroidManifest.xml

    add this code to the manifest, at the end, right before

    you can also click the Permissions tab, click the button Add, then in the namelist choose, android.permission.CALL_PHONE and the code will get added in the manifest.

    this shuld make your appication call the number enterd.

  14. If you want to be able to dial MMI/USSD codes from this script, you will need to make the following changes.

    First add these permissions to the android manifest:
    android.permission.CALL_PHONE
    android.permission.CALL_PRIVILEGED android.permission.MODIFY_PHONE_STATE

    and alter the dialing line to Uri encode “#” and “*” characters.

    Change this line:

    startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(“tel:” + mEditText_number.getText())));

    to:

    startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(“tel:” + Uri.encode(mEditText_number.getText().toString()))));

    I hope that helps anyone trying to send MMI/USSD codes.

  15. Hi, i just tried your piece of code and when i push the button “DIAL” the call doesn’t start.
    In the console windows i got following message in last line
    [2010-08-27 14:39:12 – AndroidProject] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.cube/.AndroidProject }

    What ’s wrong?

  16. m new for android programming so i would lik to writ prg of android
    so plz help
    how to study
    which point i refer in java

  17. hey
    i have an assignment for the semester on how to write a source code in android for a hangman game
    i dont want the whole source code but i have a very slight idea about android so i was wondering if you have some suggestions for a code or hints:)

  18. Hi,
    Am using Froyo – 2.2.1 – I tried the approach for call forwarding – by encoding the “#” (encoding “*” does not make any difference). But I am still not able to invoke automatic call forwarding using this approach. ACTION_DIAL works fine. The manifest has all the above permissions.
    Is this an intended approach ? I am trying this on a phone in AT&T network using *21*PH_NUMBER#
    Any insight will be useful….
    Thanks,
    Nish

  19. I want to know how to insert image in this project(corresponding phoneno’s image).Thank you in advance

Comments are closed.