Android Hello World tutorial using Eclipse for beginners - Step by Step Android SDK implementation

Android is a Linux-based operating system first introduced on Nov. 5, 2007, was originally developed by Android Inc. and subsequently purchased by Google. It primarily runs on cell phones, but Android devices do so much more than just act as a phone. The Android open-source software stack consists of Java applications running on a Java-based, object-oriented application framework on top of Java core libraries running on a Dalvik virtual machine featuring JIT compilation.

Google Android platform is fast becoming one of the most popular development platforms for your mobile devices. As more people are using their mobile devices for things normally done from a laptop or desktop means your customers or other internal employees such as sales people will soon start expecting you to offer them the capability to communicate with your data and business logic through mobile devices.

Setting Up Your PC for Android Development


Things you need in your PC to develop an Android application. The best part, you can download them for free:

1) Java Development Kit (JDK) of 1.5 or higher
Tip: You can skip this step and go to Step 3. When the Android Installer starts it will check your system for Java SDK and if not found will give you the link to install the Java SDK. After that it will install the Android SDK. Then go and take care of Eclipse in Step 2.

2) Eclipse Java Developer IDE (Eclipse Ganymede or above installation)

3) Android SDK (Software Development kit)

4) Eclipse plugin for Android Development
  • Use URL https://dl-ssl.google.com/android/eclipse/ to access the Android Development Tools Plugin (ADT)

5) Android Virtual Device
  • Its just a simulator so that you don't have to have a Android Phone to TEST plus its easy to do it this way for development.

Step by Step to get Hello World ! running on Android

Note: You don't have to do all the steps if you already have Java SDK or Eclipse setup on your computer.

Setup Java Development Kit

