Create an
Android Application
HELLO WORLD
In this post..
1. Basics of Android
2.System Requirements
3.Download
4.Create a simple application-"Hello World"
5.Understand the Android Code
6.How to make Effective Android code?
As in the case of any new programming horizon, we start our journey with creating simple "hello world" application. It is given in very simple manner and pictures are included to make some concepts very easy and comprehensive. The complete details of Android code is also given. The code for Android application and user interface are explained separatily. Then the simple tips to make good android code is discussed . Hope you will enjoy.
Before moving to actual coding on "HELLO WORLD" we should familiar with some details on Android.
6.How to make Effective Android code?
As in the case of any new programming horizon, we start our journey with creating simple "hello world" application. It is given in very simple manner and pictures are included to make some concepts very easy and comprehensive. The complete details of Android code is also given. The code for Android application and user interface are explained separatily. Then the simple tips to make good android code is discussed . Hope you will enjoy.
Before moving to actual coding on "HELLO WORLD" we should familiar with some details on Android.
BASICS OF ANDROID
What is Android?
Android may be viewed as a software stack for mobile devices including 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 of Android
- Application Framework enabling reuse and replacement of components
- Dalvik virtual machine optimized for mobile devices.
- SQLite for structured data storage 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
- Media support for common audio, video and still image formats
- GSM Telephony
- Bluetooth, EDGE, 3G and WIFI
- Camera, GPS, compass and accelerometer
Android software stack is made of the elements shown in this Figure
simply, a Linux kernel and a collection of C/C++ libraries are exposed through an
application framework that provides services for, and management of, the run time and applications.
Applications
Android has a set of core applications including an email client, SMS program, calendar, maps, browser, contacts, and others writtenin 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
- 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
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& Download
Simply 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
- Eclipse 3.3 (Europa), 3.4 (Ganymede)
- Eclipse JDT plugin (included in most Eclipse IDE packages)
- WST (optional, but needed for the Android Editors feature; included in most Eclipse IDE packages)
- JDK 5 or JDK 6 (JRE alone is not sufficient)
- Android Development Tools plugin (optional)
- Not compatible with Gnu Compiler for Java (gcj)
- Eclipse 3.3 (Europa), 3.4 (Ganymede)
- 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)
1.sdk(java devolepment kit):-From http://code.google.com/android/download.html download google android sdk and unzip the files into c:\tools\android. And install it on c:\tools\java
2.Download JDK from http://java.sun.com/javase/downloads/index.jsp3.Eclipse with java:-From http://www.eclipse.org/downloads/
4. Android emulator:-To provide Physical User interface of original Android phone.
Start with a simple application-"HELLO WORLD"
1)Open your Eclipse.
2)Follow File-->New -->Project
3)Select Android Project from the displayed folder, then click finish.
4)Now you can view a dialog, enter details of your application
- Project name:-Name of your project file.
- Package name:-Specify the package.(It should have two fields
- Activity Name:-Name of activity that will serve as the default main/launching activity.
- Application name:-Name of your Application.
One simple example is given here...
Classic "hello world" display is now created.
To run the application, follow Run--> Run(Run-->Run Configurations will give more flexibility.Now select "Android application" to invoke emulator.
Emulator will show ..
As you see, creating platform for android applications is quiet simple with using SDK. Now, Lets move on the real code part.
Understand the Android Code
You can find the code for our "HELLO WORLD" as in following manner::
package com.Firststep;
import android.app.Activity;
import android.os.Bundle;
import android.os.Bundle;
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
This code simply directs to create an activity by name"HelloWorld".We can think that @Override instructs the code following it should be treated first whenever that activity start up.
setContentView(R.layout.main) says that main.xml should be the interface for the application. We can find main.xml as res/layout/main.xml.
Now open main.xml. Its Graphical Layout part gives the original User Interface, while main.xml
gives the code:
<?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="@string/hello"
/>
</LinearLayout>
This code specifies a single linear layout for our application. Widgets within <LinearLayout >..... </LinearLayout> will be under one linear layout. TextView provides a text displayed on the screen on running the application.android:text="@string/hello" tells that is stored in string.xml(we can find it at res/values/string.xml).
How can we make efficient Android code?
How can we make efficient Android code?
Since Android is a resouce-constrained system we should take care that objectives should be done in minimum code as possible and memory allocations should be minimum as far as possible.
{1}Avoid Creating Objects as large no. of objects cause a periodic garbage collection, creating little "hiccups" in the user experience.
{2}Prefer Native Methods:-When processing strings, don't hesitate to use specialty methods like String.indexOf(), String.lastIndexOf(), and their cousins
{3}Prefer Virtual Over Interface
{4}Avoid Internal Getters/Setters
{5}Prefer Static Over Virtual
{6}Declare Constants Final
{7}Avoid Enums
{8}Use Enhanced For Loop Syntax With Caution
{1}Avoid Creating Objects as large no. of objects cause a periodic garbage collection, creating little "hiccups" in the user experience.
{2}Prefer Native Methods:-When processing strings, don't hesitate to use specialty methods like String.indexOf(), String.lastIndexOf(), and their cousins
{3}Prefer Virtual Over Interface
{4}Avoid Internal Getters/Setters
{5}Prefer Static Over Virtual
{6}Declare Constants Final
{7}Avoid Enums
{8}Use Enhanced For Loop Syntax With Caution
{9}Avoid Float as far as possible
0 comments:
Post a Comment