• No se han encontrado resultados

U N MUNDO SIN NÚMEROS

In document Las Aventuras Matematicas de Daniel (página 45-51)

Now you’re ready to build your first Android application. You’ll start by building a simple “Hello World!” program. Create the skeleton of the application by following these steps:

1. Launch Eclipse and select File ➤ New ➤ Project. In the New Project dialog box, select Android and then click Next. You will then see the New Android Project dialog box, as shown in Figure 2–5. Eclipse might have added “Android Project” to the New menu so you can use that if it’s there. There’s also a New Android Project button on the toolbar which you can use.

2. As shown in Figure 2–5, enter HelloAndroid as the project name, HelloAndroidApp as the application name, com.androidbook as the package name, and HelloActivity as the Create Activity name. Note that for a real application, you’ll want to use a meaningful application name because it will appear in the application’s title bar. Also note that the default location for the project will be derived from the Eclipse

workspace location. In this case, your Eclipse workspace is c:\android, and the New Project Wizard appends the name of the new application to the workspace location to come up with c:\android\HelloAndroid\. Finally, the Min SDK Version value of 4 tells Android that your

application requires Android 1.6 or newer.

3. Click the Finish button, which tells ADT to generate the project skeleton for you. For now, open the HelloActivity.java file under the src folder and modify the onCreate() method as follows:

/** Called when the activity is first created. */ @Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

/** create a TextView and write Hello World! */ TextView tv = new TextView(this);

tv.setText("Hello World!");

/** set the content view to the TextView */ setContentView(tv);

}

Eclipse should automatically add an import statement for android.widget.TextView. You might need to click the “+” sign next to the first import statement to see them all. If the import statement doesn’t get added automatically, be sure to add it yourself. Save the HelloActivity.java file.

To run the application, you’ll need to create an Eclipse launch configuration, and you’ll need a virtual device on which to run. We’re going to quickly take you through these steps and come back later to more details about Android Virtual Devices (AVDs). Create the Eclipse launch configuration by following these steps:

1. Select Run ➤ Run Configurations.

2. In the Run Configurations dialog box, double-click Android Application in the left pane. The wizard will insert a new configuration named New Configuration.

3. Rename the configuration RunHelloWorld.

4. Click the Browse… button and select the HelloAndroid project. 5. Under Launch Action, select Launch and select

com.androidbook.HelloActivity from the drop-down list. The dialog should appear as shown in Figure 2–6.

Figure 2–6. Configuring an Eclipse launch configuration to run the “Hello World!” application 6. Click Apply and then Run. You’re almost there. Eclipse is ready to run

your application, but it needs a device on which to run. As shown in Figure 2–7, you will be warned that no compatible targets were found and asked if you’d like to create one. Click Yes.

Figure 2–7. Eclipse warning about targets and asking for a new AVD

7. You’ll be presented with a window that shows the existing AVDs. (See Figure 2–8.) Note that this is the same window we saw earlier in Figure 2–4.You’ll need to add one suitable for your new application. Click the New button.

Figure 2–8. The existing Android Virtual Devices

8. Fill in the Create AVD form as shown in Figure 2–9. Set Name to DefaultAVD, choose Android 2.0 - API Level 5 for the Target, set SD Card to 32 (for 32MB) and leave the default HVGA for Skin. Click Create AVD. Eclipse will confirm the successful creation of your AVD. Close the Android SDK window by clicking OK.

NOTE: We’re choosing a newer version of the SDK for our Android Virtual Device, but our application could also run on an older one. This is okay because AVDs with newer SDKs can run applications that require older SDKs. The opposite, of course, would not be true: an application that requires a newer SDK won’t run on an AVD with an older SDK.

9. Finally, select your new AVD from the bottom list. Note that you may need to click the Refresh button for any new AVDs to show up in the list. Click the OK button.

Figure 2–9. Configuring an Android Virtual Device

NOTE: It might take the emulator a minute to emulate the device-bootup process. After starting up, you should see HelloAndroidApp running in the emulator, as shown in Figure 2–10. In addition, be aware that the emulator starts other applications in the background during the startup process, so you might see a warning or error message from time to time. If you see an error message, you can generally dismiss it to allow the emulator to go to the next step in the startup process. For example, if you run the emulator and see a message like “application abc is not responding,” you can either wait for the application to start or simply ask the emulator to forcefully close the application. Generally, you should wait and let the emulator start up cleanly.

Figure 2–10. HelloAndroidApp running in the emulator

Now you know how to create a new Android application and run it in the emulator. Next, we’ll look more closely at Android Virtual Devices, followed by a deeper dive into an Android application’s artifacts and structure.

In document Las Aventuras Matematicas de Daniel (página 45-51)