Android Toy

“Hello World” with Java Using Eclipse for Android Development

All, Game & Software Engineering, Programming Languages
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.

Picture

Next, through Eclipse’s File menu select File-New-Project…, to bring up the New Project dialog.

Picture

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.

Picture

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).

Picture

On the next dialog, leave the default configured project settings, as in the screen capture below.  The check-boxes are used to indicate that the app is going to need a launcher icon as well as an activity.  “An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so” for example “the Activity class takes care of creating a window for you…“.

Picture

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.

Picture

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”.

Picture

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.

Picture

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.

Picture

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.

Picture

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.

Picture

To begin to correct the error double click on AndroidManifest.xml within the Package Explorer.

Picture

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.

Picture

Within the displayed XML source-code change the attribute android:icon from “@drawable/ic_launcher” to “@mipmap/ic_launcher”, within the application tag.

Picture

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.

Picture

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.

Picture

If this is the first attempt to run an Android application through an emulator, then the following dialog will appear – select yes.

Picture

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…”.

Picture

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.

Picture

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.

Picture

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.

Picture

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.

Picture

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.

Picture

Congratulations!  You are now finished with writing “Hello World” in Java for an Android Device.

Picture

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.

Picture

Again, congratulations on the App, especially if it is your first!  Please leave your comments, below.

Please, keep your comments family friendly and respectful of each other and the author.