Professional Documents
Culture Documents
http://www.Word-to-PDF-Converter.net
Mobile: 9099340469
Email: jayesh.in_ahd@yahoo.com
A hands-on Guide to building mobile applications, this book features concise and
compelling examples that show you how to quickly construct real-world mobile
applications for Android phones. Fully up-to-date for version 1.0 of the Android SDK. It
covers all the essential features and explores the advances capabilities of Android.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
1. Android Terms
4. Context
1. Android SDK
2. Eclipse
3. Configuration
4. Devices
D. Error Handling
1. Create Project
3. Create Attributes
4. Add UI Elements
5. Maintain UI Properties
7. Start Project
1. Overview
1. Project
2. Add a menu
3. Using preference
4. Run
H. Content Provider
1. Overview
3. Example
I. Scroll View
K. Important View
2. File Explorer
L. Shell
N. Thanks You
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
Linux Kernel Core services (including hardware drivers, process and memory management,
security, network, and power management) are handled by a Linux 2.6 kernel. The kernel also
provides an abstraction layer between the hardware and the remainder of the stack.
Libraries Running on top of the kernel, Android include various C/C++ core libraries such as libc
and SSL, as well as:
Graphics libraries that include SGL and OpenGL for 2D and 3D graphics
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
SQLite for native database support
SSL and WebKit for integrated web browser and Internet security
Android Run Time What makes an Android phone an Android phone rather than a mobile Linux
implementation is the Android run time. Including the core libraries and the Dalvik virtual
machine, the Android run time is the engine that powers your applications and, along with the
libraries, forms the basis for the application framework.
Core Libraries While Android development is done in Java, Dalvik is not a Java VM. The core
Android libraries provide most of the functionality available in the core Java libraries as well as
the Android-specifi c libraries.
Dalvik Virtual Machine Dalvik is a register-based virtual machine that’s been optimized to ensure
that a device can run multiple instances effi ciently. It relies on the Linux kernel for threading and
low-level memory management.
Application Framework The application framework provides the classes used to create Android
applications. It also provides a generic abstraction for hardware access and manages the user
interface and application resources.
Application Layer All applications, both native and third party, are built on the application layer
using the same API libraries. The application layer runs within the Android run time using the
classes and services made available from the application framework.
Android uses a special Java virtual machine (Dalvik) which is based on the Apache
Harmony Java implementation. Dalvik uses special byte code. Therefore you cannot
run standard Java byte code on Android. Android provides a tool "dx" which allows to
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
convert Java Class files into "dex" (Dalvik Executable) files. Android applications are
then packed into an .apk (Android Package) file.
Android supports 2-D and 3-D graphics using the OpenGL libraries and supports data
storage in a SQLLite database.
For development Google provides the Android Development Tools (ADT) for Eclipse to
develop Android applications.
Every Android applications runs in its own process and it isolated from other running
applications. Therefore on misbehaving application cannot harm other Android
applications.
Intends allow the application to request and / or provide services . For example the
application call ask via an intent for a contact application. Application register themself
via an IntentFilter. Intends are a powerful concept as they allow to create loosely
coupled applications.
· onPause() - always called if the Activity ends, can be used to release ressource
or save data
B.4 Context
The class android.content.Context provides the connections to the Android system.
Contexts provides the method getSystemService which allows to receive a manager
object for the different hardware parts. As Activities and Services extend this class you
can directly access the context via "this".
C.2 Eclipse
Use the Eclipse update manager to install all available plugins for the Android
Development Tools (ADT) from the URL https://dl-ssl.google.com/android/eclipse/ .
C.3 Configuration
In Eclipse open the Preferences dialog via Windows -> Preferences. Select Android
and maintain the installation path of the Android SDK.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
Tip
If you maintain the location the Android plugin will remind you frequently (and for every
workspace). Join me in starring at Bug 3210 to get this improved.
Select now Window -> Android SDK and AVD Manager from the menu.
Select available packages and select the latest version of the SDK.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
Press "Install selected" and confirm the license for all package.
C.4 Device
You need to define a device which can be used for emulation. Press the device
manager button, press "New" and maintain the following.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
Press "Create AVD".This will create the device. To test if you setup is correct, eelect
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
your device and press "Start".
Tip
You can use the perspective "DDMS" to monitor your device.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
D. Error Handling
Things are not always working as they should be. Several users report that get the
following errors:
2. The project could not be built until build path errors are resolved.
To solve this error select from the menu Project -> Clean.
If you having problems with your own code you can use the LogCat viewer as
described in LogCat Viewer .
Tip
I think this wizard should have the option to add the project to an existing working set.
Please stare at Android New Project Wizard should have the option to add to Working
set to get this functionality.
Press "Finish". This should create the following directory structure.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
"R.java" is a generated class which contains the text and the UI elements. Please do
not try to modify this class manually.
Android allows to create attributes for resources, e.g. for strings and / or colors. These
attributes can be used in your UI definition via XML or in your Java source code.
Select the file "res/values/string.xml" and press "Add". Select "Color" and maintain
"myColor" as the name and "#3399CC" as the value.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
Add also the following "String" attributes. String attributes allow to translate the
application at a later point.
Switch to the XML representation and validate that you maintained the values correctly.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
<resources>
<color name="myColor">#3399CC</color>
<string name="buttonHandler">myClickHandler</string>
<string name="calc">Calculate</string>
</resources>
Delete the "Hello World, Hello!" via a right mouse click. From the "Views" bar, drag in
an "EditText". Add from the layout a "RadioGroup" and then two RadioButtons, add one
"Button". The result should look like the following and the corresponding XML is listed
below. Make sure that your code is the same as listed below.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
Switch to "main.xml" and verify that your XML looks like the following.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup android:id="@+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:text="@+id/RadioButton01"
android:id="@+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></RadioButton>
<RadioButton android:text="@+id/RadioButton02"
android:id="@+id/RadioButton02"
android:layout_width="wrap_content"
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
android:layout_height="wrap_content"></RadioButton>
</RadioGroup>
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
Assign the "celsius" string attribute to your "text" property of the first radio button and
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
"fahrenheit" to the second. Set the property "Checked" to true for the first RadioButton.
Assign "calc" to the text property of your button and assign "buttonHandler" to the
"onClick" property. Delete the text property in the EditText (this means no text will be
initially shown) and set the "Input type" property to "numberSigned" and "number
decimal".
Select the complete widget and use the Properties view to set the property
"background" to the color attribute "@color/myColor".
Switch to the "main.xml" tab and verify that the XML is correctly maintained.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/myColor">
<RadioGroup android:id="@+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/celsius"
android:checked="true"></RadioButton>
<RadioButton android:id="@+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fahrenheit"></RadioButton>
</RadioGroup>
<Button android:id="@+id/Button01"
android:layout_height="wrap_content"
android:onClick="@string/buttonHandler" android:layout_width="wrap_content"
android:text="@string/calc"></Button>
</LinearLayout>
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
package de.vogella.android.temperature;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
switch (view.getId()) {
case R.id.Button01:
if (text.getText().length() == 0) {
Toast.makeText(
this,
return;
float inputValue =
Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {
text.setText(String
.valueOf(convertFahrenheitToCelcius(inputValue)));
} else {
text.setText(String
.valueOf(convertCelciusToFahrenheit(inputValue)));
if (fahrenheitButton.isChecked()) {
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
} else {
fahrenheitButton.setChecked(true);
celsiusButton.setChecked(false);
break;
// Converts to celcius
// Converts to fahrenheit
}
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
Type in a number, select your conversion and press the button. The result should be
displayed and the other option should get selected.
F. Lists
F.1. Overview
List can be used to display a scrollable list of items. You can either use lists in your
layout or if the purpose of your Activity is to show primary a list you can extend
ListActivities which provides nice hocks for typical actions for lists.
package de.vogella.android.listactivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
super.onCreate(icicle);
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked,
names));
}
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
@Override
Object o = this.getListAdapter().getItem(position);
.show();
}
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
Create the following layout file "rowlayout.xml" in the res/layout folder of your project
"de.vogella.android.listactivity".
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</ImageView>
<TextView android:text="@+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/label" android:textSize="30px"></TextView>
</LinearLayout>
Change your activity "MyList" to the following. This is almost the same coding as in the
previous example, the only difference is that we are using our own layout in the
ArrayAdapter and telling the adapter which UI element should contains the text.
package de.vogella.android.listactivity2;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
super.onCreate(icicle);
// Use your own layout and point the adapter to the UI elements
which contains the label
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.rowlayout,
R.id.label, names));
@Override
Object o = this.getListAdapter().getItem(position);
.show();
}
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
package de.vogella.android.listactivity;
import android.app.Activity;
import android.view.LayoutInflater;
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
this.context = context;
this.names = names;
@Override
label.setText(names[position]);
System.out.println(names[position]);
if (names[position].startsWith("Windows7") ||
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
names[position].startsWith("iPhone") ) {
imageView.setImageResource(R.drawable.alt_window_16);
return rowView;
package de.vogella.android.listactivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
super.onCreate(icicle);
@Override
Object o = this.getListAdapter().getItem(position);
.show();
G.1. Project
This chapter will demonstrate how to create and evaluate a menu, how to define
preferences and how to navigate between activities via an intent . Create a project
"de.vogella.android.preferences" with the activity "HelloPreferences". Change the UI in
the file "/res/layout/main.xml" to the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="wrap_content" android:text="Show
Preferences"></Button>
</LinearLayout>
Press Add and select "Item". Maintain the following value. This defines the entries in
your menu. We will have only one entry.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
package de.vogella.android.preferences;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
@Override
inflater.inflate(R.menu.menu, menu);
return true;
Run your application and press "Menu" on the emulator. Your menu should be
displayed.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
Press Add, add a category and add two preferences "EditTextPreferences" to this
category : "User" and "Password".
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
package de.vogella.android.preferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
@Override
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Select "AndroidManifest.xml" and the tab "Application". Add the activity "Preferences".
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
To use the preferences add a button to your main.xml with the id "@+id/Button01" and
change the coding of HelloPreferences to the following.
package de.vogella.android.preferences;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
SharedPreferences preferences;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Initialize preferences
preferences =
PreferenceManager.getDefaultSharedPreferences(this);
button.setOnClickListener(new OnClickListener() {
@Override
String username =
preferences.getString("username", "n/a");
String password =
preferences.getString("password", "n/a");
Toast.makeText(HelloPreferences.this,
Toast.LENGTH_LONG).show();
});
@Override
inflater.inflate(R.menu.menu, menu);
return true;
@Override
switch (item.getItemId()) {
case R.id.preferences:
startActivity(i);
Toast.makeText(HelloPreferences.this,
Toast.LENGTH_LONG).show();
break;
return true;
G.4. Run
Run your application. Press the "menu" hardware button and then select your menu
item "Preferences". You should be able to enter your user settings then press the back
hardware button to return to your main activity and press the button. The saved values
should be displayed in a small message windows (Toast).
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
H. Content Provider
H.1. Overview
ContentProvider are used to provide data from an application to another.
ContentProvider do not store the data but provide the interface for other applications to
access the data.
The following example will use an existing context provider from "Contacts".
H.3. Example
Create a new Android project "de.vogella.android.contentprovider" with the activity
"ContactsView".
Rename the id of the the existing TextView from the example wizard to "contactview".
Delete the default text. Also change the layout_height to "fill_parent".
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/contactview"
/>
</LinearLayout>
In AndroidManifest.xml add the User Permission that the application can use
"android.permission.READ_CONTACTS".
package de.vogella.android.contentprovider;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
while (cursor.moveToNext()) {
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
contactView.append("Name: ");
contactView.append(displayName);
contactView.append("\n");
// Run query
ContactsContract.Contacts.DISPLAY_NAME };
+ ("1") + "'";
sortOrder);
I. Scroll View
ScrollViews can be used to contain one child that might be to big to fit on one screen. If
the child is to big the ScrollView will display a scroll bar to scroll the context. Of course
the child can be a layout which can then contain other elements.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fillViewport="true">
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="@+id/Button01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Submit" android:layout_weight="1.0"></Button>
<Button android:id="@+id/Button02"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Cancel" android:layout_weight="1.0"></Button>
</LinearLayout>
</LinearLayout>
</ScrollView>
package de.vogella.android.scrollview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
@Override
super.onCreate(savedInstanceState);
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
setContentView(R.layout.main);
String s="";
s += "vogella.de ";
view.setText(s);
The attribute "android:fillViewport="true"" ensures that the the scrollview is set to the
full screen even if the elements are smaller then one screen and the "layout_weight"
tell the android system that these elements should be extended.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
The Android platform provides a lot of pre-defined services, usually exposed via a
Manager class. In this chapter we will use the AlertManager and VibratorManager. The
alarm manager will in our example our own broadcast receiver.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/time" android:hint="Number
of seconds" android:inputType="numberDecimal"></EditText><Button
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/ok" android:onClick="startAlert" android:text="Start
Counter"></Button>
</LinearLayout>
Create the following broadcast receiver class. This class will get the Vibrator service.
package de.vogella.android.alarm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.widget.Toast;
@Override
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
Toast.LENGTH_LONG).show();
vibrator.vibrate(2000);
Maintain this class as broadcast receiver in "AndroidManifest.mf" and allow the vibrate
authorization.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.vogella.android.alarm" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".AlarmActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category
android:name="android.intent.category.LAUNCHER" />
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true">
</receiver>
</application>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
</manifest>
Now define your main Activitiy. This activity will create an Intent for the Broadcast
receiver and get the AlarmManager service.
package de.vogella.android.alarm;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
import android.widget.Toast;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int i = Integer.parseInt(text.getText().toString());
alarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.LENGTH_LONG).show();
K. Important Views
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
You can see the log (including System.out.print() statements) via the LogCat view.
The file explorer allows to see the files on the android simulator.
L. Shell
adb shell
This will connect you to your device and give you Linux command line access to the
underlying file system, e.g. ls, rm, mkdir, etc. The application data is stored in the
directory "/data/data/package_of_your_app".
If you have several devices running you can issue commands to one individuel device.
adb devices
#Result
emulator-5554 attached
emulator-5555 attached
For example to change the power settings of your phone, to receive an sms and to get
an incoming call make the following.
# connects to device
For more information on the emulator console please see Emulator Console manual
Turn on "USB Debugging" on your device in the settings. Select in the settings
Applications > Development, then enable USB debugging. You also need to install the
driver for your mobile phone. For details please see Developing on a Device . Please
note that the Android version you are developing for must be the installed version on
your phone.
To select your phone, select the "Run Configurations", select "Manual" selection and
select your device.
( Word to PDF Converter - Unregistered )
http://www.Word-to-PDF-Converter.net
Thank You