Android Tutorial
Android is
a complete set of software for mobile devices such as tablet computers,
smartphones, electronic book readers, notebooks, set-top boxes etc. It contains
anlinux based operating system, middleware and key
mobile applications.
It can be thought of as a
mobile operating system. But it is not limited to mobile only. It is currently
used in mobiles, tablets, televisions etc.
This tutorial is developed
for beginners and experienced persons. Let's see the topics of android that we are
going to learn.
Basics of Android
In this fundamental
chapter, you will learn about android, its components, how to create first
android application, internal of first android application etc.
Activity and Intent
It provides the detail of
activity, its life cycle with example, implicit intent, sending information
from one activity to another etc.
Android UI Widgets
There are given a lot of
android ui widgets examples such as toggle button, alert dialog, spinner,
progress bar, alert dialog etc.
Menus in Android
Here, you will learn about
the option menu that is primary menu, context menu that works on long press and
popup menu.
Service in Android
It can be used to perform
any task in background. It doesn't have any UI.
Data Storage
You can store data in
android using preferences, internal storage, external storage and sqlite
database. Preferences are used to primitives, internal storage stores data in device
internal memory, external storage stores data in device external memory such as
SD card and SQLite is used to store data in sqlite database i.e. inbuilt in
android OS.
SQLite Database
XML and JSON Parsing
There are three ways to
parse the xml file but android recommends XMLPullParser to parse the XML.
Instead of XML, JSON is prefered currently because it is short and fast.
Android Multimedia
Android multimedia api
deals in playing and controlling the audio and video.
Android Speech API
Speech API is used to
convert text into speech.
Telephony API
It can be used to get
details about the device, making phone calls, sending sms, sending emails etc.
Device Connectivity
We can connect and control
bluetooth, wifi and camera in android.
Android - What, Where and Why?
Android is
a software package and linux based operating system for mobile devices such as
tablet computers and smartphones.
It is
developed by Google and later the OHA (Open Handset Alliance). Java language is
mainly used to write the android code even though other languages can be used.
The goal
of android project is to create a successful real-world product that improves
the mobile experience for end users.
What
is Open Handset Alliance (OHA) ?
It's a
consortium of 84 companies such as google, samsung, AKM, synaptics, KDDI,
Garmin, Teleca, Ebay, Intel etc.
It was
established on 5th November, 2007, led by Google. It is commited to advance
open standards, provide services and deploy handsets using the Android Plateform.
Features
of Android
There are
many advantages of android. They are as follows:
- It is open-source.
- Anyone can customize the Android Platform.
- There are a lot of mobile applications that can be
choosen by the consumer.
- It provides many interesting features like weather
details, opening screen, live RSS (Really Simple Syndication) feeds etc.
- It provides support for messaging services(SMS and
MMS), web browser, storage (SQLite), connectivity (GSM, CDMA, Blue Tooth,
Wi-Fi etc.), media, handset layout etc.
Categories
of Android applications
There are
many android applications in the market. The top categories are:
- Entertainment
- Tools
- Communication
- Productivity
- Personalization
- Music and Audio
- Social
- Media and Video
- Travel and Local etc.
Android - History and Versions
Android
History and Versions are interesting. The codenames of android ranges from A to
J currently, such as Aestro, Blender, Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice
Cream Sandwitchand Jelly
Bean,KitKat and Marshmallow. Let's
see the main points of android history:
- Initially, Andy
Rubin founded Android
Incorporation in Palo Alto, California, United States in October, 2003.
- In August 17, 2005, Google acquired Android Inc.
Since then, it is in the subsidiary of Google Inc.
- Key employees of Android Inc. are Andy Rubin, Rich Miner, Nick Sears and Chris White.
Android Versions, Codename and API
Let's see
the android versions codename and API provided by Google.
|
Version
|
Codename
|
API
|
|
1.5
|
Cupcake
|
3
|
|
1.6
|
Donut
|
4
|
|
2.1
|
Éclair
|
7
|
|
2.2
|
Froyo
|
8
|
|
2.3
|
Gingerbread
|
9 and 10
|
|
3.1 and 3.3
|
Honeycomb
|
12 and 13
|
|
4.0
|
Ice Cream Sandwitch
|
15
|
|
4.1 and 4.2
|
Jelly Bean
|
16 and 17
|
Android
Versions Image
Android Software Stack
Android
software stack (also known as android architecture) includes linux kernal,
native libraries (middleware), Android Runtime, Application Framework and
Applications.
Linux
Kernal is
responsible for device drivers, power management, resource access etc. OS
tasks.
On the
top of linux kernal, their are Native
libraries such
as WebKit, OpenGL, FreeType, SQLite, Media, C runtime library (libc) etc.
The
WebKit library is responsible for browser support, SQLite is for database,
FreeType for font support, Media for playing and recording audio and video
formats.
The main Android
API's are
UI (User Interface), telephony, resources, locations, Content Providers (data)
and package managers.
Android Core Building Blocks
A component is
simply a piece of code that has a well defined life cycle e.g. Activity,
Receiver, Service etc.
The core
building blocks or fundamental
components of
android are activities, views, intents, services, content providers, fragments
and AndroidManifest.xml.
Activity
An
activity is a class that represents a single screen. It is like a Frame in AWT.
View
A view is
the UI element such as button, label, text field etc. Anything that you see is
a view.
Intent
Intent is
used to invoke components. It is mainly used to:
- Start the service
- Launch an activity
- Display a web page
- Display a list of contacts
- Broadcast a message
- Dial a phone call etc.
For
example, you may write the following code to view the webpage.
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);
Service
Service
is a background process that can run for a long time.
There are
two types of services local and remote. Local service is accessed from within
the application whereas remote service is accessed remotely from other
applications running on the same device.
Content
Provider
Content
Providers are used to share data between the applications.
Fragment
Fragments
are like parts of activity. An activity can display one or more fragments on
the screen at the same time.
AndroidManifest.xml
It
contains informations about acvities, content providers, permissions etc. It is
like the web.xml file in Java EE.
Android
Virtual Device (AVD)
It is
used to test the android application without the need for mobile or tablet etc.
It can be created in different configurations to emulate different types of
real devices.
Android Emulator
Android
Emulator is used to run, debug and test the android application. If you don't
have the real device, it can be the best way to run, debug and test the
application.
It uses
an open source processor emulator technology called QEMU.
The
emulator tool enables you to start the emulator from the command line. You need
to write:
emulator -avd <AVD NAME>
In case
of Eclipse IDE, you can create AVD by Window
menu > AVD Manager > New.
In the
given image, you can see the android emulator, it displays the output of the
hello android example.
Installing softwares for Android
Android
supports java, c++, c# etc. language to develop android applications. Java is
the officially supported language for android. All the android examples of this
site is developed using Java language and Eclipse IDE.
Here, we
are going to tell you, the required softwares to develop android applications
using Eclipse IDE.
Simple way by ADT Bundle
It is the
simplest technique to install required softwares for android application. It
includes:
- Eclipse IDE
- Android SDK
- Eclipse Plugin
If you
download the ADT from android site, you don't need to have eclipse IDE, android
SDK and eclipse Plugin because it is already included in adt bundle.
If you
have downloaded the ADT bundle, unjar it, go to eclipse IDE and start the
eclipse by clicking on the eclipse icon. You don't need to do any extra steps
here.
If eclipse is not started, paste the JRE
directory inside the eclipse directory.
Other way by setting up eclipse IDE manually
How to setup Android for Eclipse IDE
|
In this page, you will learn what softwares are required for
running an android application on eclipse IDE. Here, you will be able to
learn how to install the android SDK ADT plugin for Eclipse IDE. Let's see
the softwares required to setup android for eclipse IDE manually.
1. Install the JDK
2. Download and install the Eclipse for developing android
application
3. Download and Install the android SDK
4. Intall the ADT plugin for eclipse
5. Configure the ADT plugin
6. Create the AVD
7. Create the hello android application
|
1) Install the Java Development Kit (JDK)
|
For creating android application, JDK must be installed if you
are developing the android application with Java language. download the JDK
|
2) Download and install the Eclipse IDE for developing the android
application
|
For developing the android application using eclipse IDE, you
need to install the Eclipse. you can download it from this location download the Eclipse. Eclispe classic version is recommended but we are using the
Eclipse IDE for JavaEE Developers (Eclipse Helios).
|
3) Download and install the android SDK
|
First of all, download the android SDK. In this example we have installed the android SDK for windows
(.exe version). Now double click on the exe file, it will be installed. I am
using the android 2.2 version here.
|
4) Download the ADT plugin for eclipse
|
ADT (Android Development Tools) is required for developing the
android application in the eclipse IDE. It is the plugin for Eclipse IDE that
is designed to provide the integrated environment.
|
|
For downloading the ADT, you need to follow these steps:
1. Start the eclipse IDE, then select Help > Install new software...
2. In the work with combo box, write https://dl-ssl.google.com/android/eclipse/
3. select the checkbox next to Developer Tools and click next
4. you will see, a list of tools to be downloaded here, click next
5. click finish
6. After completing the installation, restart the eclipse IDE
|
5) Configuring the ADT plugin
|
After the installing ADT plugin, now tell the eclipse IDE for
your android SDK location. For this:
1. Select the Window menu > preferences
2. Now select the android from the left panel. Here you may see a
dialog box asking if you want to send the statistics to the google. Click proceed.
3. Click on the browse button and locate your SDK directory e.g. my
SDK location is C:\Program Files\Android\android-sdk .
4. Click the apply button then OK.
|
6) Create an Android Virtual Device (AVD)
|
For running the android application in the Android Emulator, you
need to create and AVD. For creating the AVD:
1. Select the Window menu > AVD Manager
2. Click on the new button, to create the AVD
3. Now a dialog appears, write the AVD name e.g. myavd. Now choose
the target android version e.g. android2.2.
4. click the create AVD
|
7) create and run the simple android example
Hello Android Example
|
In this page, you will know how to create the simple hello
android application. We are creating the simple example of android using the
Eclipse IDE. For creating the simple example:
1. Create the new android project
2. Write the message (optional)
3. Run the android application
|
Example of Hello Android
|
You need to follow the 3 steps mentioned above for creating the
Hello android application.
|
1) Create the New Android project
|
For creating the new android project:
1. Select File > New > Project...
2. Select the android project and click next
3. Fill the Details in this diaglog box and click finish
|
|
Now an android project have been created. You can explore the
android project and see the simple program, it looks like:
|
2) Write the message
|
For writing the message we are using the TextView class. Change
the onCreate method as:
|
package com.example.helloandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview=new TextView(this);
textview.setText("Hello Android!");
setContentView(textview);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
To understand the first android application,
visit the next page (internal details of hello android example).
3) Run the android application
|
To run the android application:
·
Right click on your project >
Run As.. > Android Application
|
|
The android emulator might take 2 or 3 minutes to boot. So
please have patience. After booting the emulator, the eclipse plugin installs
the application and launches the activity. You will see something like this:
|
|
|
Internal Details of Hello Android Example
Here, we
are going to learn the details of first android program.
Android
application contains different components such as java source code, string
resources, images, manifest file, apk file etc. Let's understand the project
structure of android application.
Java
Source Code
Let's see
the java source file created by the Eclipse IDE:
File: MainActivity.java
1.
package com.example.helloandroid;
2.
3.
import android.os.Bundle;
4.
import android.app.Activity;
5.
import android.view.Menu;
6.
import android.widget.TextView;
7.
8.
public class MainActivity extends Activity {//(1)
9.
10.
@Override
11.
protected void onCreate(Bundle savedInstanceState) {//(2)
12.
super.onCreate(savedInstanceState);
13.
14.
setContentView(R.layout.activity_main);//(3)
15.
}
16.
17.
@Override
18.
public boolean onCreateOptionsMenu(Menu menu) {//(4)
19.
// Inflate the menu; this adds items to the action bar if it is present.
20.
getMenuInflater().inflate(R.menu.activity_main, menu);
21.
return true;
22.
}
23.
24.
}
(1) Activity is
a java class that creates and default window on the screen where we can place
different components such as Button, EditText, TextView, Spinner etc. It is like
the Frame of Java AWT.
It
provides life cycle methods for activity such as onCreate, onStop, OnResume
etc.
(2) The onCreate
method is
called when Activity class is first created.
(3) The setContentView(R.layout.activity_main) gives
information about our layout resource. Here, our layout resources are defined
in activity_main.xml file.
File: activity_main.xml
1.
<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
2.
xmlns:tools="http://schemas.android.com/tools"
3.
android:layout_width="match_parent"
4.
android:layout_height="match_parent"
5.
tools:context=".MainActivity" >
6.
7.
<TextView
8.
android:layout_width="wrap_content"
9.
android:layout_height="wrap_content"
10.
android:layout_centerHorizontal="true"
11.
android:layout_centerVertical="true"
12.
android:text="@string/hello_world" />
13.
14.
</RelativeLayout>
As you
can see, a textview is created by the framework automatically. But the message
for this string is defined in the strings.xml file. The @string/hello_world provides
information about the textview message. The value of the attribute hello_world
is defined in the strings.xml file.
File: strings.xml
1.
<?xml version="1.0" encoding="utf-8"?>
2.
<resources>
3.
4.
<string name="app_name">helloandroid</string>
5.
<string name="hello_world">Hello world!</string>
6.
<string name="menu_settings">Settings</string>
7.
8.
</resources>
You can
change the value of the hello_world attribute from this file.
Generated
R.java file
It is the
auto-generated file that contains IDs for all the resources of res directory.
It is generated by aapt(Android Asset Packaging Tool). Whenever you create any
component on activity_main, a corresponding ID is created in the R.java file
which can be used in the Java Source file later.
File: R.java
1.
/* AUTO-GENERATED FILE. DO NOT MODIFY.
2.
*
3.
* This class was automatically generated by the
4.
* aapt tool from the resource data it found. It
5.
* should not be modified by hand.
6.
*/
7.
8.
package com.example.helloandroid;
9.
10.
public final class R {
11.
public static final class attr {
12.
}
13.
public static final class drawable {
14.
public static final int ic_launcher=0x7f020000;
15.
}
16.
public static final class id {
17.
public static final int menu_settings=0x7f070000;
18.
}
19.
public static final class layout {
20.
public static final int activity_main=0x7f030000;
21.
}
22.
public static final class menu {
23.
public static final int activity_main=0x7f060000;
24.
}
25.
public static final class string {
26.
public static final int app_name=0x7f040000;
27.
public static final int hello_world=0x7f040001;
28.
public static final int menu_settings=0x7f040002;
29.
}
30.
public static final class style {
31.
/**
32.
Base application theme, dependent on API level. This theme is replaced
33.
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
34.
35.
36.
Theme customizations available in newer API levels can go in
37.
res/values-vXX/styles.xml, while customizations related to
38.
backward-compatibility can go here.
39.
40.
41.
Base application theme for API 11+. This theme completely replaces
42.
AppBaseTheme from res/values/styles.xml on API 11+ devices.
43.
44.
API 11 theme customizations can go here.
45.
46.
Base application theme for API 14+. This theme completely replaces
47.
AppBaseTheme from BOTH res/values/styles.xml and
48.
res/values-v11/styles.xml on API 14+ devices.
49.
50.
API 14 theme customizations can go here.
51.
*/
52.
public static final int AppBaseTheme=0x7f050000;
53.
/** Application theme.
54.
All customizations that are NOT specific to a particular API-level can go here.
55.
*/
56.
public static final int AppTheme=0x7f050001;
57.
}
58.
}
APK(Android
Application package) File
An apk
file is created by the framework automatically. If you want to run the android
application on the mobile, transfer and install it.
Resources
It
contains resource files including activity_main, strings, styles etc.
Manifest
file
It
contains information about package including components such as activities,
services, content providers etc.
Dalvik Virtual Machine
As we
know the modern JVM is high performance and provides excellent memory
management. But it need to be optimized for low-powered handheld devices. The
Dalvik Virtual Machine is optimized for mobile devices. It optimizes the JVM
for memory, performance,
and battery
life.
Dalvik is
the name of a town in Iceland. The Dalvik VM was written by Dan Bornstein.
The Dex
compiler converts the class files into the .dex files that run on the Dalvik
VM.
Let's see
the compiling and packaging process from the source file:
The javac
tool compiles
the java source file into the class file.
The dx
tool takes
all the class files of your application and generates a single .dex file. It is
a platform-specific tool.
The aapt
(Android Assets Packaging Tool) handles
the packaging process.
AndroidManifest.xml file in android
The AndroidManifest.xml
file contains
information about your package, including components of the application such as
activities, services, content providers, broadcast receivers etc.
It
performs some other tasks also:
- It is responsible
to protect the application to
access any protected parts by providing the permissions.
- It also declares
the android api that the
application is going to use.
- It lists
the instrumentation classes. The instumentation classes provides
profiling and other informations. These informations are removed just
before the application is published etc.
This is
the required xml file for all the android application and located inside the
root directory.
A simple
AndroidManifest.xml file looks like this:
1.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2.
package="com.abc.hello"
3.
android:versionCode="1"
4.
android:versionName="1.0" >
5.
6.
<uses-sdk
7.
android:minSdkVersion="8"
8.
android:targetSdkVersion="15" />
9.
10.
<application
11.
android:icon="@drawable/ic_launcher"
12.
android:label="@string/app_name"
13.
android:theme="@style/AppTheme" >
14.
<activity
15.
android:name=".MainActivity"
16.
android:label="@string/title_activity_main" >
17.
<intent-filter>
18.
<action android:name="android.intent.action.MAIN" />
19.
20.
<category android:name="android.intent.category.LAUNCHER" />
21.
</intent-filter>
22.
</activity>
23.
</application>
24.
25.
</manifest>
Elements of the AndroidManifest.xml file
The
elements used in the above xml file are described below.
<manifest>
manifest is
the root element of the AndroidManifest.xml file. It includes the namespace
declaration. It haspackage attribute
that describes the package name of the activity class.
<application>
application is
the subelement of the manifest. It includes the namespace declaration. This
element contains several subelements that declares the application component
such as activity etc.
The
commonly used attributes are of this element are icon, label, theme etc.
android:icon represents
the icon for all the application components.
android:label works
as the default label for all the application components. It should be set as a
reference to string resource.
android:theme represents
a common theme for all the activities.
<activity>
activity is
the subelement of application and represents an activity. Each activity must be
defined in the AndroidManifest.xml file. It has many attributes such as name,
label etc.
android:label represents
a label for the activity, often displayed on the screen.
android:name represents
a name for the activity class. It is required attribute.
<intent-filter>
intent-filter is
the subelement of activity and describes the type of intent to which activity,
service or broadcast receiver can respond to.
<action>
It adds
an action for the intent-filter. The intent-filter must have one or more action
elements.
<category>
It adds
an category name to an intent-filter.
R.java file in android
It is an
auto-generated file by aapt (Android Asset Packaging Tool) that
contains resouce IDs for all the resources of res/ directory.
If you create any component
in the activity_main.xml file, id for the corresponding component is
automatically created in this file. This id can be used in the activity source
file to perform any action on the component.
1.
/* AUTO-GENERATED FILE. DO NOT MODIFY.
2.
*
3.
* This class was automatically generated by the
4.
* aapt tool from the resource data it found. It
5.
* should not be modified by hand.
6.
*/
7.
8.
package com.example.helloandroid;
9.
10.
public final class R {
11.
public static final class attr {
12.
}
13.
public static final class drawable {
14.
public static final int ic_launcher=0x7f020000;
15.
}
16.
public static final class id {
17.
public static final int menu_settings=0x7f070000;
18.
}
19.
public static final class layout {
20.
public static final int activity_main=0x7f030000;
21.
}
22.
public static final class menu {
23.
public static final int activity_main=0x7f060000;
24.
}
25.
public static final class string {
26.
public static final int app_name=0x7f040000;
27.
public static final int hello_world=0x7f040001;
28.
public static final int menu_settings=0x7f040002;
29.
}
30.
public static final class style {
31.
/**
32.
Base application theme, dependent on API level. This theme is replaced
33.
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
34.
35.
36.
Theme customizations available in newer API levels can go in
37.
res/values-vXX/styles.xml, while customizations related to
38.
backward-compatibility can go here.
39.
40.
41.
Base application theme for API 11+. This theme completely replaces
42.
AppBaseTheme from res/values/styles.xml on API 11+ devices.
43.
44.
API 11 theme customizations can go here.
45.
46.
Base application theme for API 14+. This theme completely replaces
47.
AppBaseTheme from BOTH res/values/styles.xml and
48.
res/values-v11/styles.xml on API 14+ devices.
49.
50.
API 14 theme customizations can go here.
51.
*/
52.
public static final int AppBaseTheme=0x7f050000;
53.
/** Application theme.
54.
All customizations that are NOT specific to a particular API-level can go here.
55.
*/
56.
public static final int AppTheme=0x7f050001;
57.
}
58. }
No comments:
Post a Comment