IJRSAT Issue 1-Volume 3-Developing an Android Application Using Eclipse Juno

Document Sample
IJRSAT Issue 1-Volume 3-Developing an Android Application Using Eclipse Juno Powered By Docstoc
					                Naresh * et al.
                International Journal for Research in Science & Advanced                                  ISSN 2319-2690
                Issue-1, Volume-3, 090-095
              Developing an Android Application Using
                            Eclipse Juno
                  Naresh Babu M M and Y Sreeraman                               Dr. Ravindranath Cherukuri
                    Information Technology Department                    Electrical & Electronics Engineering Department
         Sreenivasa Institute of Technology & Management                Gyan Ganga Institute of Technology & Management
                        Andhra Pradesh, India                                            Bhopal, India
           itsnaresh4u@gmail.com, sramany@gmail.com                                ravindranathc@gmail.com

  Abstract -- In this paper, we present a step by step approach       applications are compiled into the Dalvik executables).
for beginning android application development on Eclipse using        Dalvik is a specialized virtual machine designed
the Android Development Tools (ADT) plugin which is a open            specifically for Android and optimized for battery-
source environment. We also specified the procedure to run the        powered mobile devices with limited memory and CPU.
developed application on an android mobile and also to publish
                                                                     Application framework - Exposes the various capabilities
the developed application in the Android Market (Google Play).
                                                                      of the Android OS to application developers so that they
Keywords: Android, Eclipse,      ADT    plugin,   Google   Play,      can make use of them in their applications.
Application Development.                                             Applications - At this top layer, you will find
                                                                      applications that ship with the Android device (such as
                      I. INTRODUCTION                                 Phone, Contacts, Browser, etc.), as well as applications
                                                                      that you download and install from the Android Market.
  Android is an open-source mobile operating system                   Any applications that you write are located at this layer.
developed by Google and the Open Handset Alliance on
which interesting and powerful new applications can be
quickly developed and distributed to many mobile devices.
  The Open Handset Alliance (OHA) is a collection of more
than 30 technology companies including mobile carriers,
hardware manufacturers, software developers and mobile
technology companies like Motorola, HTC, T-Mobile, and
Qualcomm. The OHA hopes to deliver a better mobile
software experience for consumers by providing the platform
needed for innovative mobile development at a faster rate and
a higher quality without licensing fees for handset
  The first Android mobile handset, the T-Mobile G1, was
released in the US in October 2008. Android is designed to
support a large variety of hardware platforms with no
licensing fees or proprietary software.
  In order to understand how Android works, take a look at
                                                                                 Fig. 1: ARCHITECTURE OF ANDROID
Fig. 1, which shows the various layers that make up the
Android operating system (OS).[1]                                    The rest of the paper is organized as follows. The
                                                                   Development environment that was used to develop Android
