Recipe Creating a Project and an Activity

A straightforward way to create an Android project or any of its components is to use the Eclipse Integrated Development Environment (IDE).This method ensures proper setup of the supporting files.The steps to create a new Android project are

1. In Eclipse, choose File ^ New ^ Android Project.This displays a New Android Project creation screen.

2. Fill in the Project name, such as SimpleActivityExample.

3. Select a Build Target from the choices provided. These choices are based on the Software Development Kit (SDK) versions that are installed on the development computer.

4. Fill in the Application name, such as Example of Basic Activity.

5. Fill in the Package name, such as com.cookbook.simple_activity.

6. To create the main activity in the same step, be sure Create Activity is checked and fill in an Activity name, such as SimpleActivity.

All activities extend the abstract class Activity or one of its subclasses. The entry point to each activity is the onCreate() method. It is almost always overridden to initialize the activity, such as setting up the UI, creating button listeners, initializing parameters, and starting threads.

If the main activity is not created with the project or another activity needs to be added, the steps to create an activity are

1. Create a class to extend Activity. (In Eclipse, this can be done by right-clicking on the project, choosing New ^ Class, and then specifying android.app. Activity as the super class.)

2. Override the onCreate() function. (In Eclipse, this can be done by right-clicking on the class file, choosing Source ^ Override/Implement Methods..., and then checking the onCreate() method.)

3. As with most overridden functions, it must invoke the super class method, too; otherwise, an exception may be thrown at run-time. Here, the super.onCreate() should be called first to properly initialize the activity, as shown in Listing 2.1.

Listing 2.1 src/com/cookbook/simple_activity/SimpleActivityjava package com.cookbook.simple_activity;

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

public class SimpleActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

4. If a UI is used, specify the layout in an XML file in the res/layout/ directory. Here it is called main.xml, as shown in Listing 2.2.

5. Set the layout of the activity using the setContentView() function and passing it the resource ID for the XML layout file. Here, it is R.layout.main, as shown in Listing 2.1.

Listing 2.2 res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />

</LinearLayout>

6. Declare the properties of the activity in the AndroidManifest XML file. This is covered in more detail in Listing 2.5.

Note that the string resources are defined in the strings.xml file in the res/values/ folder, as shown in Listing 2.3.This provides a central place for all strings in case text needs to be changed or reused.

Listing 2.3 res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="hello">Hello World, SimpleActivity!</string> <string name="app_name">SimpleActivity</string> </resources>

Now a more detailed look at the directory structure of this project and the additional auto-generated content is explored.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment