Android Tutorials Hello World

Pre-Tasks

Setup Eclipse and Android SDK.

Install a Platform

In the Android SDK and AVD Manager, choose Available Packages and install from repository.

Create an AVD

Select Window > Android SDK and AVD Manager
Select Virtual Devices > New, enter type (version) and name

Create the Project

Select File > New > Project…. > Android Project
Fill project name, build target(AVD), Application name, package name, create activity, min sdk ver

Explore Activity

  • An Activity is a single application entity that is used to perform actions.
  • An application may have many separate activities, but the user interacts with them one at a time.
  • The onCreate() method is called by the Android system when your Activity starts — it is where you should perform all initialization and UI setup.
  • An activity is not required to have a user interface, but usually does.

HelloAndroid.java

package com.example.helloandroid;
 
import android.app.Activity;
import android.os.Bundle;
 
public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Construct the UI

Modify HelloAndroid.java

package com.example.helloandroid;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class HelloAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);
   }
}

The following code piece create a TextView and set the text to "Hello, Android".

       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);
  • A View is a drawable object used as an element in your UI layout, such as a button, image, or (in this case) a text label.
  • Each of these objects is a subclass of the View class and the subclass that handles text is TextView.
  • TextView has the class constructor, which accepts an Android Context instance as its parameter.
  • A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on.
  • The Activity class inherits from Context.
  • Pass the TextView to setContentView() in order to display it as the content for the Activity UI.

Run the Code

Select Run > Run > Android Application

Application titled with "Hello, Android" is started.

The application title is set by res/values/strings.xml

    <string name="app_name">Hello, Android</string>

and referenced by AndroidManifest.xml
        <activity android:name=".HelloAndroidActivity"
                  android:label="@string/app_name">

Upgrade the UI to an XML Layout

Using XML Layout, we can avoid the overhead on manage source code and the errors such as forget connect views.

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/textview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:text="@string/hello"/>

res/values/ folder, open strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello, Android! I am a string resource!</string>
    <string name="app_name">Hello, Android</string>
</resources>

HelloAndroid.java

package com.example.helloandroid;
 
import android.app.Activity;
import android.os.Bundle;
 
public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

TextView has five XML attributes.

  • xmlns:android - XML namespace that tells the Android tools that you are going to refer to common attributes defined in the Android namespace.
  • android:id - A unique identifier of the TextView element
  • android:layout_width - Width
  • android:layout_height - Height
  • android:text - Text, which can refer the res/values/strings.xml. see Android Resources and Internationalization for more information.

After select Run > Run > Android Application . R.java is generated automatically. (Or you can generate it with ant).
R.java

package com.example.helloandroid;
 
public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int textview=0x7f050000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}

Debug Your Project

We can debug android project just as normal java project.
i.e. add the following piece of code to HelloAndroid.java and set a breakpoint to it.

Object o = null;
        o.toString();

Creating the Project Without Eclipse

Android has tools, like the emulator, aapt, adb, ddms runs under command-line mode and can be run under ant.
And android tool can be used to create all the source code and directory stubs for your project, as well as an ant-compatible build.xml file.

android create project \
    --package com.example.helloandroid \
    --activity HelloAndroid \
    --target 2 \
    --path <path-to-your-project>/HelloAndroid

See Also

Android入門 Hello World

Reference

http://developer.android.com