The Android OS is divided into five sections:                      Application Development is described in Section 2. Creating
  Linux kernel - This is the kernel on which Android is           the project along with development and testing is described in
     based. This layer contains all the low level device drivers   Section 3.Publishing your application to the Android Market
     for the various hardware components of an Android             (Google Play) is explained in Section 4. Section 5 concludes
     device.                                                       the paper.
  Libraries - These contain all the code that provides the
     main features of an Android OS.                                        II. ANDROID DEVELOPMENT ENVIRONMENT
  Android runtime – This layer provides a set of core
     libraries that enable developers to write Android apps          Before you write your first Android Application, however
     using the Java programming language. It also includes         you need to download and install the required tools. Android
     the Dalvik virtual machine, which enables every Android       applications can be written on the following operating
     application to run in its own process, with its own           systems: Windows XP or later, Mac OS X 10.5.8 or later (x86
     instance of the Dalvik virtual machine (Android               only), Linux

  Nov-Dec 2012 Edition
                                                            Page 90
                Naresh * et al.
                International Journal for Research in Science & Advanced                                     ISSN 2319-2690
                Issue-1, Volume-3, 090-095
Installing the Java Development Kit:                              profile, a mapping to a system image, as well as emulated
  The Android SDK makes use of the Java SE Development            storage, such as a secure digital (SD) card.
Kit (JDK). Hence, if your computer does not have the JDK            You can create as many AVDs as you want in order to test
installed, you should start by downloading the JDK from [2]       your applications with several different configurations. This
and install it.                                                   testing is important to confirm the behavior of your
                                                                  application when it is run on different devices with varying
The Eclipse:                                                      capabilities.
  The first step towards developing any applications is             To create an AVD:
obtaining the integrated development environment (IDE). In           1. In Eclipse, choose Window Android SDK and AVD
the case of Android, the recommended IDE is Eclipse, a multi-            Manager.
language software development environment featuring an               2. Select Virtual Devices in the left panel.
extensible plug-in system.                                           3. Click New. The Create New AVD dialog appears.
  For Android development, you should download the Eclipse           4. Type the name of the AVD, such as "myPhone."
IDE for Java EE Developers [3]. The current version is               5. Choose a target. The target is the platform (that is, the
Eclipse 4.2 (Juno).                                                      version of the Android SDK) to be run on the emulator.
                                                                         You will also get an option to choose the Google APIs,
The Android SDK:                                                         but that is unnecessary unless we want to have some
  You need to install the Android Software Development Kit               Google API in the app specifically.
(SDK) to develop Android applications. The Android SDK               6. Click Create AVD.
includes the Android JAR file (Android application
framework classes) as well as Android documentation, tools,            III. CREATING YOUR FIRST ANDROID APPLICATION
and sample code. The Android SDK is available from the
Android Developer website [4].                                      You’ve downloaded the SDK, installed Eclipse, and plugged
                                                                  in the plug-in. You’re now ready to start programming for
The Android Development Tool (ADT) Plug-in:                       Android. Start by creating a new project and setting up your
 The ADT plug-in for Eclipse simplifies your Android              Eclipse run and debug configurations. Here we have created a
development by integrating the developer tools, including the     Temperature Convertor application which converts Celsius to
emulator and .class-to-.dex converter, directly into the IDE.     Fahrenheit.
  The ADT plug-in integrates the following into Eclipse:            Now we will see the step by step procedure for creating an
    An Android Project Wizard that simplifies creating           android application. To create a new Android project using the
       new projects and includes a basic application template.    Android New Project Wizard:
    Forms-based manifest, layout, and resource editors to        a. Select File ➪ New ➪ Project in the Eclipse IDE, Select
       help create, edit, and validate your XML resources.             the Android Project application type from the Android
    Automated building of Android projects, conversion to             folder, and click Finish.
       Android executables (.dex), packaging to package files     b. In the dialog that appears (shown in Fig 2), enter the
       (.apk), and installation of packages onto Dalvik virtual        details for your new project. The “Project name” is the
       machines.                                                       name of your project file (here it is Temperature
    The Android Emulator, including control of the                    Convertor), the “Package name” specifies its package
       emulator’s appearance, network connection settings,             generally package names is used to deploy the app in the
       and the ability to simulate incoming calls and SMS              Android market so it should be unique (here it is
       messages                                                        naresh.temperatureconvertor).
    The Dalvik Debug Monitoring Service (DDMS),
       which includes port forwarding; stack, heap, and
       thread viewing; process details; and screen capture
    Access to the device or emulator’s file system,
       allowing you to navigate the folder tree and transfer
    Runtime debugging, so you can set breakpoints and
       view call stacks.
    All Android/Dalvik log and console outputs

Creating Android Virtual Devices (AVD s):
 The next step is to create AVD to be used for testing your
Android applications. AVD stands for Android Virtual
Devices. An AVD is an emulator instance that enables you to
model an actual device. Each AVD consists of a hardware                        Fig. 2: New Android Application Dialog Box

  Nov-Dec 2012 Edition
                                                           Page 91
                 Naresh * et al.
                 International Journal for Research in Science & Advanced                                             ISSN 2319-2690
                 Issue-1, Volume-3, 090-095
c.    Click nest and Select the BlankActivity template and press                                  TABLE I: String Attributes
      the Next button. On the next dialog change Title attribute                           Name                     Value
                                                                                   celsius                 to Celsius
      to Temperature Converter. Leave the rest as it is. Press                     fahrenheit              to Fahrenheit
      the Finish button. The wizard may prompt you to install                      calc                    Calculate
      the Support library. If you are prompted, select to install
      it.                                                              f.   Adding Views:
                                                                               Select the res/layout/activity_main.xml file and open
d.    After the wizard finishes the project structure shown in               the Android editor via a double-click. This editor
      Fig. 3 should be created.                                              allows you to create the layout via drag and drop or via
                                                                             the XML source code. You can switch between both
                                                                             representations via the tabs at the bottom of the editor.
                                                                             For changing the position and grouping elements you
                                                                             can use the Eclipse Outline View.
                                                                               The following shows a screenshot of the Palette side
                                                                             of this editor from which you can drag and drop new
                                                                             user interface components into your layout. Please note
                                                                             that the Palette view changes frequently so your view
                                                                             might be a bit different.

                  Fig. 3: Android Application Project Structure

e.    Creating attributes:
          Android allows you to create static attributes, e.g.
        Strings or colors. These attributes can for example be
        used in your XML layout files or referred to via Java
        source code.
        Select    the res/values/string.xml file   and     press
        the Add button. Select the Color entry in the following
        dialog and press the OK button. Enter myColor as the                                        Fig. 5: Eclipse Palette
        name and #F5F5F5 as the value shown in
        Fig. 4.                                                               You will now create the layout for your Android
                                                                            application. Right-click on the existing Hello World! text
                                                                            object in the layout. Select Delete from the popup menu
                                                                            to remove the text object.

                                                                       g.   Afterwards        select    the Text    Fields section    in
                                                                            the Palette and locate the Plain Text (via the tooltip). All
                                                                            entries in the Text Fields section define text fields. The
                                                                            different entries define additional attribute for them, e.g.
                                                                            if a text field should only contain numbers.
                                                                              Drag this onto the layout to create a text input field.
                                                                            Afterwards select the Form Widgets section in
                                                                            the Palette and drag a RadioGroup entry into the layout.
                     Fig. 4: Specifying Background Colour                   The number of radio buttons added to the radio button
          Add more attributes, this time of the String type.                group depends on your version of Eclipse. Make sure
        String attributes allow the developer to translate the              there are two radio buttons by deleting or adding radio
                                                                            buttons to the group.
        application at a later point.
                                                                              Drag a Button from the Form Widgets section into the
                                                                            layout. The result should look like Fig. 6.

     Nov-Dec 2012 Edition
                                                                  Page 92
                   Naresh * et al.
                   International Journal for Research in Science & Advanced                                           ISSN 2319-2690
                   Issue-1, Volume-3, 090-095
                                                                           changed via the right mouse menu. You can also edit
                                                                           properties of fields directly in XML. Changing properties
                                                                           in the XML file is much faster, if you know what you
                                                                           want to change. But the right mouse functionality is nice,
                                                                           if you are searching for a certain property.
                                                                             Open your layout file. Use the right click on the first
                                                                           radio button to assign the celsius String attribute to its text
                                                                           property. Assign the fahrenheit string attribute to
                                                                           the text property of the second radio button (Fig. 7).

                                 Fig. 6: Layout

h.    Switch to the XML tab of your layout file and verify that
      the file looks similar to the following listing. ADT
      changes the templates very fast, so your XML might look
      slighty different.
       android:background="@color/myColor" >
          android:layout_marginTop="22dp"                                                     Fig. 7: Resource Chooser
          android:inputType="number|numberSigned" >                   j.   From now on, we assume you are able to use the
          <requestFocus />                                                 properties menu on user interface components. You can
        </EditText>                                                        always either edit the XML file or modify the properties
                                                                           via right mouse click. Set the Checked property to true for
          android:layout_width="wrap_content"                              the first RadioButton (copy and paste the code from the
          android:layout_height="wrap_content"                             above XML file).
          android:layout_alignLeft="@+id/editText1"                          Assign calc to the text property of your button and
                                                                           assign the value onClick to the onClick property. Set
          android:layout_marginTop="27dp"                                  the Inputtype propertyto numberSigned and numberDecim
          android:checked="true"                                           al on the EditText. All your user interface components are
          android:text="@string/Celsius" />                                contained in a layout. Assign a background color to
                                                                           this Layout. Right-click on an empty space in Graphical
          android:layout_width="wrap_content"                              Layout mode, then select Other Properties → All by
          android:layout_height="wrap_content"                             Name →Background.             Select Color and        then
          android:layout_alignLeft="@+id/radioButton1"                     select myColor in the dialog. Afterwards the background
                                                                           should change to the whitesmoke color. It might difficult
          android:text="@string/fahrenheit" />                             to see the difference.
          android:id="@+id/button1"                                   k.   Changing the Activity source code:
                                                                             During the generation of your new Android project you
          android:layout_alignRight="@+id/radioButton2"                    specified that an Activity called MainActivity should be
          android:layout_centerVertical="true"                             created. The project wizard created the corresponding
          android:onClick="onClick"                                        Java class. Change your MainActivity class to the
          android:text="@string/calc" />
                                                                           following listing. Note that the onClick will be called
                                                                           based on the OnClick property of your button. we use the
i.    Edit View properties:                                                same name as this is easier to remember.
        If you select a user interface component (an instance
      of View), you can change its properties via the                      package com.naresh.temperatureconvertor;
                                                                           import android.app.Activity;
      Eclipse Properties View. Most of the properties can be               import android.os.Bundle;

     Nov-Dec 2012 Edition
                                                                 Page 93
                    Naresh * et al.
                    International Journal for Research in Science & Advanced                                                ISSN 2319-2690
                    Issue-1, Volume-3, 090-095
      import android.view.View;
      import android.widget.EditText;
      import android.widget.RadioButton;
      import android.widget.Toast;
      public class MainActivity extends Activity {
        private EditText text;
        public void onCreate(Bundle savedInstanceState) {
         text = (EditText) findViewById(R.id.editText1);
        // This method is called at button click because we assigned the name
      //to the "OnClick property" of the button
       public void onClick(View view) {
          switch (view.getId()) {
          case R.id.button1:
       RadioButton celsiusButton = (RadioButton)
            RadioButton fahrenheitButton = (RadioButton)
            if (text.getText().length() == 0) {
              Toast.makeText(this, "Please enter a valid number",
                 Toast.LENGTH_LONG).show();                                                         Fig. 8: Application Output Screen
              return;                                                                .
            float inputValue = Float.parseFloat(text.getText().toString());
            if (celsiusButton.isChecked()) {
            } else {
        // Converts to celsius
        private float convertFahrenheitToCelsius(float fahrenheit) {
          return ((fahrenheit - 32) * 5 / 9);
        }                                                                                         Fig. 9: Android Emulator (AVD)
        // Converts to fahrenheit
        private float convertCelsiusToFahrenheit(float celsius) {
          return ((celsius * 9) / 5) + 32;
                                                                                         IV. PUBLISHING THE ANDROID APPLICATION
      }                                                                            In order to get your application running on user’s devices,
                                                                                you need a way to deploy it and distribute it. Google has made
l.    Starting the Project:                                                     it relatively easy to publish your Android application so that it
        To start the Android Application, select your project,                  can be quickly distributed to end users. The steps to publishing
      right click on it, and select Run As → Android                            your Android application generally involve the following:
      Application. If an emulator is not yet running, it will be                    a. Export your application as an APK (Android Package)
      started. Be patient, the emulator starts up very slowly.                          file.
        Type in a number, select your conversion and press the                      b. Generate your own self-signed certificate and digitally
      button. The result should be displayed and the other                              sign your application with it.
      option should get selected as shown in Fig. 8.                                c. Deploy the signed application.
                                                                                    d. Use the Android Market for hosting and selling your
m. Starting an installed application:                                                   application.
     After you run your application on the virtual device,
   you can start it again on the device. If you press the                         In the Eclipse IDE open New tab and navigate to Export
   Home button shown in Fig. 9 you can select your                              dialog, expand the Android item and select Export Android
   application and run it.                                                      Application Click Next. The project should now be displayed.
                                                                                Click Next.

     Nov-Dec 2012 Edition
                                                                           Page 94
                Naresh * et al.
                International Journal for Research in Science & Advanced                                          ISSN 2319-2690
                Issue-1, Volume-3, 090-095
 Select the “Create new keystore” option to create a new               The application in APK format
certificate (keystore) for signing your application. Enter a path      At least two screenshots. You can use the DDMS
to save your new keystore and then enter a password to protect             perspective in Eclipse to capture screenshots of your
the keystore. Click Next.                                                  application running on the Emulator or real device.
  You also need to enter a validity period for the key.                A high-resolution application icon. This size of this
According to Google, your application must be signed with a                image must be 512×512 pixels.
cryptographic private key whose validity period ends after 22         The other information details are optional, and you can
October 2033. Hence, enter a number that is greater than 2033       always supply them later. When you have given your consent
minus the current year. Click Next. Enter a path to store the       to the two guidelines and agreements, click Publish to publish
destination APK file. Click Finish, the APK file will now be        your application on the Android Market.
generated. That’s it! The APK is now generated.                       That’s it! Your application is now available on the Android
                                                                    Market. You will be able to monitor any comments submitted
Publishing on the Android Market:                                   about your application, as well as bug reports and total number
  So far, you have learned how to package your Android              of downloads.
application. To host your application on the Android Market, a                                V. CONCLUSION
Google-hosted service that makes it very easy for users to
discover and download (i.e., purchase) applications for their         The main goal of this paper is to gain basic knowledge on
Android devices. Users simply need to launch the Market             the android development process. In the project a simple
application on their Android device in order to discover a wide     application is developed that is the Temperature
range of applications that they can install on their devices.       Convertor. The tool used for developing this application is
  The first step toward publishing on the Android Market is to      fully open source.
create a developer profile at [5] for this; you need a Google          Eclipse was chosen to develop the project because it uses
account (such as your Gmail account). Once you have logged          the official Google’s ADT plugin for Android Application
in to the Android Market, you first create your developer           Development.
profile, Click Continue after entering the required information       Mainly to develop an Android Application it is not
as shown in Fig. 10.                                                necessary to own a physical android device instead we used
                                                                    AVD the emulator to test the application.

                                                                    [1]   Ed Burnette, Hello Android, Introducing Google’s Mobile Development
                                                                          Platform, 3rd edition, Pragmatic Programmers, 2010, pp 30-35.
                                                                    [2]   www.oracle.com/technetwork/java/javase/downloads/index.html.
                                                                    [3]   www.eclipse .org/downloads/packages/eclipse-ide-java-eedevelopers/
                                                                    [4]   http://developer.android.com/sdk/index.html.
                                                                    [5]   http://market.android.com/publish/Home.

                Fig.10: Profile Creation in Google Play

  For publishing on the Android Market, you need to pay a
one-time registration fee, currently U.S. $25. Click the Google
Checkout button to be redirected to a page where you can pay
the registration fee. After paying, click the Continue link.

Submitting Your Apps:
  Once you have set up your profile, you are ready to submit
your application to the Android Market. If you intend to
charge for your application, click the Setup Merchant Account
link located at the bottom of the screen. Here you enter
additional information such as bank account and tax ID.
  You will be asked to supply some details for your
application. Among the information needed, the following are

  Nov-Dec 2012 Edition
                                                             Page 95

Description: IJRSAT [International Journal for Research in Science & Advanced Technologies] is an international open access, interdisciplinary, bimonthly, peer reviewed, Indexed and referred journal. IJRSAT is an online journal having full access to the research and review paper. The journal also seeks clearly written survey and review articles from experts in the field, to promote insightful understanding of the state-of-the-art and application trends. The journal aims to cover the latest outstanding developments in the field of engineering sciences and Technology. )