You may already have Java installed on your computer but it may be just the JRE (Java Runtime Environment). You need to make sure that you have JDK (Java Development Kit) version 1.5 or higher. You can go to the Oracle website(http://www.oracle.com) and get it. Here is a direct link to the JDK 1.6 version.

https://www.java.com/en/download/

Setup Eclipse

Eclipse is a multi-language software development environment comprising an integrated development environment (IDE) and an extensible plug-in system. It is written mostly in Java and can be used to develop applications in Java and, by means of various plug-ins, other programming languages including Ada, C, C++, COBOL, Perl, PHP, Python, R. Ruby (including Ruby on Rails framework), Scala, Clojure, and Scheme. The IDE is often called Eclipse ADT (Ada Development Toolkit) for Ada, Eclipse CDT for C/C++, Eclipse JDT for Java, and Eclipse PDT for PHP.

You can go to http://www.eclipse.org to download or

Here is the direct link
http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/heliossr2

Eclipse is easy to install
Once you have unzipped the previously downloaded file, you will see a folder named eclipse. In that folder you will find the eclipse application (a big blue dot). We recommend you create a shortcut on the desktop to simplify the launching of eclipse. Notice that unlike Java, Eclipse does not have an installation process. Once you have unzipped the file you are done.

After being launched Eclipse will ask you to specify the workspace to use. The workspace is a folder used by eclipse to keep all your work. Specify an already existing folder or accept the default provided by Eclipse or provide a new folder.
Launch Eclipse
You are all set for now. If you want you can read the manuals or click the curved arrowed you will eclipse IDE.

Download the Android SDK


Here is the direct link to the Android SDK Installer
http://developer.android.com/sdk/index.html

Please note the SDK starter package is not a full development environment—it includes only the core SDK Tools, which you can use to download the rest of the SDK components (such as the latest Android platform).

For more information about installing the Android SDK click here
http://developer.android.com/sdk/installing.html

Install Eclipse plugin for Android Development


Start Eclipse if has not been started. Now go to Help > Install New Software
Eclipse New  Software Install
Click Add to to add New Repository, basically the site where the software is.
Name = ADT Plugin
Location = https://dl-ssl.google.com/android/eclipse/
Eclipse New  Software Install
Then Select your resource URL that you just added and check the box that say Developer Tools. After that take all the default options, basically click Next, Finish and accept License whatever it takes. Once it does the Install it will ask you to Restart the Eclipse, just take YES.
Eclipse New  Software Install

Eclipse is now setup for Android development

You should see the the Android SDK and AVD Manager under Window menu.
Eclipse Android SDK and AVD Manager
Click on Android SDK and AVD Manager and then click on Installed packages. If you don't see the Android SDK and documentation as shown below then click on Available packages and Install them.
Eclipse Android SDK and AVD Manager

Time to create the Android Virtual Device (AVD)


AVD is a Android Phone emulator so that you can TEST your applications without having an actual Android Phone. To deploy applications in the emulator, you need to define an Android Virtual Device (AVD) that defines the characteristics of your device.

Select Virtual Devices in the left panel then Click New.
Eclipse New Android Virtual Device
The Create New AVD dialog appears.
Type the name of the AVD, such as "MyDroid3.0".
Choose a target. 
    The target is the platform (that is, the version of the Android SDK, such as 3.1 or 3.0) you want to run on the emulator. My personal preference is always go one or two levels down. Saves me a bunch of time as the latest ones gives more trouble.

    You can ignore the rest of the fields for now. Click Create AVD.

    Create Hello World Android Project


    From Eclipse, select File > New > Project.
    New Android Project
    If the ADT Plugin for Eclipse has been successfully installed, the resulting dialog should have a folder labeled "Android" which should contain "Android Project". (After you create one or more Android projects, an entry for "Android XML File" will also be available.)

    Select "Android Project" and click Next.
    Android Hello World Tutorial Project
    Fill in the project details with the following values:

    Project name: Android Tutorials
    Application name: My first Android Program, Hello World
    Package name: com.as400samplecode (or your own private namespace)
    Create Activity: HelloWorld

    Here is a description of each field:

    Project Name
    This is the Eclipse Project name — the name of the directory that will contain the project files.
    Application Name
    This is the human-readable title for your application — the name that will appear on the Android device.
    Package Name
    This is the package namespace

    Create Activity
    This is the name for the class stub that will be generated by the plugin. This will be a subclass of Android's Activity class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn't need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.

    Analyzing the Android Hello World Project

    Once the Hello World project is created you will the following in your Eclipse Package Explorer.
    Android Hello World Tutorial Project
    The main Components that you need to look here are. Take your time but don't modify yet !

    src > com.as400samplecode > HelloWorld.java
    res > layout > main.xml
    res > values > string.xml

    Let's run the application. We will come back and play with these later ....

    Run the Hello World Application


    The Eclipse plugin makes it easy to run your applications:

    Select the project "Android Tutorials"
    Select Run > Run As.
    Select "Android Application".

    The Eclipse plugin automatically creates a new run configuration for your project and then launches the Android Emulator. Depending on your environment, the Android emulator might take several minutes to boot fully, so please be patient. When the emulator is booted, the Eclipse plugin installs your application and launches the default Activity. You should now see something like this (depends on what version you chose for the AVD emulator).
    Android Hello World Tutorial Project

    Modify our Hello World Program

    Go to the strings.xml file under src > values. It should look like this.
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, HelloWorld!</string>
        <string name="app_name">My first Android Program, Hello World</string>
    </resources>
    

    These are the values getting displayed on the AVD emulator and gets mapped to the layout in main.xml
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"/>
    

    So go ahead and change this values and see the results ! Follow the same steps to run the application as above but this time it will not take that much time as the AVD was already up and running.

    Alternate way to modify the Hello World application

    You can edit the HelloWorld.java directly as directed below

    Replace the following code:
    setContentView(R.layout.main);

    With the code given below
    TextView textView = new TextView(this);
    textView.setText("Hello World!");
    setContentView(textView);

    Tip: In case you are getting errors is you need to import the missing java packages. An easy way to add import packages to your project is to press Ctrl-Shift-O (Cmd-Shift-O, on Mac). This is an Eclipse shortcut that identifies missing packages based on your code and adds them for you.


    Here you are not using the layout main.xml anymore. I am not a great fan of this. You should always use Model View Controller (MVC) architecture when available as its easy to maintain applications that way and different skillset can work on different components.

    All done with Hello World, you can mess around with the main.xml and strings.xml file !

    Recommended Reading

    No comments:

    Post a Comment

    NO JUNK, Please try to keep this clean and related to the topic at hand.
    Comments are for users to ask questions, collaborate or improve on existing.