Friday, December 7, 2012

Creating Your First Android Activity -- For Beginner


You’ve downloaded the SDK, installed Eclipse, and plugged in the plug-in. You’re now ready to start
programming for Android. Start by creating a new project and setting up your Eclipse run and debug
confi gurations.
Starting a New Android Project
To create a new Android project using the Android New Project Wizard:
1. Select File ➪ New ➪ Project.
2. Select the Android Project application type from the Android folder, and click Finish.
25

3. In the dialog that appears, enter the details for your new project. The
“Project name” is the name of your project fi le; the “Package name” specifi es its package; the
“Activity name” is the name of the class that is your initial Activity; and the “Application
name” is the friendly name for your application.


4. When you’ve entered the details, click Finish.
The ADT plug-in then creates a new project that includes a new class that extends Activity. Rather
than being completely empty, the default template implements “Hello World.” Before modifying the
project, take this opportunity to confi gure run and debug launch confi gurations.
Creating a Launch Confi guration
Launch confi gurations let you specify runtime options for running and debugging applications. Using
a launch confi guration you can specify the following:
❑ The Project and Activity to launch
❑ The emulator options to use
❑ Input/output settings (including console defaults)

You can specify different launch confi gurations for Run and Debug modes. The following steps show
how to create a launch confi guration for an Android application:
1. Select Run ➪ Open Run Dialog … (or Run ➪ Open Debug Dialog …).
2. Right-click Android Application on the project type list, and select New.
3. Enter a name for the confi guration. You can create multiple confi gurations for each project, so
create a descriptive title that will help you identify this particular setup.
4. Now choose your start-up options. The fi rst (Android) tab lets you select the project and Activity
that you want to start when you run (or debug) the application.The settings
for the project you created earlier.


5. Use the Target tab to confi gure the emulator. There are options to choose the screen size, device
skin, and network connection settings. You can also optionally wipe the user data on the emulator
and enable or disable the start-up animation. Using the command-line textbox, you can
specify additional emulator start-up options if needed.
6. Finally, set any additional properties in the Common tab.
7. Click Apply, and your launch confi guration will be saved.
Running and Debugging Your Android Applications
You’ve created your fi rst project and created the run and debug confi gurations for it. Before making any
changes, test your installation and confi gurations by running and debugging the Hello World project.
From the Run menu, select Run or Debug to launch the most recently selected confi guration, or select
Open Run Dialog … or Open Debug Dialog … to select a confi guration to use.
If you’re using the ADT plug-in, running or debugging your application:
❑ Compiles the current project and converts it to an Android executable (.dex).
❑ Packages the executable and external resources into an Android package (.apk).
❑ Starts the emulator (if it’s not already running).
❑ Installs your application onto the emulator.
❑ Starts your application.
If you’re debugging, the Eclipse debugger will then be attached, allowing you to set breakpoints and
debug your code.

If everything is working correctly, you’ll see a new Activity running in the emulator, as shown in


Understanding Hello World
With that confi rmed, let’s take a step back and have a real look at your fi rst Android application.
Activity is the base class for the visual, interactive components of your application; it is roughly
equivalent to a Form in traditional desktop development. The following snippet shows the skeleton
code for an Activity-based class; note that it extends Activity, overriding the onCreate method.
package com.paad.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 icicle) {
super.onCreate(icicle);
}
}
What’s missing from this template is the layout of the visual interface. In Android, visual components
are called Views, which are similar to controls in traditional desktop development.
In the Hello World template created by the wizard, the onCreate method is overridden to call
setContentView, which lays out the user interface by infl ating a layout resource, as highlighted below:
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}
The resources for an Android project are stored in the res folder of your project hierarchy, which
includes drawable, layout, and values subfolders. The ADT plug-in interprets these XML resources
to provide design time access to them through the R variable as described in Next Lesson.
The following code snippet shows the UI layout defi ned in the main.xml fi le created by the Android
project template:
<?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=”Hello World, HelloWorld”
/>
</LinearLayout>
Defi ning your UI in XML and infl ating it is the preferred way of implementing your user interfaces, as
it neatly decouples your application logic from your UI design.
To get access to your UI elements in code, you add identifi er attributes to them in the XML defi nition.
You can then use the findViewById method to return a reference to each named item. The following
XML snippet shows an ID attribute added to the TextView widget in the Hello World template:
<TextView
android:id=”@+id/myTextView”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Hello World, HelloWorld”
/>
And the following snippet shows how to get access to it in code:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
Alternatively (although it’s not considered good practice), if you need to, you can create your layout
directly in code as shown below:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout.LayoutParams lp;
lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
LinearLayout.LayoutParams textViewLP;
textViewLP = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView myTextView = new TextView(this);
myTextView.setText(“Hello World, HelloWorld”);
ll.addView(myTextView, textViewLP);
this.addContentView(ll, lp);
}
All the properties available in code can be set with attributes in the XML layout. As well as allowing
easier substitution of layout designs and individual UI elements, keeping the visual design decoupled
from the application code helps keep the code more concise.

No comments:

Post a Comment