Saturday, January 1, 2011

Create New Project & Hello World

Today topic will cover on how to create a new project in Eclipse (Helios 3.6) with develop an apps called Hello World.
Tips: You may skip any of the sentence with gray in color if you want to have a fast tutorial

*If you are a typical programmer, sure you will know what is Hello World. Hello World is the simplest program/ application that can be ran without error for testing purpose. For me, it is a program that shows the whole picture how an application works. To those who want to know more, this is a full description for it.

Todo List
  1. Create Android Virtual Device(AVD) - one AVD can be used for all applications
  2. Create New Project
  3. Insert programming coding to shows Hello, World! using HelloWorld.java
2. Create New Project
  • In Eclipse, select File > New > Project
  • Expand Android > Android Project and click Next


  • Insert Project Details
    Project Name : HelloWorld
    Application Name : Hello, World
    Package Name : com.example.helloworld (or your own private namespace, that means anything that ease you to memorize for future usage)
    Create Activity : HelloWorld
    Click Finish

    Project Name
    This is the Eclipse Project name — the name of the directory that will contain the project files.
    Application Name
    This is the human-readable title for your application — the name that will appear on the Android device.
    Package Name
    This is the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity will be generated.

    Your package name must be unique across all packages installed on the Android system; for this reason, it's important to use a standard domain-style package for your applications. The example above uses the "com.example" namespace, which is a namespace reserved for example documentation — when you develop your own applications, you should use a namespace that's appropriate to your organization or entity.
    Create Activity
    This is the name for the class stub that will be generated by the plugin. This will be a subclass of Android's Activity class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn't need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.
    Min SDK Version
    This value specifies the minimum API Level required by your application. For more information, see Android API Levels.
3. Insert programming coding to shows Hello, World! using HelloWorld.java
  • Your project is ready. In Eclipse left panel (Window > Show View > Project Explorer)
    Open the HelloWorld.java located in HelloWorld > src > com.example.helloworld.



  • It should looks something like this:

    package com.example.helloworld;

    import android.app.Activity;
    import android.os.Bundle;

    public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }
    }

  • Lets modify some code above.
    package com.example.helloworld;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this); //create object
    tv.setText("Hello, Wolrd!"); //set text
    setContentView(tv); //display object
    //setContentView(R.layout.main); //comment it so that the compiler will not run this line
    }
    }


  • Save changes (Ctrl + S).
    Run the application by select Run > Run (Ctrl + F11)
    Select Android Application.
    You should be able to see something like this.



    That is all for the moment. Next post will be a little bit advance by using xml to show the same result (Hello, World!), it is easier if you are familiar with xml coding.
    Please left a comment if you are confused.

No comments:

Post a Comment