A good introduction to a new programming language or a good way to test a freshly configured development environment, is to write a small program called “Hello World” and see if the program executes, correctly.
This tutorial covers how to write (setup) “Hello World” so that the program will execute and display on an Android Device, through the UI (touch screen).
To begin, verify the computer has been setup for development for an Android device. If not, then please refer to the link to learn how.
After the computer has been setup and configured for Android development, then launch Eclipse and create a new work space called HelloWorldDroidJava.
Next, through Eclipse’s File menu select File-New-Project…, to bring up the New Project dialog.
Within the New Project dialog select within the drop-down tree “Android and Android Application Project”, and then click the Next button to start the configuration wizard for building an Android application project.
After clicking the Next button, the “New Android Application” dialog will pop up. Please, fill in the text-box “Application Name” with “Hello World – Java”. The project and package names should automatically fill in with default names. However, if desired these text-boxes can also be modified, by typing different names.
The minimum, target and compiled SDKs refer to the SDKs needed to target a range of Android OSs for the application to run on. The website, AppBrain
is a good reference to figure out the desired entries for these combo boxes. With the entries configured below, the application should be able to run on 87.6% of the estimated Android devices being used (at the time of this writing).
After indicating to the wizard that the application is going to need a launcher icon and an activity then it is time to configure the app’s launching icon. In the following screen capture, the launcher icon’s configuration settings are left in their default format. However, it is okay to modify the settings to see the resulting outcome.
Next, it is time to create the activity. Since the “Hello World” app isn’t going to do much, but just display some simple characters spelling out “Hello World” then all that is required for the app to operate is a “Blank Activity”.
The next step in the wizard, is to name the activity. As seen below, the default name for the activity is fine. After naming the activity, click the Finish button to end the project’s configuration process.
After the project’s configuration has been completed then it is time to bring up an Eclipse perspective that will allow for simple Android development. By default Eclipse will display an introductory tab, as seen below.
If the tab is up then simply click on the tab’s ‘X’ close icon, next to the text ‘Welcome’. By closing the introduction tab Eclipse will bring up the default Java perspective.
At the bottom of the perspective there is a tab with the text “Console”. If the tab is not selected then please select the tab, to see its contents. If the console is displaying the error message “… AndroidManifest.xml:11: error: Error: No resource found that matches the given name (at ‘icon’ with value ‘@drawable/ic_launcher’).” then the manifest is not configured to look in the correct set of directories for the launcher icon. The manifest is setup to look in the drawable directories.
The error is normal, given the app is setup to run on many different Android devices. In earlier releases of the SDKs the launcher icons were located in the drawable directories, but has since moved into the mipmap folders with the newer SDKs.
To begin to correct the error double click on AndroidManifest.xml within the Package Explorer.
To the right of the Package Explorer the Manifest tab should appear, with another set of tabs below the group of displayed text-boxes. Within the set of tabs select the tab labeled AndroidManifest.xml.
Within the displayed XML source-code change the attribute android:icon from “@drawable/ic_launcher” to “@mipmap/ic_launcher”, within the application tag.
Note, if the folders res/mipmap-_hdpi is expanded (as below) the various launcher icons are revealed. Each launcher icon will be appropriately displayed on the user’s Android device depending on the user’s screen resolution.
To run the application through an Android emulator, start by performing a right mouse click on the project name, within the Package Explorer — then select Run As-Android Application.
If this is the first attempt to run an Android application through an emulator, then the following dialog will appear – select yes.
The dialog Android Device Chooser will appear. Within the dialog choose the radio-button “Launch a new Android Virtual Device” and then click the button “Manager…”.
Within the Android Virtual Device (AVD) Manger select the Device Definitions tab.
Within the tab choose a default emulator to run the app-Nexus S is one the easier ones to setup.
Within the emulator configuration, verify the Target API is within the range of targeted APIs (SDKs) chosen earlier, [16, 22]. Also, verify or select the CPU matches the CPU for a mobile device (ARM) and the Hardware keyboard check-box is selected.
Once the configuration is completed the AVD (the emulator) should be displayed within the Manager, if not click on the Refresh button and then it should display. Exit the AVD manager to move onto the next step.
Once the AVD manager is closed then the focus should return to the Android Device Chooser. If the previously built AVD is not displayed then click the Refresh button. After the AVD is displayed within the table select the AVD and click the dialog’s Okay button to launch the emulator and the “Hello World” app.
It may take a while for the emulator to boot up, so be patient. Once the emulator is finished booting, the emulator will display an Android device log in screen, just wait another minute; and the application should launch and look similar to the following image.
Congratulations! You are now finished with writing “Hello World” in Java for an Android Device.
Where’s the Java source-code? In Eclipse expand the folders src and gen within the Project Explorer. The wizard auto-generated the Java code for the Hello World app, here. The folder src, is where the developer (you) will place your developed or modified code, in future projects.
Again, congratulations on the App, especially if it is your first! Please leave your comments, below.