CREATING YOUR FIRST APPLICATION
With all the tools and the SDK downloaded and installed, it is now time to start your engine! As in all programming books, the first example uses the ubiquitous Hello World application. This will enable you to have a detailed look at the various components that make up an Android project. So, without any further ado, let’s dive straight in!1 Using Eclipse, create a new project by selecting File ➪ Project…
CREATING YOUR FIRST APPLICATION
2. Expand the Android folder and select Android ProjectCREATING YOUR FIRST APPLICATION
3. Name the Android project as shown in FigureCREATING YOUR FIRST APPLICATION
4. The Eclipse IDE should now look like Figure.5. In the Package Explorer (located on the left of the Eclipse IDE), expand the Hello World project by clicking on the various arrows displayed to the left of each item in the project. In the res/layout folder, double-click the main.xml file.
6 The main.xml file defines the user interface (UI) of your application. The default view is the Layout view, which lays out the activity graphically. To modify the UI, click the main.xml tab located at the bottom.
CREATING YOUR FIRST APPLICATION
7. Add the following code in bold to the main.xml file:android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
android:layout_height=”wrap_content”
android:text=”@string/hello” />
android:layout_height=”wrap_content”
android:text=”This is my first Android Application!” />
8. To save the changes made to your project, press Ctrl+s.
9. You are now ready to test your application on the Android Emulator. Select the project name in Eclipse and press F11. You will be asked to select a way to debug the application. Select Android Application and click OK
CREATING YOUR FIRST APPLICATION
10. The Android Emulator will now be started (if the emulator is locked, you need to slide the unlock button to unlock it first). Figure shows the application running on the Android Emulator.11. Click the Home button (the house icon in the lower-left corner above the keyboard) so that it now shows the Home screen
12. Click the application Launcher icon to display the list of applications installed on the device. Note that the HelloWorld application is now installed in the application launcher (see Figure 1-28).
CREATING YOUR FIRST APPLICATION
How It WorksTo create an Android project using Eclipse, you need to supply the information shown in Table
In Android, an Activity is a window that contains the user interface of your applications. An application can have zero or more activities; in this example, the application contains one activity: MainActivity
This MainActivity is the entry point of the application, which is displayed when the application is started.
In this simple example, you modified the main.xml file to display the string “This is my first Android Application!” and a button. The main.xml file contains the user interface of the activity, which is displayed when MainActivity is loaded.
When you debug the application on the Android Emulator, the application is automatically installed on the emulator. And that’s it — you have developed your first Android application!
The next section unravels how all the various files in your Android project work together to make your application come alive.