Pages

Saturday, 9 April 2011

First Steps (3) My First Android Project

Here I will run through the steps for creating your first Android project. I assume that you have a fully installed Eclipse with Android SDK environment and you know how to create an AVD (Android Virtual Device).

1.Open Eclipse then select from the top menu; File > New > Other...


2. Select Android Project, which is under the Android folder in the list:

3. Fill in the required project properties:
  • Project name: MyAndroidProject
    Project name within Eclipse and folder name within the workspace.
  • Contents: (leave as defaults)
  • Built Target: Google APIs 2.2
    The target version of the Android SDK. Generally I target the most common Android version running. (currently 2.2)
  • Application name: MyAndroidProject
    The name of the app which displayed to the user and below the icon on the device.
  • Package name: com.androidelements.MyAndroidProject
    The namespace of your project. The convertion is to have your company name followed by the project name with a three character type prefix (com, meaning company).
  • Create Activity: Main
    The name of the first activity to load.
  • Min SDK Version: 8
    The minimum Android SDK version to support. Using the same as the target version for this example.



4. Click Finish (skipping the Test creation step for this example).

The project is now created and available within Eclipse.

5. Expand the "src" (source) folder, then the package folder and you will see the Main.java activity class. Double-click on this to view the code within Eclipse. You should see the following code

package com.androidelements.MyAndroidProject;

import android.app.Activity;

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

This is our Activity called "Main" which is inheriting from the Activity class. The onCreate method is called when the activity starts. Note that the setContentView method has the parameter R.layout.main.
R is the generated resources file and the reference is to the main.xml file in layout.

5. Expand res - layout then double-click main.xml

You will see a preview of the layout in the Graphical Layout window which will display "Hello World, Main!". Click on the tabe below this which is labelled "main.xml". The XML will now be visible:

<?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>

This is a 'linear' layout with a single TextView element. A linear layout stacks 'views' elements in the order they are decalred. The text view is a label.

6. We are going to add a button to this layout:

<?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"
    />
 <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click me"
    android:onClick="clicked"
    />
</LinearLayout>

7. Save and close the XML file to return to the code. We need to add the handler for the clicked event called by the button. This method must take one parameter that contains the reference to the calling View (the button in this case):

    public void clicked(View view) {
        Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
    }

8. You will require 2 import references at the top of the code to refer to the View and Toast classes.

    import android.view.View;
    import android.widget.Toast;

9. Now we are ready to launch the app. Click on the Android SDK and AVD Manager from the toolbar (or Window menu)

10. Select an AVD (Android Virtual Device) that is configured for the platform and API versions 8 then click on the Start.. button then on the next screen leave the default and click Launch.

11. Now click the "Run As" button (it looks like a standard play button) then select Android Application and click the OK button.


12. Your app should now load within the emulator. Click your "Click me" button and the toast message should appear.



And that is it. Simples.

0 comments:

Post a Comment