A Brief Guide for Creating your first Android Application Tip Calculator  Table of Contents 1 ) Welcome To Android - 3 2 ) System Requirements - 6 3 ) Downloads - 7 4 ) Creating a Dummy Application - 8 5 ) Writing Tip Calculator - 11 6 ) What’s Next - 20 Welcome to Android! According to the official google release “Android is a software stack for mobile devices that includes an operation system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language. Features :- · Application Framework enabling reuse and replacement of components · Dalvik virtual machine optimized for mobile devices · Integrated browser based on the open source Webkit engine · Optimized graphics powered by a custom 2D graphics library ; 3D graphics based on the OpenGL ES 1.0 specification · SQLite for structured data storage · Media support for common audio, video and still image formats · GSM Telephony · Bluetooth, EDGE, 3G and WIFI · Camera, GPS, compass and accelerometer · Rich development environment “ Android Architecture The following picture shows the components of Android Operation System.  Applications Android will ship with a set of core applications including an email client, SMS program, calendar, maps, browser, contacts, and others. All applications are written using the Java programming language. Application Framework Developers have full access to the same framework APIs used by the core applications. The application architecture is designed to simplify the reuse of components; any application can publish its capabilities and any other application may then make use of those capabilities (subject to security constraints enforced by the framework). This same mechanism allows components to be replaced by the user. Underlying all applications is a set of services and systems, including: - A rich and extensible set of Views that can be used to build an application, including lists, grids, text boxes, buttons, and even an embeddable web browser
- Content Providers that enable applications to access data from other applications (such as Contacts), or to share their own data
- A Resource Manager, providing access to non-code resources such as localized strings, graphics, and layout files
- A Notification Manager that enables all applications to display custom alerts in the status bar
- An Activity Manager that manages the life cycle of applications and provides a common navigation backstack
For more details and a walkthrough of an application, see below... Libraries Android includes a set of C/C++ libraries used by various components of the Android system. These capabilities are exposed to developers through the Android application framework. Some of the core libraries are listed below: - System C library - a BSD-derived implementation of the standard C system library (libc), tuned for embedded Linux-based devices
- Media Libraries - based on PacketVideo's OpenCORE; the libraries support playback and recording of many popular audio and video formats, as well as static image files, including MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG
- Surface Manager - manages access to the display subsystem and seamlessly composites 2D and 3D graphic layers from multiple applications
- LibWebCore - a modern web browser engine which powers both the Android browser and an embeddable web view
- SGL - the underlying 2D graphics engine
- 3D libraries - an implementation based on OpenGL ES 1.0 APIs; the libraries use either hardware 3D acceleration (where available) or the included, highly optimized 3D software rasterizer
- FreeType - bitmap and vector font rendering
- SQLite - a powerful and lightweight relational database engine available to all applications
Android Runtime Android includes a set of core libraries that provides most of the functionality available in the core libraries of the Java programming language. Every Android application runs in its own process, with its own instance of the Dalvik virtual machine. Dalvik has been written so that a device can run multiple VMs efficiently. The Dalvik VM executes files in the Dalvik Executable (.dex) format which is optimized for minimal memory footprint. The VM is register-based, and runs classes compiled by a Java language compiler that have been transformed into the .dex format by the included "dx" tool. The Dalvik VM relies on the Linux kernel for underlying functionality such as threading and low-level memory management. Linux Kernel Android relies on Linux version 2.6 for core system services such as security, memory management, process management, network stack, and driver model. The kernel also acts as an abstraction layer between the hardware and the rest of the software stack. System Requirements So rather than wasting time on learning the theoretical aspects we would rather like you to get your hands dirty and create your first app with Google Android. But you need the following things ready with you before starting the development. This tutorial assumes that your OS is Windows XP but you can follow the procedure for other OS easily. Just download the relevant files from the download locations specified. System and Software Requirements :- To develop android app you need to have suitable environment as discussed below :- Supported Operating Systems · Windows XP or Vista · Mac OS X 10.4.8 or later (x86 only) · Linux (tested on Linux Ubuntu Dapper Drake) Supported Development Environments: - Eclipse IDE
- Other development environments or IDEs
- JDK 5 or JDK 6 (JRE alone is not sufficient)
- Apache Ant 1.6.5 or later for Linux and Mac, 1.7 or later for Windows
- Not compatible with Gnu Compiler for Java (gcj)
\ Downloads and Development Settings 1 ) Download the google android sdk from http://code.google.com/android/download.html And unzip the files into c:\tools\android 2 ) Download Ant from http://ant.apache.org/bindownload.cgi And unzip the files into c:\tools\ant 3 ) Download JDK from http://java.sun.com/javase/downloads/index.jsp And install it on c:\tools\java 4 ) Download DroidDraw from http://code.google.com/p/droiddraw/ ( Not Necessary but it will be easier to create the GUI using this ) and unzip the files into c:\tools\droiddraw 5) Create two environment variables ANT_HOME and JAVA_HOME and point them to c:\tools\ant and c:\tools\java respectively. Also add c:\tools\ant\bin & c:\tools\java\bin into your path variable. You can create the variables by right clicking on My Computer -> Properties -> Advanced -> Environment Variables.  Be careful when you are editing your path. You should add the paths at the end of already existing path value by separating the paths with ; Creating A Dummy Application Please follow this steps and you will have a simple google android app which will print the message “Hello, World” on your mobile screen. 1 ) Run emulator.exe ( c:\tools\android\tools)  You should have the default screen visible. Keep the emulator running and goto Command Prompt and enter the following commands to create and upload dummy android app to our dummy phone. c:\tools\android\tools>activitycreator.bat --out tipcalc com.android.tipcalc c:\tools\android\tools>cd tipcalc c:\tools\android\tools\tipcalc>ant c:\tools\android\tools\tipcalc>..\adb install bin\tipcalc-debug.apk - The activitycreator.bat file will create all the basic stub files needed to create a basic android app.
- Ant will compile the program created by activity creator into binary format which can be run on android phone.
- Adb install will upload the binary application on the phone.
This is how your screen should look like :-  Now a simple android app has been created, compiled and uploaded onto our phone emulator. You can go and run that app. It should greet you with the famous “Hello, World “ Message.   Now we will modify this sample app and create our tip calculating application. Don’t forget to delete the uploaded app before installing new app. It is necessary to delete the uploaded app. You can delete it by running this commands. c:\tools\android\tools\tipcalc>..\adb shell #cd data/app #rm com.android.apk #exit You can use ls command to have a look at the app installed by user. Writing Tip Calculator Now we will create a simple tip calculating application very similar to one over here http://www.onlineconversion.com/tip_calculator.htm  First we will design our GUI. You can use droiddraw to design the gui.  The droiddraw will generate xml code. You have to replace the code in tipcalc\res\layout\main.xml with the generated code. Old main.xml file contents :- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, tipcalc" /> </LinearLayout> New main.xml file contents :- <?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, tipcalc" /> <TextView android:id="@+id/widget28" android:layout_width="99px" android:layout_height="17px" android:text="Amount of Bill" android:layout_x="40px" android:layout_y="32px" > </TextView> <TextView android:id="@+id/widget29" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Percentage to Tip" android:layout_x="40px" android:layout_y="82px" > </TextView> <TextView android:id="@+id/widget30" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Number of People" android:layout_x="40px" android:layout_y="132px" > </TextView> <TextView android:id="@+id/widget31" android:layout_width="wrap_content" android:layout_height="18px" android:text="Tip Amout" android:layout_x="40px" android:layout_y="262px" > </TextView> <TextView android:id="@+id/widget32" android:layout_width="wrap_content" android:layout_height="18px" android:text="Total Per Person" android:layout_x="40px" android:layout_y="352px" > </TextView> <TextView android:id="@+id/widget33" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Total To Pay" android:layout_x="40px" android:layout_y="302px" > </TextView> <Button android:id="@+id/btncalculate" android:layout_width="87px" android:layout_height="wrap_content" android:text="Calculate" android:layout_x="40px" android:layout_y="182px" > </Button> <Button android:id="@+id/btnreset" android:layout_width="86px" android:layout_height="wrap_content" android:text="Reset" android:layout_x="140px" android:layout_y="182px" > </Button> <EditText android:id="@+id/txtbillamount" android:layout_width="wrap_content" android:layout_height="35px" android:text="100" android:textSize="18sp" android:layout_x="200px" android:layout_y="22px" > </EditText> <EditText android:id="@+id/txtpercentage" android:layout_width="51px" android:layout_height="36px" android:text="10" android:textSize="18sp" android:layout_x="200px" android:layout_y="72px" > </EditText> <EditText android:id="@+id/txtpeople" android:layout_width="wrap_content" android:layout_height="39px" android:text="1" android:textSize="18sp" android:layout_x="200px" android:layout_y="122px" > </EditText> <TextView android:id="@+id/txttipamount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_x="200px" android:layout_y="262px" > </TextView> <TextView android:id="@+id/txttotal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_x="200px" android:layout_y="302px" > </TextView> <TextView android:id="@+id/txtperperson" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_x="200px" android:layout_y="352px" > </TextView> </AbsoluteLayout> Now we will also write the program which will use this GUI to create the tip calculation functionality. In the tipcalc\src\com\android\tipcalc.java file you will find this code ( which was automatically created by activitycreator.bat . Old tipcalc.java contents :- package com.android; import android.app.Activity; import android.os.Bundle; public class tipcalc extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } Replace the above code with the following code . New tipcalc.java contents :- package com.android; import android.app.Activity; import android.os.Bundle; import android.widget.EditText; import android.widget.TextView; import android.widget.Button; import android.view.View; public class tipcalc extends Activity { private EditText txtbillamount; private EditText txtpeople; private EditText txtpercentage; private TextView txtperperson; private TextView txttipamount; private TextView txttotal; private Button btncalculate; private Button btnreset; private double billamount = 0; private double percentage = 0; private double numofpeople=0; private double tipamount = 0; private double totaltopay = 0; private double perperson = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initControls(); } private void initControls() { txtbillamount = (EditText)findViewById(R.id.txtbillamount); txtpeople = (EditText)findViewById(R.id.txtpeople); txtpercentage = (EditText)findViewById(R.id.txtpercentage); txtperperson=(TextView)findViewById(R.id.txtperperson); txttipamount=(TextView)findViewById(R.id.txttipamount); txttotal=(TextView)findViewById(R.id.txttotal); btncalculate = (Button)findViewById(R.id.btncalculate); btnreset = (Button)findViewById(R.id.btnreset); btncalculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }}); btnreset.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ reset(); }}); } private void calculate() { billamount=Double.parseDouble(txtbillamount.getText().toString()); percentage=Double.parseDouble(txtpercentage.getText().toString()); numofpeople=Double.parseDouble(txtpeople.getText().toString()); tipamount=(billamount*percentage)/100; totaltopay=billamount+tipamount; perperson=totaltopay/numofpeople; txttipamount.setText(Double.toString(tipamount)); txttotal.setText(Double.toString(totaltopay)); txtperperson.setText(Double.toString(perperson)); } private void reset() { txtbillamount.setText(""); txtpeople.setText(""); txtpercentage.setText(""); txtperperson.setText(""); txttipamount.setText(""); txttotal.setText(""); } } Now we will add an icon for our app.  Modify AndroidManifest.xml so that this line is changed from <application android:label="@string/app_name"> To <application android:icon="@drawable/icon" android:label="@string/app_name"> Now create a directory drawable in tipcalc\res and put icon.png file there. Now compile our new application again by running C:\tools\android\tools\tipcalc>ant Upload the app to the emulator using adb install command and your first google android app is ready for launch. Don’t forget to remove the previous uploaded application first. In Action   What’s Next So your first android application is now running successfully on the emulator. You should go through the documents provided along with the SDK to learn more about android. There are also few sample applications provided ( in samples ) directory. Compile them, upload them and try to understand them. Initially try modifying few things here and there and once you feel confident you can start with full-fledged development. You should also go through online forums and learn from others. Hopefully you will be creating great android apps in near future. Don’t forget to share them with us. Wishing you Good Luck. Download Article Source Code Below (Must be registered user, registration is free!) |