Installation done lets do some code

Before we can start coding we obviously have to create a new Android Project.

Creating a new Android Project

1. The first thing that needs to be for every Android Application is to create a new Android Project. To do that, simply open the Package Exlporer in Eclipse right-click on some blank space and choose:

Picture 7 First Android Application - Step 1

2. Select:

Android > Android Project

Picture 8 First Android Application - Step 2

3. Fill out the form with values fitting your applications purpose.

Picture 9 First Android Application - Step 3

4. This are all the files for your first Android-Application (don't panic, mostly all of them are resource-files)

tl Package Explorer Hierarchy^

J> 3DColorCube

r* lS Hello.Android

[> ¡3 src

[:=• Sli Referenced Libraries

J

& assets

t> & res

^ O AndroidManifest.xml A

> Listviews

> ts? PizzaMapApp

Picture 10 First Android Application - Step 4

Huh, what are all that files supposed to do ?

As you now have created your first Android Project you will see a bunch of files appear in that new project.

The Main Activity

You'll see some default code like the following, when you now navigate to:

"/src/your package Structure/Hello Android.java"

package org.anddev.android.Hello_Android;

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

public class Hello_Android extends Activity {

/** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); /* Make this application use

* the main.xml-layout-file. */ this.setContentView(R.layout.main);

We could now immediately O our Application but let me explain of the other files too.

The XML-Layout (main.xml)

The ADT created this very basic Activity for you. As we know Activities are somehow like JFrames in Swing. In Android we extend from Activity and need to overwrite a single function, called onCreate(...). Within this function we have to call i.e.

setContentView(R.layout.main) which makes our Activity use the main.xml which was also created by the ADT:

<?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, Hello_Android"/>

</LinearLayout>

We have a 'fullscreen' vertical LinearLayout, that contains a single TextView showing a pre-defined String.

The AndroidManifest.xml

So let's face the AndroidManifest .xml:

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="org.anddev.android.hello android">

<application android:icon="@drawable/icon">

<activity android:name=".Hello Android"

android:label="@string/app name">

<intent-filter>

<action android:name="android.intent.action.MAIN"

/>

<category

android:name="android.intent.category.LAUNCHER"

/>

</intent-filter>

</activity>

</application>

</manifest>

Every xml-file starts with the following line, it defines the xml-version and encoding-type of the xml-text. Just copy-paste it to every new file.

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

As we know the outermost tag in every xml-file should contain this attribute:

xmlns:android="http://schemas.android.com/apk/res/android"

Because it makes a variety of general Android attributes available in the file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.anddev.android.hello_android">

</manifest>

The <application> tag is the root element containing declarations of all application-level components contained in the package. This element can also include global and/or default attributes for the application, such as a label, icon, theme, required permission, etc.

Here we define just the icon, by making a '@-refer' to a image placed under "/res/drawable/".

<application android:icon="@drawable/icon"> </application>

Inside of the <application> tag we need to define all Activities/IntentReceivers that need to be started via Intents. In this case we have only one simple Activity.

<activity android:name=".Hello_Android"

android:label="@string/app_name">

</activity>

You probably have recognizes that the package-attribute from the <manifest> tag together with the android:name attribute from the <activity> tag always result in the complete package-path to the Activity described.

The innermost tag is the <intent-filter> tag which defines on which Intents we want to listen. Here we want the Hello_Android Activity launchable from within the emulator by clicking the icon (that was defined in the <application> tag).

<intent-filter>

<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

(D Note: The debugging-process with a Android Application is exactly the same than with a normal Java Application.

Running your first application

Now we need to create a '^-Configuration'. Open the U'-DropDown in the Eclipses upper menu and Click "Open Run Dialog..."

Picture 11 Opening the run-dialog
Picture 12 Creating a run-configuration

From now on you can run your application by clicking the G button. Having done that you'll see this:

Hello_Android - by anddev.org

Hello World, Hello_Android

Picture 13 First Android Application - The result (sdk-version m5)

After the first deploy you can also see your application being listed in the quick-menu, displayed with the default icon:

| (£) Q 10:04 PM

WF-*-

V

API Demos

Dialer IHello.Android

Maps

Contacts Dialer

All '

Picture 14 First Android Application - Added to QuickMenu (sdk-version m5)

0 0

Post a comment