Android Tutorial
Android Tutorial
Android Tutorial
Audience
This tutorial has been prepared for beginners to help them understand basic
Android programming. After completing this tutorial, you will find yourself at a
moderate level of expertise in Android programming from where you can take
yourself to next levels.
Prerequisites
Android programming is based on Java programming language. If you have a
basic understanding of Java programming, then it will be fun to learn Android
application development.
Table of Contents
About the Tutorial i
Audience i
Prerequisites i
Copyright & Disclaimer i
Table of Contents ii
1. OVERVIEW 1
What is Android? 1
Features of Android 1
Android Applications 2
2. ENVIORNMENT SETUP 4
Step 1 - Setup Java Development Kit (JDK) 4
Step 2 - Setup Android SDK 5
Step 3 - Setup Eclipse IDE 6
Step 4 - Setup Android Development Tools (ADT) Plugin 7
Step 5 - Create Android Virtual Device 9
3. ARCHITECTURE 11
Linux kernel 11
Libraries 11
Android Runtime 12
Application Framework 12
Applications 12
4. APPLICATIONS COMPONENT 13
Activities 13
Services 14
ii
Broadcast Receivers 14
Content Providers 14
Additional Components 15
7. ACTIVITIES 32
8. SERVICES 38
9. BROADCAST RECEIVERS 49
Creating the Broadcast Receiver 49
Registering Broadcast Receiver 49
Broadcasting Custom Intents 51
Content URIs 58
Create Content Provider 59
11. FRAGMENTS 74
Fragment Life Cycle 75
How to use Fragments? 76
vi
viii
ix
xi
xii
xiii
xiv
xv
1. OVERVIEW
Android
What is Android?
Android is an open source and Linux-based Operating System for mobile
devices such as smartphones and tablet computers. Android was developed by
the Open Handset Alliance, led by Google, and other companies.
Android offers a unified approach to application development for mobile devices
which means developers need to develop only for Android, and their applications
should be able to run on different devices powered by Android.
The first beta version of the Android Software Development Kit (SDK) was
released by Google in 2007, whereas the first commercial version, Android 1.0,
was released in September 2008.
On June 27, 2012, at the Google I/O conference, Google announced the next
Android version, 4.1 Jelly Bean. Jelly Bean is an incremental update, with the
primary aim of improving the user interface, both in terms of functionality and
performance.
The source code for Android is available under free and open source software
licenses. Google publishes most of the code under the Apache License version
2.0 and the rest, Linux kernel changes, under the GNU General Public License
version 2.
Features of Android
Android is a powerful operating system competing with Apple 4GS and support
great features. Few of them are listed below:
Feature
Description
Beautiful UI
Connectivity
Storage
Android
Media support
Messaging
Web browser
Multi-touch
Multi-tasking
User can jump from one task to another and same time
various application can run simultaneously.
Resizable widgets
Multi-Language
GCM
Wi-Fi Direct
Android Beam
Android Applications
Android applications are usually developed in the Java language using the
Android Software Development Kit.
Once developed, Android applications can be packaged easily and sold out either
through a store such as Google Play or the Amazon Appstore.
Android powers hundreds of millions of mobile devices in more than 190
countries around the world. It's the largest installed base of any mobile platform
2
Android
and is growing fast. Every day more than 1 million new Android devices are
activated worldwide.
This tutorial has been written with an aim to teach you how to develop and
package Android application. We will start from environment setup for Android
application programming and then drill down to look into various aspects of
Android applications.
2. ENVIORNMENT SETUP
Android
You will be glad to know that you can start your Android application
development on either of the following operating systems:
Second point is that all the required tools to develop Android applications are
freely available and can be downloaded from the Web. Following is the list of
software's you will need before you start your Android application programming.
Android SDK
Here last two components are optional and if you are working on Windows
machine then these components make your life easy while doing Java based
application development. So let us have a look at how to proceed to set the
required environment.
Android
Android
Once you launched SDK manager, it is time to install other required packages.
By default it will list down total 7 packages to be installed, but we will suggest to
de-select Documentation for Android SDK and Samples for SDK packages to
reduce installation time. Next click the Install 7 Packages button to proceed,
which will display following dialogue box:
If you agree to install all the packages, select Accept All radio button and
proceed by clicking Install button. Now let SDK manager do its work and you
go, pick up a cup of coffee and wait until all the packages are installed. It may
take some time depending on your internet connection. Once all the packages
are installed, you can close SDK manager using top-right cross button.
Android
Android
Now
use Add button
to
add ADT
Plugin as
name
and https://dlssl.google.com/android/eclipse/ as the location. Then click OK to add this
location. As soon as you will click OK button to add this location, Eclipse starts
searching for the plug-in available in the given location and finally lists down the
found plugins.
Android
Now select all the listed plug-ins using Select All button and click Next button
which will guide you ahead to install Android Development Tools and other
required plugins.
Android
10
3. ARCHITECTURE
Android
Linux kernel
At the bottom of the layers is Linux - Linux 2.6 with approximately 115 patches.
This provides basic system functionality like process management, memory
management, device management like camera, keypad, display etc. Also, the
kernel handles all the things that Linux is really good at, such as networking and
a vast array of device drivers, which take the pain out of interfacing to
peripheral hardware.
Libraries
On top of Linux kernel there is a set of libraries including open-source Web
browser engine WebKit, well known library libc, SQLite database which is a
useful repository for storage and sharing of application data, libraries to play and
record audio and video, SSL libraries responsible for Internet security etc.
11
Android
Android Runtime
This is the third section of the architecture and available on the second layer
from the bottom. This section provides a key component called Dalvik Virtual
Machine which is a kind of Java Virtual Machine specially designed and
optimized for Android.
The Dalvik VM makes use of Linux core features like memory management and
multi-threading, which is intrinsic in the Java language. The Dalvik VM enables
every Android application to run in its own process, with its own instance of the
Dalvik virtual machine.
The Android runtime also provides a set of core libraries which enable Android
application developers to write Android applications using standard Java
programming language.
Application Framework
The Application Framework layer provides many higher-level services to
applications in the form of Java classes. Application developers are allowed to
make use of these services in their applications.
Applications
You will find all the Android application at the top layer. You will write your
application to be installed on this layer only. Examples of such applications are
Contacts Books, Browser, Games, etc.
12
Android
4. APPLICATIONS COMPONENT
Description
Activities
Services
Broadcast Receivers
Content Providers
Activities
An activity represents a single screen with a user interface. For example, an
email application might have one activity that shows a list of new emails,
another activity to compose an email, and one for reading emails. If an
application has more than one activity, then one of them should be marked as
the activity that is presented when the application is launched.
An activity is implemented as a subclass of Activity class as follows:
public class MainActivity extends Activity
{
13
Android
Services
A service is a component that runs in the background to perform long-running
operations. For example, a service might play music in the background while the
user is in a different application, or it might fetch data over the network without
blocking user interaction with an activity.
A service is implemented as a subclass of Service class as follows:
public class MyService extends Service
{
Broadcast Receivers
Broadcast Receivers simply respond to broadcast messages from other
applications or from the system. For example, applications can also initiate
broadcasts to let other applications know that some data has been downloaded
to the device and is available for them to use, so this is broadcast receiver who
will intercept this communication and will initiate appropriate action.
A broadcast receiver is implemented as a subclass of BroadcastReceiver class
and each message is broadcasted as an Intent object.
public class MyReceiver
extends
BroadcastReceiver
Content Providers
A content provider component supplies data from one application to others on
request.
Such
requests
are
handled
by
the
methods
of
the ContentResolver class. The data may be stored in the file system, the
database or somewhere else entirely.
A content provider is implemented as a subclass of ContentProvider class and
must implement a standard set of APIs that enable other applications to perform
transactions.
public class MyContentProvider extends
ContentProvider
14
Android
}
We will go through these tags in detail while covering application components in
individual chapters.
Additional Components
There are additional components which will be used in the construction of above
mentioned entities, their logic, and wiring between them. These components
are:
Components
Description
Fragments
Views
Layouts
Intents
Resources
External elements,
drawable pictures.
Manifest
such
control
as
screen
strings,
format
and
constants
and
15
Android
Let us start actual programming with Android Framework. Before you start
writing your first example using Android SDK, you have to make sure that you
have setup your Android development environment properly as explained
in Android - Environment Setup tutorial. We also assume, that you have a little bit
working knowledge with Eclipse IDE.
So let us proceed to write a simple Android Application which will print "Hello
World!".
Next, follow the instructions provided and keep all other entries as default till the
final step. Once your project is created successfully, you will have the following
project screen:
16
Android
17
Android
S.N.
src
This contains the .java source files for your project. By default, it
includes anMainActivity.java source file having an activity class that runs
when your app is launched using the app icon.
gen
This contains the .R file, a compiler-generated file that references all the
18
Android
resources found in your project. You should not modify this file.
3
bin
This folder contains the Android package files .apk built by the ADT
during the build process and everything else needed to run an Android
application.
res/drawable-hdpi
This is a directory for drawable objects that are designed for highdensity screens.
res/layout
This is a directory for files that define your app's user interface.
res/values
This is a directory for other various XML files that contain a collection of
resources, such as strings and colors definitions.
AndroidManifest.xml
This is the manifest file which describes the fundamental characteristics
of the app and defines each of its components.
Following section will give a brief overview few of the important application files.
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
19
Android
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Here, R.layout.activity_main refers to the activity_main.xml file located in
the res/layout folder. The onCreate() method is one of many methods that are
fired when an activity is loaded.
Android
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Here <application>...</application> tags enclosed the components related to
the application. Attribute android:icon will point to the application icon available
underres/drawable-hdpi. The application uses the image named ic_launcher.png
located in the drawable folders.
The <activity> tag is used to specify an activity and android:name attribute
specifies the fully qualified class name of the Activity subclass and
the android:label attributes specifies a string to use as the label for the activity.
You can specify multiple activities using <activity> tags.
The action for the intent filter is named android.intent.action.MAIN to indicate
that this activity serves as the entry point for the application. The category for
the intent-filter is named android.intent.category.LAUNCHER to indicate that the
application can be launched from the device's launcher icon.
The @string refers
to
the strings.xml file
explained
below.
Hence, @string/app_name refers to the app_name string defined in the
strings.xml file, which is "HelloWorld". Similar way, other strings get populated
in the application.
Following is the list of tags which you will use in your manifest file to specify
different Android application components:
Android
text, and similar types of strings go into this file. This file is responsible for their
textual content. For example, a default string file will look like as following file:
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
The R File
The gen/com.example.helloworld/R.java file is the glue between the activity
Java files likeMainActivity.java and the resources like strings.xml. It is an
automatically generated file and you should not modify the content of the R.java
file. Following is a sample of R.java file:
/* AUTO-GENERATED FILE.
DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found.
It
package com.example.helloworld;
Android
}
public static final class id {
public static final int menu_settings=0x7f080000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int activity_main=0x7f070000;
}
public static final class string {
public static final int app_name=0x7f050000;
public static final int hello_world=0x7f050001;
public static final int menu_settings=0x7f050002;
public static final int title_activity_main=0x7f050003;
}
public static final class style {
public static final int AppTheme=0x7f060000;
}
}
<TextView
android:layout_width="wrap_content"
23
Android
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
This is an example of simple RelativeLayout which we will study in a separate
chapter. TheTextView is an Android control used to build the GUI and it has
various attributes like android:layout_width, android:layout_height, etc., which
are being used to set its width and height etc. The @string refers to the
strings.xml file located in the res/values folder. Hence, @string/hello_world
refers to the hello string defined in the strings.xml file, which is "Hello World!".
24
Android
Congratulations! You have developed your first Android Application and now just
keep following rest of the tutorial step by step to become a great Android
Developer. All the very best!
25
Android
Organize Resources
You should place each type of resource in a specific subdirectory of your
project's res/directory. For example, here's the file hierarchy for a simple
project:
MyProject/
src/
MyActivity.java
res/
drawable/
icon.png
layout/
activity_main.xml
info.xml
values/
strings.xml
The res/ directory contains all the resources in various sub-directories. Here we
have an image resource, two layout resources, and a string resource file.
Following table gives a detail about the resource directories supported inside
project res/ directory.
Directory
Resource Type
anim/
26
Android
color/
XML files that define a state list of colors. They are saved in
res/color/ and accessed from the R.color class.
drawable/
Image files like .png, .jpg, .gif or XML files that are compiled
into bitmaps, state lists, shapes, animation drawables. They
are
saved
in
res/drawable/
and
accessed
from
the R.drawable class.
layout/
XML files that define a user interface layout. They are saved in
res/layout/ and accessed from the R.layout class.
menu/
raw/
values/
xml/
Android
Alternative Resources
Your application should provide alternative resources to support specific device
configurations. For example, you should include alternative drawable resources
(i.e. images) for different screen resolution and alternative string resources for
different languages. At runtime, Android detects the current device configuration
and loads the appropriate resources for your application.
To specify configuration-specific alternatives for a set of resources, follow these
steps:
Create a new directory in res/ named in the form <resources_name><config_qualifier>. Here resources_name will be any of the resources
mentioned in the above table, like layout, drawable etc. The qualifier will
specify an individual configuration for which these resources are to be
used. You can check official documentation for a complete list of qualifiers
for different type of resources.
Below is an example which specifies images for a default screen and alternative
images for high resolution screen.
MyProject/
src/
MyActivity.java
res/
drawable/
icon.png
background.png
drawable-hdpi/
icon.png
background.png
layout/
activity_main.xml
info.xml
values/
strings.xml
28
Android
Below is another example which specifies layout for a default language and
alternative layout for Arabic language (layout-ar/).
MyProject/
src/
MyActivity.java
res/
drawable/
icon.png
background.png
drawable-hdpi/
icon.png
background.png
layout/
activity_main.xml
info.xml
layout-ar/
main.xml
values/
strings.xml
Accessing Resources
During your application development you will need to access defined resources
either in your code, or in your layout XML files. Following section explains how to
access your resources in both the scenarios:
Android
imageView.setImageResource(R.drawable.myimage);
Here first line of the code uses the R.id.myimageview to get ImageView defined
with idmyimageview in a Layout file. Second line of code uses
the R.drawable.myimage to get an image with name myimage available in
drawable sub-directory under /res.
Example:
Consider next example where res/values/strings.xml has following definition:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string
name="hello">Hello, World!</string>
</resources>
Now you can set the text on a TextView object with ID msg using a resource ID
as follows:
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello);
Example:
Consider a layout res/layout/activity_main.xml with the following definition:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
30
Android
This application code will load this layout for an Activity, in the onCreate()
method as follows:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
31
7. ACTIVITIES
Android
The Activity class defines the following callbacks i.e. events. You don't need to
implement all the callback methods. However, it's important that you
understand each one and implement those that ensure your app behaves the
way users expect.
Callback
Description
onCreate()
This is the first callback and called when the activity is first
created.
onStart()
Android
user.
onResume()
onPause()
The paused activity does not receive user input and cannot
execute any code and called when the current activity is being
paused and the previous activity is being resumed.
onStop()
onDestroy()
onRestart()
Example:
This example will take you through simple steps to show Android application activity
life cycle. Follow the below mentioned steps to modify the Android application we
created in Hello World Example chapter:
Step
Description
You will use Eclipse IDE to create an Android application and name it as
HelloWorld under a package com.example.helloworld as explained in
the Hello World Example chapter.
Run the application to launch Android emulator and verify the result of
the changes done in the application.
Following
is
the
content
of
the
modified
main
activity
file
src/com.example.helloworld/MainActivity.java. This file includes each of
the fundamental lifecycle methods. The Log.d() method has been used to
generate log messages:
package com.example.helloworld;
33
Android
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
34
Android
super.onPause();
Log.d(msg, "The onPause() event");
}
Android
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
If either the MAIN action or LAUNCHER category are not declared for one of your
activities, then your app icon will not appear in the Home screen's list of apps.
Let's try to run our modified Hello World! application we just modified. We
assume, you had created your AVD while doing environment setup. To run the
app from Eclipse, open one of your project's activity files and click Run
icon
from the toolbar. Eclipse installs the app on your AVD and starts it and if
everything is fine with your setup and application, it will display Emulator
window and you should see following log messages in LogCat window in Eclipse
IDE:
07-19 15:00:43.405: D/Android :(866): The onCreate() event
07-19 15:00:43.405: D/Android :(866): The onStart() event
07-19 15:00:43.415: D/Android :(866): The onResume() event
36
Android
37
8. SERVICES
Android
Description
Started
Bound
A service has lifecycle callback methods that you can implement to monitor
changes in the service's state and you can perform work at the appropriate
stage. The following diagram on the left shows the lifecycle when the service is
created with startService() and the diagram on the right shows the lifecycle
when the service is created with bindService():(image courtesy : android.com )
38
Android
To create a service, you create a Java class that extends the Service base class
or one of its existing subclasses. The Service base class defines various callback
methods and the most important are given below. You don't need to implement
all the callback methods. However, it is important that you understand each one
and implement those that ensure your app behaves the way users expect.
Callback
Description
onStartCommand()
onBind()
Android
onRebind()
onCreate()
onDestroy()
import android.app.Service;
import android.os.IBinder;
import android.content.Intent;
import android.os.Bundle;
Android
/** Called when The service is no longer used and is being destroyed
*/
41
Android
@Override
public void onDestroy() {
}
}
Example:
This example will take you through simple steps to show how to create your own
Android Service. Follow the below mentioned steps to modify the Android application
we created in Hello World Example chapter:
Step
Description
You will use Eclipse IDE to create an Android application and name it as
HelloWorld under a package com.example.helloworld as explained in
the Hello World Example chapter.
Modify
main
activity
add startService() and stopService()methods.
Create
a
new
java
file MyService.java under
the
package com.example.helloworld. This file will have implementation of
Android service related methods.
Define
two
constants start_service and stop_service in res/values/strings.xml file.
Run the application to launch Android emulator and verify the result of
the changes done in the application.
file MainActivity.java to
Following
is
the
content
of
the
modified
main
activity
file
src/com.example.helloworld/MainActivity.java. This file can include each
of
the
fundamental
lifecycle
methods.
We
have
added startService() and stopService() methods to start and stop the service.
42
Android
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
Android
package com.example.helloworld;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed",
Toast.LENGTH_LONG).show();
}
}
Following is the modified content of AndroidManifest.xml file. Here we have
added <service.../> tag to include our service:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
44
Android
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".MyService" />
</application>
</manifest>
Following will be the content of res/layout/activity_main.xml file to include
two buttons:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/btnStartService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/start_service"
android:onClick="startService"/>
<Button android:id="@+id/btnStopService"
android:layout_width="fill_parent"
45
Android
android:layout_height="wrap_content"
android:text="@string/stop_service"
android:onClick="stopService" />
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new
constants:
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="start_service">Start Service</string>
<string name="stop_service">Stop Service</string>
</resources>
Let's try to run our modified Hello World! application we just modified. We
assume, you had created your AVD while doing environment setup. To run the
app from Eclipse, open one of your project's activity files and click Run
icon
from the toolbar. Eclipse installs the app on your AVD and starts it and if
everything is fine with your setup and application, it will display following
Emulator window:
46
Android
Now to start your service, let's click on Start Service button, this will start the
service and as per our programming in onStartCommand() method, a
message Service Started will appear on the bottom of the simulator as follows:
47
Android
To stop the service, you can click the Stop Service button.
48
Android
9. BROADCAST RECEIVERS
There is one additional step in case you are going to implement your custom
intents; then you will have to create and broadcast those intents.
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.",
Toast.LENGTH_LONG).show();
}
Android
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
</application>
Now whenever your Android device gets booted, it will be intercepted by
BroadcastReceiver MyReceiver and implemented logic inside onReceive() will be
executed.
There are several system generated events defined as final static fields in
the Intent class. The following table lists a few important system events.
Event Constant
Description
android.intent.action.BATTERY_CHANGED
android.intent.action.BATTERY_LOW
android.intent.action.BATTERY_OKAY
android.intent.action.BOOT_COMPLETED
android.intent.action.BUG_REPORT
android.intent.action.CALL
Android
by the data.
android.intent.action.CALL_BUTTON
android.intent.action.DATE_CHANGED
android.intent.action.REBOOT
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="com.tutorialspoint.CUSTOM_INTENT">
</action>
</intent-filter>
51
Android
</receiver>
</application>
Example:
This example will explain you how to create BroadcastReceiver to intercept custom
intent. Once you are familiar with custom intent, then you can program your
application to intercept system generated intents. So let's follow the below
mentioned steps to modify the Android application we created in Hello World
Example chapter:
Step
Description
You will use Eclipse IDE to create an Android application and name it as
HelloWorld under a package com.example.helloworld as explained in
the Hello World Example chapter.
Modify
main
activity
add broadcastIntent() method.
Run the application to launch Android emulator and verify the result of
the changes done in the application.
file MainActivity.java to
Following
is
the
content
of
the
modified
main
activity
file
src/com.example.helloworld/MainActivity.java. This file can include each
of the fundamental lifecycle methods. We have added broadcastIntent() method
to broadcast a custom intent.
package com.example.helloworld;
52
Android
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
// broadcast a custom intent.
public void broadcastIntent(View view)
{
Intent intent = new Intent();
intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
sendBroadcast(intent);
}
}
Following is the content of src/com.example.helloworld/MyReceiver.java:
package com.example.helloworld;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
53
Android
import android.widget.Toast;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.",
Toast.LENGTH_LONG).show();
}
}
Following will be the modified content of AndroidManifest.xml file. Here we have
added <service.../> tag to include our service:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
54
Android
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="com.tutorialspoint.CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
</application>
</manifest>
Following will be the content of res/layout/activity_main.xml file to include a
button to broadcast our custom intent:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/btnStartService"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/broadcast_intent"
android:onClick="broadcastIntent"/>
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new
constants:
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="broadcast_intent">Broadcast Intent</string>
</resources>
55
Android
Let's try to run our modified Hello World! application we just modified. We
assume, you had created your AVD while doing environment setup. To run the
app from Eclipse, open one of your project's activity files and click Run
icon
from the toolbar. Eclipse installs the app on your AVD and starts it and if
everything is fine with your setup and application, it will display following
Emulator window:
Now to broadcast our custom intent, let's click on Broadcast Intent button, this
will broadcast our custom intent "com.tutorialspoint.CUSTOM_INTENT" which will
be intercepted by our registered BroadcastReceiver i.e. MyReceiver and as per
our implemented logic a toast will appear on the bottom of the simulator as
follows:
56
Android
system
57
Android
ContentProvider
Content URIs
To query a content provider, you specify the query string in the form of a URI
which has following format:
<prefix>://<authority>/<data_type>/<id>
Here is the detail of various parts of the URI:
Part
Description
prefix
58
Android
authority
data_type
id
First of all you need to create a Content Provider class that extends the
ContentProviderbase class.
Secondly, you need to define your content provider URI address which will
be used to access the content.
Next you will need to create your own database to keep the content.
Usually, Android uses SQLite database and framework needs to
override onCreate() method which will use SQLite Open Helper method to
create or open the provider's database. When your application is
launched, the onCreate() handler of each of its Content Providers is called
on the main application thread.
Finally register
<provider> tag.
your
Content
Provider
in
your
activity
file
using
Here is the list of methods which you need to override in Content Provider class
to have your Content Provider working:
Android
getType() This method returns the MIME type of the data at the given
URI.
Example:
This example will explain you how to create your own ContentProvider. So let's
follow the following steps similar to what we followed while creating Hello World
Example:
Step
Description
You will use Eclipse IDE to create an Android application and name it as
MyContentProvider under a package com.example.mycontentprovider,
with blank Activity.
Run the application to launch Android emulator and verify the result of
the changes done in the application.
Following
is
the
content
of
the
modified
main
activity
file
src/com.example.mycontentprovider/MainActivity.java. This file can
include each of the fundamental lifecycle methods. We have added two new
methods onClickAddName()and onClickRetrieveStudents() to
handle
user
interaction with the application.
package com.example.mycontentprovider;
60
Android
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
values.put(StudentsProvider.NAME,
((EditText)findViewById(R.id.txtName)).getText().toString());
values.put(StudentsProvider.GRADE,
61
Android
((EditText)findViewById(R.id.txtGrade)).getText().toString());
Toast.makeText(getBaseContext(),
uri.toString(), Toast.LENGTH_LONG).show();
}
c.getString(c.getColumnIndex( StudentsProvider.NAME))
+
", " + c.getString(c.getColumnIndex(
StudentsProvider.GRADE)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
}
}
Create
new
file
StudentsProvider.java
under com.example.mycontentprovider package and following is the content
of src/com.example.mycontentprovider/StudentsProvider.java:
package com.example.mycontentprovider;
import java.util.HashMap;
62
Android
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
Android
/**
* Database specific constant declarations
*/
private SQLiteDatabase db;
static final String DATABASE_NAME = "College";
static final String STUDENTS_TABLE_NAME = "students";
static final int DATABASE_VERSION = 1;
static final String CREATE_DB_TABLE =
" CREATE TABLE " + STUDENTS_TABLE_NAME +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
" name TEXT NOT NULL, " +
" grade TEXT NOT NULL);";
/**
* Helper class that actually creates and manages
* the provider's underlying data repository.
*/
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_DB_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " +
STUDENTS_TABLE_NAME);
onCreate(db);
64
Android
}
}
@Override
public boolean onCreate() {
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
/**
* Create a write able database which will trigger its
* creation if it doesn't already exist.
*/
db = dbHelper.getWritableDatabase();
return (db == null)? false:true;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
/**
* Add a new student record
*/
long rowID = db.insert(
/**
* If record is added successfully
*/
if (rowID > 0)
{
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLException("Failed to add a record into " + uri);
}
@Override
65
Android
switch (uriMatcher.match(uri)) {
case STUDENTS:
qb.setProjectionMap(STUDENTS_PROJECTION_MAP);
break;
case STUDENT_ID:
qb.appendWhere( _ID + "=" + uri.getPathSegments().get(1));
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
if (sortOrder == null || sortOrder == ""){
/**
* By default sort on student names
*/
sortOrder = NAME;
}
Cursor c = qb.query(db,
return c;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
66
Android
int count = 0;
switch (uriMatcher.match(uri)){
case STUDENTS:
count = db.delete(STUDENTS_TABLE_NAME, selection,
selectionArgs);
break;
case STUDENT_ID:
String id = uri.getPathSegments().get(1);
count = db.delete( STUDENTS_TABLE_NAME, _ID +
" = " + id +
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)){
case STUDENTS:
count = db.update(STUDENTS_TABLE_NAME, values,
selection, selectionArgs);
break;
case STUDENT_ID:
count = db.update(STUDENTS_TABLE_NAME, values, _ID +
" = " + uri.getPathSegments().get(1) +
67
Android
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
/**
* Get all student records
*/
case STUDENTS:
return "vnd.android.cursor.dir/vnd.example.students";
/**
* Get a particular student
*/
case STUDENT_ID:
return "vnd.android.cursor.item/vnd.example.students";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
}
Following will be the modified content of AndroidManifest.xml file. Here we have
added <provider.../> tag to include our content provider:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mycontentprovider"
68
Android
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mycontentprovider.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<provider android:name="StudentsProvider"
android:authorities="com.example.provider.College">
</provider>
</application>
</manifest>
Following will be the content of res/layout/activity_main.xml file to include a
button to broadcast your custom intent:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
69
Android
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="@+id/txtName"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Grade" />
<EditText
android:id="@+id/txtGrade"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<Button
android:text="Add Name"
android:id="@+id/btnAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickAddName" />
<Button
android:text="Retrieve Students"
android:id="@+id/btnRetrieve"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickRetrieveStudents" />
</LinearLayout>
Make sure you have following content of res/values/strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyContentProvider</string>
70
Android
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>;
Let's try to run our modified MyContentProvider application we just created.
We assume, you had created your AVD while doing environment setup. To run
the app from Eclipse, open one of your project's activity files and click
Run icon from the toolbar. Eclipse installs the app on your AVD and starts it
and if everything is fine with your setup and application, it will display following
Emulator window, be patience because it may take some time based on your
computer speed:
Now let's enter student Name and Grade and finally click on Add Name button,
this will add student record in the database and will flash a message at the
bottom showing ContentProvider URI along with record number added in the
database. This operation makes use of our insert() method. Let's repeat this
process to add few more students in the database of our content provider.
71
Android
Once you are done with adding records in the database, now its time to ask
ContentProvider to give us those records back, so let's click Retrieve
Students button which will fetch and display all the records one by one which is
as per our implementation of our query() method.
72
Android
You can write activities against update and delete operations by providing
callback functions in MainActivity.java file and then modify user interface to
have buttons for update and deleted operations in the same way as we have
done for add and read operations.
This way you can use existing Content Provider like Address Book or you can use
Content Provider concept in developing nice database oriented applications
where you can perform all sort of database operations like read, write, update
and delete as explained above in the example.
73
11. FRAGMENTS
Android
A fragment has its own layout and its own behavior with its own lifecycle
callbacks.
You can combine multiple fragments in a single activity to build a multipane UI.
Fragment life cycle is closely related to the lifecycle of its host activity
which means when the activity is paused, all the fragments available in
the activity will also be stopped.
You create fragments by extending Fragment class and you can insert a
fragment into your activity layout by declaring the fragment in the activity's
layout file, as a <fragment> element.
Prior to fragment introduction, we had a limitation because we can show only a
single activity on the screen at one given point in time. So we were not able to
divide device screen and control different parts separately. But with the
introduction of fragment we got more flexibility and removed the limitation of
having a single activity on the screen at a time. Now we can have a single
activity but each activity can comprise of multiple fragments which will have
their own layout, events and complete lifecycle.
Following is a typical example of how two UI modules defined by fragments can
be combined into one activity for a tablet design, but separated for a handset
design.
74
Android
75
Android
First of all decide how many fragments you want to use in an activity. For
example, we want to use two fragments to handle landscape and portrait
modes of the device.
Next, based on number of fragments, create classes which will extend the
Fragment class. The Fragment class has above mentioned callback
functions. You can override any of the functions based on your
requirements.
76
Android
Finally modify activity file to define the actual logic of replacing fragments
based on your requirement.
Here is the list of important methods which you can override in your fragment
class:
onCreate() The system calls this when creating the fragment. You should
initialize essential components of the fragment that you want to retain
when the fragment is paused or stopped, then resumed.
onCreateView() The system calls this callback when it's time for the
fragment to draw its user interface for the first time. To draw a UI for
your fragment, you must return a View component from this method that
is the root of your fragment's layout. You can return null if the fragment
does not provide a UI.
onPause() The system calls this method as the first indication that the
user is leaving the fragment. This is usually where you should commit any
changes that should be persisted beyond the current user session.
Example:
This example will explain you how to create your own Fragments. Here we will
create two fragments and one of them will be used when device is in landscape
mode and another fragment will be used in case of portrait mode. So let's follow the
below mentioned steps similar to what we followed while creating Hello World
Example:
Step
Description
You will use Eclipse IDE to create an Android application and name it as
MyFragments under a package com.example.myfragments, with blank
Activity.
Create layout
files res/layout/lm_fragment.xml and res/layout/pm_fragment.xml and
77
Android
Run the application to launch Android emulator and verify the result of
the changes done in the application.
Following
is
the
content
of
the
modified
main
src/com.example.mycontentprovider/MainActivity.java:
activity
file
package com.example.myfragments;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.view.WindowManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
78
Android
}
Create two fragment files LM_Fragement.java and PM_Fragment.java under
com.example.mycontentprovider package.
Following is the content of LM_Fragement.java file:
package com.example.myfragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Android
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Android
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/landscape_message"
android:textColor="#000000"
android:textSize="20px" />
-->
</LinearLayout>
Following is the content of pm_fragment.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#666666">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/portrait_message"
android:textColor="#000000"
81
Android
android:textSize="20px" />
-->
</LinearLayout>
Following will be the content of res/layout/activity_main.xml file which
includes your fragments:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment
android:name="com.example.fragments"
android:id="@+id/lm_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment
android:name="com.example.fragments"
android:id="@+id/pm_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
Make sure you have following content of res/values/strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
82
Android
<string name="app_name">MyFragments</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="landscape_message">This is Landscape mode fragment
</string>
<string name="portrait_message">This is Portrait mode fragment
</string>
</resources>
Let's try to run our modified MyFragments application we just created. We
assume, you had created your AVD while doing environment setup. To run the
app from Eclipse, open one of your project's activity files and click Run
icon
from the toolbar. Eclipse installs the app on your AVD and starts it and if
everything is fine with your setup and application, it will display Emulator
window where you will click on Menu button to see the following window. Be
patience because it may take some time based on your computer speed:
ctrl+F11 on Windows.
83
Android
ctrl+F11 on Linux.
Once you changed the mode, you will be able to see the GUI which you have
implemented for landscape mode as below:
This way you can use same activity but different GUIs through different
fragments. You can use different type of GUI components for different GUIs
based on your requirements.
84
Android
Context.startActivity()
The Intent object is passed to this method to launch a new activity or
to get an existing activity to do something new.
Context.startService()
The Intent object is passed to this method to initiate a service or
deliver new instructions to an ongoing service.
Context.sendBroadcast()
The Intent object is passed to this method to deliver the message to
all interested broadcast receivers.
85
Android
Intent Objects
An Intent object is a bundle of information which is used by the component that
receives the intent plus information used by the Android system.
An Intent object can contain the following components based on what it is
communicating or going to perform:
Action
This is mandatory part of the Intent object and is a string naming the action to
be performed or, in the case of broadcast intents, the action that took place and
is being reported. The action largely determines how the rest of the intent object
is structured. The Intent class defines a number of action constants
corresponding to different intents. Here is a list of Android Intent Standard Actions
The action in an Intent object can be set by the setAction() method and read by
getAction().
Data
The URI of the data to be acted on and the MIME type of that data. For example,
if the action field is ACTION_EDIT, the data field would contain the URI of the
document to be displayed for editing.
The setData() method specifies data only as a URI, setType() specifies it only as
a MIME type, and setDataAndType() specifies it as both a URI and a MIME type.
The URI is read by getData() and the type by getType().
Some examples of action/data pairs are:
S.N.
ACTION_VIEW content://contacts/people/1
Display information about the person whose identifier is "1".
ACTION_DIAL content://contacts/people/1
Display the phone dialer with the person filled in.
ACTION_VIEW tel:123
Display the phone dialer with the given number filled in.
ACTION_DIAL tel:123
Display the phone dialer with the given number filled in.
86
Android
ACTION_EDIT content://contacts/people/1
Edit information about the person whose identifier is "1".
ACTION_VIEW content://contacts/people/
Display a list of people, which the user can browse through.
Category
The category is an optional part of Intent object and it's a string containing
additional information about the kind of component that should handle the
intent. The addCategory() method places a category in an Intent object,
removeCategory() deletes a category previously added, and getCategories()
gets the set of all categories currently in the object. Here is a list of Android Intent
Standard Categories.
You can check detail on Intent Filters in below section to understand how do we
use categories to choose appropriate activity corresponding to an Intent.
Extras
This will be in key-value pairs for additional information that should be delivered
to the component handling the intent. The extras can be set and read using the
putExtras() and getExtras() methods respectively. Here is a list of Android Intent
Standard Extra Data
Flags
These flags are optional part of Intent object and instruct the Android system
how to launch an activity, and how to treat it after it is launched etc.
Component Name
This optional field is an android ComponentName object representing either
Activity, Service or BroadcastReceiver class. If it is set, the Intent object is
delivered to an instance of the designated class, otherwise Android uses other
information in the Intent object to locate a suitable target.
The component name is set by setComponent(), setClass(), or setClassName()
and read by getComponent().
Types of Intents
There are following two types of intents supported by Android till version 4.1
87
Android
Explicit Intents
These intents designate the target component by its name and they are typically
used for application-internal messages - such as an activity starting a
subordinate service or launching a sister activity. For example:
// Explicit Intent by specifying its class name
Intent i = new Intent(this, TargetActivity.class);
i.putExtra("Key1", "ABC");
i.putExtra("Key2", "123");
// Starts TargetActivity
startActivity(i);
Implicit Intents
These intents do not name a target and the field for the component name is left
blank. Implicit intents are often used to activate components in other
applications. For example:
// Implicit Intent by specifying a URI
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.example.com"));
88
Android
Step
Description
You will use Eclipse IDE to create an Android application and name it as
IntentDemo under a package com.example.intentdemo. While creating
this project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify src/MainActivity.java file and add the code to define two listeners
corresponding two buttons i.e. Start Browser and Start Phone.
Run the application to launch Android emulator and verify the result of
the changes done in the application.
Following
is
the
content
of
the
modified
src/com.example.intentdemo/MainActivity.java.
main
activity
file
package com.example.intentdemo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
89
Android
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Following will be the content of res/layout/activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
90
Android
android:orientation="vertical" >
<Button android:id="@+id/start_browser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/start_browser"/>
<Button android:id="@+id/start_phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/start_phone" />
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new
constants:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">IntentDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="start_browser">Start Browser</string>
<string name="start_phone">Start Phone</string>
</resources>
Following is the default content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentdemo"
android:versionCode="1"
android:versionName="1.0" >
91
Android
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.intentdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your IntentDemo application. We assume, you had created
your AVD while doing environment setup. To run the app from Eclipse, open one
of your project's activity files and click Run
icon from the toolbar. Eclipse
installs the app on your AVD and starts it and if everything is fine with your
setup and application, it will display following Emulator window:
92
Android
Now click on Start Browser button, which will start a browser configured and
display http://www.example.com as shown below:
Similarly, you can launch phone interface using Start Phone button, which will
allow you to dial already given phone number.
Intent Filters
You have seen how an Intent has been used to call another activity. Android OS
uses filters to pinpoint the set of Activities, Services, and Broadcast receivers
that can handle the Intent with help of specified set of action, categories, data
scheme associated with an Intent. You will use <intent-filter> element in the
manifest file to list down actions, categories and data types associated with any
activity, service, or broadcast receiver.
93
Android
A filter <intent-filter> may list more than one action as shown above but
this list cannot be empty; a filter must contain at least one <action>
element, otherwise it will block all intents. If more than one actions are
mentioned then Android tries to match one of the mentioned actions
before invoking the activity.
A filter <intent-filter> may list zero, one or more than one categories. If
there is no category mentioned then android always pass this test but if
more than one categories are mentioned then for an intent to pass the
category test, every category in the Intent object must match a category
in the filter.
Each <data> element can specify a URI and a data type (MIME media
type). There are separate attributes like scheme, host, port,
and path for each part of the URI. An Intent object that contains both a
URI and a data type passes the data type part of the test only if its type
matches a type listed in the filter.
94
Android
Example:
Following example is a modification of the above example. Here we will see how
Android resolves conflict if one intent is invoking two activities defined in, next how
to invoke a custom activity using a filter and third one is an exception case if
Android does not file appropriate activity defined for an intent.
Step
Description
You will use Eclipse IDE to create an Android application and name it as
IntentDemo under a package com.example.intentdemo. While creating
this project, make sure your Target SDK and Compile With are at the
latest version of Android SDK to use higher levels of APIs.
Run the application to launch Android emulator and verify the result of
the changes done in the application.
Following
is
the
content
of
the
modified
src/com.example.intentdemo/MainActivity.java.
main
activity
file
package com.example.intentdemo;
import android.net.Uri;
import android.os.Bundle;
95
Android
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Android
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the
// action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following
is
the
content
of
the
modified
main
src/com.example.intentdemo/CustomActivity.java.
activity
file
package com.example.intentdemo;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
Android
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_view);
}
Following will be the content of res/layout/activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/start_browser_a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/start_browser_a"/>
<Button android:id="@+id/start_browser_b"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/start_browser_b"/>
<Button android:id="@+id/start_browser_c"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/start_browser_c"/>
98
Android
</LinearLayout>
Following will be the content of res/layout/custom_view.xml file:
<?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:id="@+id/show_data"
android:layout_width="fill_parent"
android:layout_height="400dp"/>
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new
constants:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">IntentDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="start_browser_a">Start Browser with VIEW
action</string>
<string name="start_browser_b">Start Browser with LAUNCH
action</string>
<string name="start_browser_c">Exception Condition</string>
</resources>
Following is the default content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentdemo"
99
Android
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.intentdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name="com.example.intentdemo.CustomActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="com.example.intentdemo.LAUNCH" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
</activity>
</application>
</manifest>
100
Android
Let's try to run your IntentDemo application. We assume, you had created
your AVD while doing environment setup. To run the app from Eclipse, open one
of your project's activity files and click Run
icon from the toolbar. Eclipse
installs the app on your AVD and starts it and if everything is fine with your
setup and application, it will display following Emulator window:
Now let's start with first button "Start Browser with VIEW Action". Here we have
defined our custom activity with a filter "android.intent.action.VIEW", and there
is already one default activity against VIEW action defined by Android which is
launching web browser. So android displays following two options to select the
activity you want to launch.
101
Android
Now if you select Browser, then Android will launch web browser and open
example.com website but if you select IndentDemo option then Android will
launch CustomActivity which does nothing but just capture passed data and
displays in a text view as follows:
102
Android
Now go back using back button and click on "Start Browser with LAUNCH Action"
button, here Android applies filter to choose define activity and it simply launch
your custom activity and again it displays following screen:
Again, go back using back button and click on "Exception Condition" button, here
Android tries to find out a valid filter for the given intent but it does not find a
valid activity defined because this time we have used data as https instead
of http though we are giving a correct action, so Android raises an exception and
shows following screen:
103
13. UI LAYOUTS
Android
The basic building block for user interface is a View object which is created from
the View class and occupies a rectangular area on the screen and is responsible
for drawing and event handling. View is the base class for widgets, which are
used to create interactive UI components like buttons, text fields, etc.
The ViewGroup is a subclass of View and provides invisible container that hold
other Views or other ViewGroups and define their layout properties.
At third level we have different layouts which are subclasses of ViewGroup class
and a typical layout defines the visual structure for an Android user interface and
can be created either at run time using View/ViewGroup objects or you can
declare your layout using simple XML file main_layout.xml which is located in
the res/layout folder of your project.
This tutorial is more about creating your GUI based on layouts defined in XML
file. A layout may contain any type of widgets such as buttons, labels,
textboxes, and so on. Following is a simple example of XML file having
LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a Button" />
-->
</LinearLayout>
104
Android
Once your layout is defined, you can load the layout resource from your
application code, in your Activity.onCreate() callback implementation as shown
below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Linear Layout
LinearLayout is a view group that aligns all children in a single direction,
vertically or horizontally.
Relative Layout
RelativeLayout is a view group that displays child views in relative
positions.
Table Layout
TableLayout is a view that groups views into rows and columns.
Absolute Layout
AbsoluteLayout enables you to specify the exact location of its children.
Frame Layout
The FrameLayout is a placeholder on screen that you can use to display
a single view.
List View
ListView is a view group that displays a list of scrollable items.
Grid View
GridView is a ViewGroup that displays items in a two-dimensional,
scrollable grid.
105
Android
Layout Attributes
Each layout has a set of attributes which define the visual properties of that
layout. There are few common attributes among all the layouts and there are
other attributes which are specific to that layout. Following are common
attributes and will be applied to all the layouts:
Attribute
Description
android:id
android:layout_width
android:layout_height
android:layout_marginTop
android:layout_marginBottom
android:layout_marginLeft
android:layout_marginRight
android:layout_gravity
android:layout_weight
android:layout_x
android:layout_y
android:layout_width
android:layout_width
106
Android
android:paddingLeft
android:paddingRight
android:paddingTop
android:paddingBottom
Here width and height are the dimension of the layout/view which can be
specified in terms of dp (Density-independent Pixels), sp (Scale-independent
Pixels), pt (Points which is 1/72 of an inch), px (Pixels), mm (Millimeters) and
finally in (inches).
You can specify width and height with exact measurements but more often, you
will use one of these constants to set the width or height:
Gravity attribute plays important role in positioning the view object and it can
take one or more (separated by '|') of the following constant values.
Constant
Value
Description
top
0x30
bottom
0x50
left
0x03
right
0x05
center_vertical
0x10
fill_vertical
0x70
Android
center_horizontal
0x01
fill_horizontal
0x07
center
0x11
fill
0x77
clip_vertical
0x80
clip_horizontal
0x08
start
0x00800003
end
0x00800005
View Identification
A view object may have a unique ID assigned to it which will identify the View
uniquely within the tree. The syntax for an ID, inside an XML tag is:
android:id="@+id/my_button"
108
Android
The at-symbol (@) at the beginning of the string indicates that the XML
parser should parse and expand the rest of the ID string and identify it as
an ID resource.
The plus-symbol (+) means that this is a new resource name that must
be created and added to our resources. To create an instance of the view
object and capture it from the layout, use the following:
109
14. UI CONTROLS
Android
An Android application user interface is everything that the user can see and
interact with. You have learned about the various layouts that you can use to
position your views in an activity. This chapter will give you detail on various
views.
A View is an object that draws something on the screen that the user can
interact with and a ViewGroup is an object that holds other View (and
ViewGroup) objects in order to define the layout of the user interface.
You define your layout in an XML file which offers a human-readable structure
for the layout, similar to HTML. For example, a simple vertical layout with a text
view and a button looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a Button" />
</LinearLayout>
Android UI Controls
There are number of UI controls provided by Android that allows you to build the
graphical user interface for your app.
S.N.
110
Android
TextView
EditText
AutoCompleteTextView
Button
ImageButton
CheckBox
An on/off switch that can be toggled by the user. You should use
checkboxes when presenting users with a group of selectable options
that are not mutually exclusive.
7
ToggleButton
RadioButton
RadioGroup
ProgressBar
111
Android
11
Spinner
A drop-down list that allows users to select one value from a set.
12
TimePicker
The TimePicker view enable users to select a time of the day, in either
24-hour mode or AM/PM mode.
13
DatePicker
Create UI Controls
As explained in previous chapter, a view object may have a unique ID assigned
to it which will identify the View uniquely within the tree. The syntax for an ID,
inside an XML tag is:
android:id="@+id/text_id"
To create a UI Control/View/Widget you will have to define a view/widget in the
layout file and assign it a unique ID as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am a TextView" />
</LinearLayout>
Then finally create an instance of the Control object and capture it from the
layout, use the following:
TextView myText = (TextView) findViewById(R.id.text_id);
112
Android
Events are a useful way to collect data about a user's interaction with interactive
components of your app, like button presses or screen touch etc. The Android
framework maintains an event queue into which events are placed as they occur
and then each event is removed from the queue on a first-in, first-out (FIFO)
basis. You can capture these events in your program and take appropriate action
as per requirements.
There are following three concepts related to Android Event Management:
onClick()
OnClickListener()
This is called when the user either clicks or touches or
focuses upon any widget like button, text, image etc. You
will use onClick() event handler to handle such event.
onLongClick()
OnLongClickListener()
This is called when the user either clicks or touches or
focuses upon any widget like button, text, image etc. for
one or more seconds. You will use onLongClick() event
handler to handle such event.
onFocusChange()
OnFocusChangeListener()
This is called when the widget loses its focus i.e. user
goes away from the view item. You will use
onFocusChange() event handler to handle such event.
113
Android
onKey()
OnFocusChangeListener()
This is called when the user is focused on the item and
presses or releases a hardware key on the device. You
will use onKey() event handler to handle such event.
onTouch()
OnTouchListener()
This is called when the user presses the key, releases the
key, or any movement gesture on the screen. You will
use onTouch() event handler to handle such event.
onMenuItemClick()
OnMenuItemClickListener()
This is called when the user selects a menu item. You will
use onMenuItemClick() event handler to handle such
event.
There are many more event listeners available as a part of View class like
OnHoverListener, OnDragListener etc., which may be needed for your
application. So we recommend to refer official documentation for Android
application development in case you are going to develop a sophisticated app.
Below section will provide you detailed examples on all the three scenarios:
Android
But if you have applied the handler to more than one control, you would have to
cut and paste the code for the handler and if the code for the handler is long, it
makes the code harder to maintain.
Following are the simple steps to show how we will make use of separate
Listener class to register and capture click event. Similarly, you can implement
your listener for any other required event type.
Step
Description
You will use Eclipse IDE to create an Android application and name it as
EventDemo under a package com.example.eventdemo as explained in
the Hello World Example chapter.
Run the application to launch Android emulator and verify the result of
the changes done in the application.
click
event
listeners
and
Following
is
the
content
of
the
modified
main
activity
file
src/com.example.eventdemo/MainActivity.java. This file can include each
of the fundamental life-cycle methods.
package com.example.eventdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
115
Android
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
116
Android
}
Following will be the content of res/layout/activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button_s"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/button_small"/>
<Button
android:id="@+id/button_l"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/button_large"/>
<TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:capitalize="characters"
android:text="@string/hello_world" />
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new
constants:
117
Android
<string name="app_name">EventDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="button_small">Small Font</string>
<string name="button_large">Large Font</string>
</resources>
Following is the default content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.guidemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.guidemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
118
Android
/>
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your EventDemo application. We assume, you had created
your AVD while doing environment setup. To run the app from Eclipse, open one
of your project's activity files and click Run
icon from the toolbar. Eclipse
installs the app on your AVD and starts it and if everything is fine with your
setup and application, it will display following Emulator window:
Now you try to click on two buttons one by one and you will see that font of
the Hello World text will change, which happens because registered click event
handler method is being called against each click event.
119
Android
Step
Description
Run the application to launch Android emulator and verify the result of
the changes done in the application.
click
event
listeners
and
Following
is
the
content
of
the
modified
main
activity
file
src/com.example.eventdemo/MainActivity.java. This file can include each
of the fundamental life-cycle methods.
package com.example.eventdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
120
Android
@Override
public boolean onCreateOptionsMenu(Menu menu) {
121
Android
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Now again let's try to run your EventDemo application. We assume, you had
created your AVD while doing environment setup. To run the app from Eclipse,
open one of your project's activity files and click Run
icon from the toolbar.
Eclipse installs the app on your AVD and starts it and if everything is fine with
your setup and application, it will display following Emulator window:
Now you try to click on two buttons one by one and you will see that font of
the Hello World text will change, which happens because registered click event
handler method is being called against each click event.
Android
Following are the simple steps to show how we can make use of layout file
Main.xml to register and capture click event.
Step
Description
Run the application to launch Android emulator and verify the result of
the changes done in the application.
click
event
to
listeners
specify
and
event
Following
is
the
content
of
the
modified
main
activity
file
src/com.example.eventdemo/MainActivity.java. This file can include each
of the fundamental life-cycle methods.
package com.example.eventdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
123
Android
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following will be the content of res/layout/activity_main.xml file. Here we
have to add android:onClick="methodName" for both the buttons, which will
register given method names as click event handlers.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
124
Android
android:orientation="vertical" >
<Button
android:id="@+id/button_s"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/button_small"
android:onClick="doSmall"/>
<Button
android:id="@+id/button_l"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/button_large"
android:onClick="doLarge"/>
<TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:capitalize="characters"
android:text="@string/hello_world" />
</LinearLayout>
Again let's try to run your EventDemo application. We assume, you had created
your AVD while doing environment setup. To run the app from Eclipse, open one
of your project's activity files and click Run
icon from the toolbar. Eclipse
installs the app on your AVD and starts it and if everything is fine with your
setup and application, it will display following Emulator window:
125
Android
Now you try to click on two buttons one by one and you will see that font of
the Hello World text will change, which happens because registered click event
handler method is being called against each click event.
Exercise:
We recommend you to try writing different event handlers for different event
types and understand exact difference in different event types and their
handling. Events related to menu, spinner, pickers widgets are little different but
they are also based on the same concepts as explained above.
126
Android
If you already know about Cascading Style Sheet (CSS) in web design then
understanding Android Style will be easy as it also works in a similar way. There
are number of attributes associated with each Android widget which you can set
to change your applications look and feel. A style can specify properties such as
height, padding, font color, font size, background color, and much more.
You can specify these attributes in Layout file as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:capitalize="characters"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="@string/hello_world" />
</LinearLayout>
But this way we need to define style attributes for every attribute separately
which is not good for source code maintenance point of view. So we work with
styles by defining them in separate file as explained below.
Defining Styles
A style is defined in an XML resource that is separate from the XML that specifies
the layout. This XML file resides under res/values/ directory of your project
and will have <resources> as the root node which is mandatory for the style
file. The name of the XML file is arbitrary, but it must use the .xml extension.
127
Android
You can define multiple styles per file using <style> tag but each style will have
its name that uniquely identifies the style. Android style attributes are set
using <item> tag as shown below:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomFontStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:capitalize">characters</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">12pt</item>
<item name="android:textColor">#00FF00</item>/>
</style>
</resources>
The value for the <item> can be a keyword string, a hex color, a reference to
another resource type, or other value depending on the style property.
Using Styles
Once your style is defined, you can use it in your XML Layout file
using style attribute as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text_id"
style="@style/CustomFontStyle"
android:text="@string/hello_world" />
</LinearLayout>
To understand the concept related to Android Style, you can check Style Demo
Example.
128
Android
Style Inheritance
Android supports Style Inheritance in very much similar way as cascading style
sheet in web design. You can use this to inherit properties from an existing style
and then define only the properties that you want to change or add.
It
is
simple
to
create
a
new
style LargeFont that
inherits
the CustomFontStyle style defined above, but make the font size big, you can
author the new style like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomFontStyle.LargeFont">
<item name="android:textSize">20ps</item>
</style>
</resources>
You can reference this new style as @style/CustomFontStyle.LargeFont in
your XML Layout file. You can continue inheriting like this as many times as
you'd like, by chaining names with periods. For example, you can extend
FontStyle.LargeFont to be Red, with:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomFontStyle.LargeFont.Red">
<item name="android:textColor">#FF0000</item>/>
</style>
</resources>
This technique for inheritance by chaining together names only works for styles
defined by your own resources. You can't inherit Android built-in styles this way.
To reference an Android built-in style, such as TextAppearance, you must use
the parent attribute as shown below:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomFontStyle" parent="@android:style/TextAppearance">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:capitalize">characters</item>
<item name="android:typeface">monospace</item>
129
Android
<item name="android:textSize">12pt</item>
<item name="android:textColor">#00FF00</item>/>
</style>
</resources>
Android Themes
Hope you understood the concept of Style, so now let's try to understand what is
a Theme. A theme is nothing but an Android style applied to an entire Activity
or application, rather than an individual View.
Thus, when a style is applied as a theme, every View in the Activity or
application will apply each style property that it supports. For example, you can
apply the same CustomFontStyle style as a theme for an Activity and then all
text inside that Activity will have green monospace font.
To set a theme for all the activities of your application, open
the AndroidManifest.xml file and edit the <application> tag to include
the android:theme attribute with the style name. For example:
<application android:theme="@style/CustomFontStyle">
But if you want a theme applied to just one Activity in your application, then add
the android:theme attribute to the <activity> tag only. For example:
<activity android:theme="@style/CustomFontStyle">
There are number of default themes defined by Android which you can use
directly or inherit them using parent attribute as follows:
<style name="CustomTheme" parent="android:Theme.Light">
...
</style>
To understand the concept related to Android Theme, you can check Theme Demo
Example.
Android
131
Android
Android offers a great list of pre-built widgets like Button, TextView, EditText,
ListView, CheckBox, RadioButton, Gallery, Spinner, AutoCompleteTextView etc.
which you can use directly in your Android application development, but there
may be a situation when you are not satisfied with existing functionality of any
of the available widgets. Android provides you with means of creating your own
custom components which you can customize to suit your needs.
If you only need to make small adjustments to an existing widget or layout, you
can simply subclass the widget or layout and override its methods which will
give you precise control over the appearance and function of a screen element.
This tutorial explains you how to create custom Views and use them in your
application using simple and easy steps.
Android
Android
}
Check this example to understand how to Instantiate a Basic Android Custom
Component using code inside an activity.
<com.example.dateviewdemo.DateView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="40sp"
android:background="#000"
/>
</RelativeLayout>
It is important to note here that we are using all TextView attributes along with
custom component without any change. Similarly, you will be able to use all the
events, and methods along with DateView component.
Check this example to understand how to Instantiate a Basic Android Custom
Component using Layout XML file.
134
Android
Step 1
The first step to enable us to use our custom attributes is to define them in a
new xml file under res/values/ and call it attrs.xml. Let's have a look on an
example attrs.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DateView">
<attr name="delimiter" format="string"/>
<attr name="fancyText" format="boolean"/>
</declare-styleable>
</resources>
Here the name=value is what we want to use in our Layout XML file as
attribute, and the format=type is the type of attribute.
Step 2
Your second step will be to read these attributes from Layout XML file and set
them for the component. This logic will go in the constructors that get passed
an AttributeSet, since that is what contains the XML attributes. To read the
values in the XML, you need to first create a TypedArray from the AttributeSet,
then use that to read and set the values as shown in the below example code:
135
Android
TypedArray
a
R.styleable.DateView);
context.obtainStyledAttributes(attrs,
Step 3
Finally you can use your defined attributes in your Layout XML file as follows:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.dateviewdemo
"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
136
Android
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.dateviewdemo.DateView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="40sp"
custom:delimiter="-"
custom:fancyText="true"
/>
</RelativeLayout>
The
important
part
isxmlns:custom="http://schemas.android.com/apk/res/com.example.dateviewd
emo". Note that http://schemas.android.com/apk/res/ will remain as is, but last
part will be set to your package name and also that you can use anything after
xmlns:, in this example we have used custom, but you could use any name you
like.
Check this example to understand how to Create Custom Attributes for Android Custom
Component with simple steps.
137
Android
Android drag/drop framework allows your users to move data from one View to
another View in the current layout using a graphical drag and drop gesture. The
framework includes following three important components to support drag &
drop functionality:
Drag listeners:
Started: This event occurs when you start dragging an item in a layout,
your application calls startDrag() method to tell the system to start a
drag. The arguments inside startDrag() method provide the data to be
dragged, metadata for this data, and a callback for drawing the drag
shadow.
The system first responds by calling back to your application to get a drag
shadow. It then displays the drag shadow on the device.
Next,
the
system
sends
a
drag
event
with
action
ACTION_DRAG_STARTED to the registered drag event listeners for all the
View objects in the current layout.
To continue to receive drag events, including a possible drop event, a
drag event listener must return true, if the drag event listener returns
false, then it will not receive drag events for the current operation until
the system sends a drag event with action type ACTION_DRAG_ENDED.
Continuing: The
user
continues
the
drag.
System
sends
ACTION_DRAG_ENTERED action followed by ACTION_DRAG_LOCATION
action to the registered drag event listener for the View where dragging
point enters. The listener may choose to alter its View object's appearance
in response to the event or can react by highlighting its View.
The drag event listener receives an ACTION_DRAG_EXITED action after
the
user has moved the drag shadow outside the bounding box of the
View.
Dropped: The user releases the dragged item within the bounding box of
a View. The system sends the View object's listener, a drag event with
action type ACTION_DROP.
138
Android
Ended: Just after the action type ACTION_DROP, the system sends out a
drag event with action type ACTION_DRAG_ENDED to indicate that the
drag operation is over.
Constants
Following are all constants integers available as a part of DragEvent class.
S.N.
ACTION_DRAG_STARTED
Signals the start of a drag and drop operation.
ACTION_DRAG_ENTERED
Signals to a View that the drag point has entered the bounding box of
the View.
ACTION_DRAG_LOCATION
Sent to a View after ACTION_DRAG_ENTERED if the drag shadow is still
within the View object's bounding box.
ACTION_DRAG_EXITED
Signals that the user has moved the drag shadow outside the bounding
box of the View.
ACTION_DROP
Signals to a View that the user has released the drag shadow, and the
drag point is within the bounding box of the View.
ACTION_DRAG_ENDED
Signals to a View that the drag and drop operation has concluded.
139
Android
Methods
Following are few important and most frequently used methods available as a
part of DragEvent class.
S.N.
int getAction()
Inspect the action value of this event.
ClipData getClipData()
Returns the ClipData object sent to the system as part of the call to
startDrag().
ClipDescription getClipDescription()
Returns the ClipDescription object contained in the ClipData.
boolean getResult()
Returns an indication of the result of the drag and drop operation.
float getX()
Gets the X coordinate of the drag point.
float getY()
Gets the Y coordinate of the drag point.
String toString()
Returns a string representation of this DragEvent object.
140
Android
The
combination
of
the onDragEvent(DragEvent) method
and View.OnDragListener is
analogous
to
the
combination
of
the onTouchEvent() and View.OnTouchListener used with touch events in old
versions of Android.
Description
You will use Eclipse IDE to create an Android application and name it as
DragNDropDemo under a package com.example.dragndropdemo. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Copy image logo.png in res/drawable-* folder. You can use images with
different resolution in case you want to provide them for different
devices.
Run the application to launch Android emulator and verify the result of
the changes done in the aplication.
141
Android
Following
is
the
content
of
the
modified
main
activity
file
src/com.example.dragndropdemo/MainActivity.java. This file can include
each of the fundamental life-cycle methods.
package com.example.dragndropdemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.widget.*;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ima = (ImageView)findViewById(R.id.iv_logo);
// Sets the tag
ima.setTag(IMAGEVIEW_TAG);
ima.setOnLongClickListener(new View.OnLongClickListener() {
142
Android
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new
ClipData.Item((CharSequence)v.getTag());
myShadow,
null,
);
return true;
}
});
// Create and set the drag event listener for the View
ima.setOnDragListener( new OnDragListener(){
@Override
public boolean onDrag(View v,
DragEvent event){
switch(event.getAction())
{
case DragEvent.ACTION_DRAG_STARTED:
layoutParams = (RelativeLayout.LayoutParams)
v.getLayoutParams();
Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
// Do nothing
break;
143
Android
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
v.setLayoutParams(layoutParams);
break;
case DragEvent.ACTION_DRAG_LOCATION
Android
<ImageView
android:id="@+id/iv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:contentDescription="@string/drag_drop"
/>
</RelativeLayout>
Following will be the content of res/values/strings.xml to define two new
constants:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">DragNDropDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="drag_drop">Click on the image to drag and drop</string>
</resources>
Following is the default content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.guidemo"
android:versionCode="1"
android:versionName="1.0" >
145
Android
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.guidemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your DragNDropDemo application. We assume, you had created
your AVD while doing environment setup. To run the app from Eclipse, open one
of your project's activity files and click Run
icon from the toolbar. Eclipse
installs the app on your AVD and starts it and if everything is fine with your
setup and application, it will display following Emulator window:
146
Android
Now do long click on the displayed android logo and you will see that logo image
moves a little after 1 seconds long click from its place, it is time when you
should start dragging the image. You can drag it around the screen and drop it
at a new location.
147
20. NOTIFICATIONS
Android
Android Toast class provides a handy way to show alerts to the users, but these
alerts are not persistent which means alert flashes on the screen for a few
seconds and then disappears.
For important messages to be given to the user, it is required to have more
persistent method. A notification is a message you can display as an icon at the
top of the device which we call notification bar or status bar.
To see the details of the notification, you will have to select the icon which will
display notification drawer having detail about the notification. While working
with emulator with virtual device, you will have to click and drag down the status
bar to expand it which will give you detail as follows. This will be just 64 dp tall
and called normal view.
Above expanded form can have a Big View which will have additional detail
about the notification. You can add up to six additional lines in the notification.
The following screenshot shows such notification.
148
Android
mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
You have plenty of optional properties which you can set for your notification. To
learn
more
about
them,
see
the
reference
documentation
for
NotificationCompat.Builder.
149
Android
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
Android
S.N.
Notification build()
Combine all of the options that have been set and return a new
Notification object.
NotificationCompat.Builder
intent)
setContentIntent
(PendingIntent
10
151
Android
11
12
13
NotificationCompat.Builder
style)
setStyle
(NotificationCompat.Style
15
16
Example:
Following example shows the functionality of an Android notification using a
NotificationCompat.Builder Class which has been introduced in Android 4.1.
Step
Description
You will use Eclipse IDE to create an Android application and name it as
NotificationDemo under a package com.example.notificationdemo. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
152
Android
Run the application to launch Android emulator and verify the result of
the changes done in the aplication.
Following
is
the
content
of
the
modified
main
activity
file
src/com.example.notificationdemo/MainActivity.java. This file can include
each of the fundamental life-cycle methods.
package com.example.notificationdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
153
Android
Android
NotificationCompat.Builder
mBuilder =
new NotificationCompat.Builder(this);
mBuilder.setContentTitle("New Message");
mBuilder.setContentText("You've received new message.");
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.drawable.woman);
/* Adds the Intent that starts the Activity to the top of the stack
*/
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Android
*/
mNotificationManager.notify(notificationID, mBuilder.build());
}
mBuilder =
new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Updated Message");
mBuilder.setContentText("You've got updated message.");
mBuilder.setTicker("Updated Message Alert!");
mBuilder.setSmallIcon(R.drawable.woman);
/* Increase
arrives */
notification
number
every
time
new
notification
mBuilder.setNumber(++numMessages);
/* Adds the Intent that starts the Activity to the top of the stack
*/
stackBuilder.addNextIntent(resultIntent);
156
Android
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
activity
file
package com.example.notificationdemo;
import android.os.Bundle;
import android.app.Activity;
157
Android
<Button android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/start_note"/>
<Button android:id="@+id/cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cancel_note" />
<Button android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/update_note" />
</LinearLayout>
Following will be the content of res/layout/notification.xml file:
<?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="400dp"
android:text="Hi, Your Detailed notification view goes here...." />
158
Android
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new
constants:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">NotificationDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="start_note">Start Notification</string>
<string name="cancel_note">Cancel Notification</string>
<string name="update_note">Update Notification</string>
</resources>
Following is the default content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.notificationdemo.MainActivity"
159
Android
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name=".NotificationView"
android:label="Details of notification"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</application>
</manifest>
Let's try to run your NotificationDemo application. We assume, you had
created your AVD while doing environment setup. To run the app from Eclipse,
open one of your project's activity files and click Run
icon from the toolbar.
Eclipse installs the app on your AVD and starts it and if everything is fine with
your setup and application, it will display following Emulator window:
160
Android
Now click Start Notification button, you will see a message "New Message
Alert!" at the top that will display momentarily and after that you will have
following screen having a small icon at the top left corner.
Now lets expand the view, long click on the small icon, after a second it will
display date information and this is the time when you should drag status bar
down without releasing mouse. You will see status bar will expand and you will
get following screen:
161
Android
Now let's try to click on the image icon, this will launch your new activity which
you have set using intent and you will have following screen:
Next, you can click on "Detail of notification" and it will take you back to the
main screen where you can try using Update Notification button which will
update existing notification and number will increase by 1 but if you will send
notification with new notification ID then it will keep adding in the stack and you
see them separately listed on the screen.
mBuilder =
new NotificationCompat.Builder(this);
mBuilder.setContentTitle("New Message");
mBuilder.setContentText("You've received new message.");
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.drawable.woman);
162
Android
inboxStyle.addLine(events[i]);
}
mBuilder.setStyle(inboxStyle);
/* Adds the Intent that starts the Activity to the top of the stack
*/
163
Android
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
164
Android
Android location APIs make it easy for you to build location-aware applications,
without needing to focus on the details of the underlying location technology.
This becomes possible with the help of Google Play services, which facilitates
adding location awareness to your app with automated location tracking,
geofencing, and activity recognition.
This tutorial shows you how to use Location Services in your app to get the
current location, get periodic location updates, look up addresses etc.
float getAccuracy()
Get the estimated accuracy of this location, in meters.
double getAltitude()
Get the altitude if available, in meters above sea level.
float getBearing()
Get the bearing, in degrees.
double getLatitude()
Get the latitude, in degrees.
double getLongitude()
165
Android
float getSpeed()
Get the speed if it is available, in meters/second over ground.
boolean hasAccuracy()
True if this location has an accuracy.
boolean hasAltitude()
True if this location has an altitude.
10
boolean hasBearing()
True if this location has a bearing.
11
boolean hasSpeed()
True if this location has a speed.
12
void reset()
Clears the contents of the location.
13
14
15
16
17
166
Android
18
19
String toString()
Returns a string containing a concise, human-readable description of
this object.
GooglePlayServicesClient.ConnectionCallbacks
GooglePlayServicesClient.OnConnectionFailedListener
These interfaces provide following important callback methods, which you need
to implement in your activity class:
S.N.
You should create the location client in onCreate() method of your activity
class, then connect it in onStart(), so that Location Services maintains the
167
Android
current location while your activity is fully visible. You should disconnect the
client in onStop() method, so that when your app is not visible, Location
Services is not maintaining the current location. This helps in saving battery
power up-to a large extent.
setExpirationDuration(long millis)
Set the duration of this request, in milliseconds.
setExpirationTime(long millis)
Set the request expiration time, in millisecond since boot.
setFastestInterval(long millis)
Explicitly set the fastest interval for location updates, in milliseconds.
setInterval(long millis)
Set the desired interval for active location updates, in milliseconds.
168
Android
setNumUpdates(int numUpdates)
Set the number of location updates.
setPriority(int priority)
Set the priority of the request.
Now for example, if your application wants high accuracy location it should
create
a
location
request
with setPriority(int) set
to
PRIORITY_HIGH_ACCURACY and setInterval(long) to 5 seconds. You can also
use bigger interval and/or other priorities like PRIORITY_LOW_POWER for to
request "city" level accuracy or PRIORITY_BALANCED_POWER_ACCURACY for
"block" level accuracy.
Activities should strongly consider removing all location request when entering
the background (for example at onPause()), or at least swap the request to a
larger interval and lower quality to save power consumption.
169
Android
Description
Search for Google Play services option from the given package list
under Extra and if it is not installed, then install it. The Google Play
services SDK is saved in your Android SDK environment at <androidsdk>/extras/google/google_play_services/.
Copy
the
library
project
at <androidsdk>/extras/google/google_play_services/libproject/ project into your
workspace. Click File > Import, select Android > Existing Android
Code
into
Workspace,
and
browse
to <androidsdk>/extras/google/google_play_services/libproject/, library project to
import it.
Description
You will use Eclipse IDE to create an Android application and name it as
LBSDemo/i> under a package com.example.lbsdemo. While creating
this project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
170
Android
Run the application to launch Android emulator and verify the result of
the changes done in the aplication.
Let's add Google Play Service reference in the project. Right click on the
project and select Build Path > Configure Build Path > Android > and then
click Add button which will show google-play-service_lib option to be added, just
double click on it, which will add required library reference and you will have
window as follows:
Following
is
the
content
of
the
modified
src/com.example.lbsdemo/MainActivity.java.
main
activity
file
171
Android
package com.example.lbsdemo;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
Android
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLocationBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
displayCurrentLocation();
}
});
disconnectBtn = (Button) findViewById(R.id.disconnect);
disconnectBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mLocationClient.disconnect();
locationLabel.setText("Got disconnected....");
}
});
connectBtn = (Button) findViewById(R.id.connect);
connectBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mLocationClient.connect();
locationLabel.setText("Got connected....");
}
});
// Create the LocationRequest object
mLocationClient = new LocationClient(this, this, this);
}
@Override
protected void onStart() {
173
Android
super.onStart();
// Connect the client.
mLocationClient.connect();
locationLabel.setText("Got connected....");
}
@Override
protected void onStop() {
// Disconnect the client.
mLocationClient.disconnect();
super.onStop();
locationLabel.setText("Got disconnected....");
}
@Override
public void onConnected(Bundle dataBundle) {
// Display the connection status
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
}
@Override
public void onDisconnected() {
// Display the connection status
Toast.makeText(this, "Disconnected. Please re-connect.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// Display the error code on failure
Toast.makeText(this, "Connection Failure : " +
connectionResult.getErrorCode(),
Toast.LENGTH_SHORT).show();
}
public void displayCurrentLocation() {
// Get the current location's latitude & longitude
Location currentLocation = mLocationClient.getLastLocation();
String msg = "Current Location: " +
174
Android
Double.toString(currentLocation.getLatitude()) + "," +
Double.toString(currentLocation.getLongitude());
/*
* When the task finishes, onPostExecute() displays the address.
*/
@Override
protected void onPostExecute(String address) {
// Display the current address in the UI
addressLabel.setText(address);
}
@Override
protected String doInBackground(Location... params) {
Geocoder geocoder =
new Geocoder(mContext, Locale.getDefault());
// Get the current location from the input parameter list
175
Android
Android
address.getAddressLine(0) : "",
// Locality is usually a city
address.getLocality(),
// The country of the address
address.getCountryName());
// Return the text
return addressText;
} else {
return "No address found";
}
}
}// AsyncTask class
}
Following will be the content of res/layout/activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/getLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/get_location"/>
<Button android:id="@+id/disconnect"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/disconnect"/>
<Button android:id="@+id/connect"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/connect"/>
177
Android
<TextView
android:id="@+id/locationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/addressLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new
constants:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">LBSDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="get_location">Get Location</string>
<string name="disconnect">Disconnect Service</string>
<string name="connect">Connect Service</string>
</resources>
Following is the default content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lbsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
178
Android
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.lbsdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your LBSDemo application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
179
Android
Select mobile device as an option and then check your mobile device which will
display following screen:
Now to see location select Get Location Button which will display location
information as follows:
180
Android
You can try by disconnecting location client using Disconnect Service and then
connecting it by using Connect Service button. You can also modify to get
location update as explained above and in Android Official documentation.
181
Android
You have learnt Android Intent, which is an object carrying an intent i.e.
message from one component to another component within the application or
outside the application.
As such you do not need to develop your email client from scratch because they
are already available like Gmail and K9mail. But you will need to send email
from your Android application, where you will have to write an Activity that
needs to launch an email client and sends an email using your Android device.
For this purpose, your Activity will send an ACTION_SEND along with appropriate
data load, to the Android Intent Resolver. The specified chooser gives the proper
interface for the user to pick how to send your email data.
Following section explains different parts of our Intent object required to send an
email.
EXTRA_BCC
182
Android
EXTRA_CC
A String[] holding e-mail addresses that should be carbon copied.
EXTRA_EMAIL
A String[] holding e-mail addresses that should be delivered to.
EXTRA_HTML_TEXT
A constant String that is associated with the Intent, used with
ACTION_SEND to supply an alternative to EXTRA_TEXT as HTML
formatted text.
EXTRA_SUBJECT
A constant string holding the desired subject line of a message.
EXTRA_TEXT
A constant CharSequence that is associated with the Intent, used with
ACTION_SEND to supply the literal data to be sent.
EXTRA_TITLE
A CharSequence dialog title to provide to the user when used with a
ACTION_CHOOSER.
Here is an example showing you how to assign extra data to your intent:
emailIntent.putExtra(Intent.EXTRA_EMAIL , new
String[]{"recipient@example.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
emailIntent.putExtra(Intent.EXTRA_TEXT
, "body of email");
Example:
Following example shows you in practical how to use Intent object to launch Email
client to send an Email to the given recipients.
To experiment with this example, you will need actual Mobile device equipped
with latest Android OS, otherwise you will have to struggle with emulator which
may not work. Second you will need to have an Email client like GMail or K9mail
installed on your device.
183
Android
Step
Description
You will use Eclipse IDE to create an Android application and name it as
SendEmailDemo under a package com.example.sendemaildemo. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Run the application to launch Android emulator and verify the result of
the changes done in the application.
Following
is
the
content
of
the
modified
main
src/com.example.sendemaildemo/MainActivity.java.
activity
file
package com.example.sendemaildemo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
Android
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void sendEmail() {
Log.i("Send email", "");
String[] TO = {"amrood.admin@gmail.com"};
String[] CC = {"mcmohd@gmail.com"};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
try {
startActivity(Intent.createChooser(emailIntent, "Send
mail..."));
finish();
Log.i("Finished sending email...", "");
185
Android
<Button android:id="@+id/sendEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/compose_email"/>
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new
constants:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SendEmailDemo</string>
<string name="hello_world">Hello world!</string>
186
Android
<string name="action_settings">Settings</string>
<string name="compose_email">Compose Email</string>
</resources>
Following is the default content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendemaildemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sendemaildemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your SendEmailDemo application. We assume, you have
connected your actual Android Mobile device with your computer. To run the app
187
Android
from Eclipse, open one of your project's activity files and click Run icon from
the toolbar. Before starting your application, Eclipse will display following
window to select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which
will display following screen:
Now use Compose Email button to list down all the installed email clients. From
the list, you can choose one of email clients to send your email. I'm going to use
Gmail client to send my email which will have all the provided defaults fields
available as shown below. Here From: will be default email ID you have
registered for your Android device.
188
Android
You can modify either of the given default fields and finally use send email
button (marked with red rectangle) to send your email to the mentioned
recipients.
189
Android
There are following two ways to send SMS using Android device:
Android
ArrayList<PendingIntent> sentIntents,
ArrayList<PendingIntent> deliveryIntents)
Send a multi-part text based SMS.
5
Example:
Following example shows you in practical how to use SmsManager object to send an
SMS to the given mobile number.
To experiment with this example, you will need actual Mobile device equipped
with latest Android OS, otherwise you will have to struggle with emulator which
may not work.
Step
Description
You will use Eclipse IDE to create an Android application and name it as
SendSMSDemo under a package com.example.sendsmsdemo. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Run the application to launch Android emulator and verify the result of
the changes done in the application.
191
Android
Following
is
the
content
of
the
modified
main
src/com.example.sendsmsdemo/MainActivity.java.
activity
file
package com.example.sendsmsdemo;
import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
Button sendBtn;
EditText txtphoneNo;
EditText txtMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});
192
Android
}
protected void sendSMSMessage() {
Log.i("Send SMS", "");
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following will be the content of res/layout/activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
193
Android
<TextView
android:id="@+id/textViewPhoneNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phone_label" />
<EditText
android:id="@+id/editTextPhoneNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone"/>
<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sms_label" />
<EditText
android:id="@+id/editTextSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"/>
<Button android:id="@+id/btnSendSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/send_sms_label"/>
</LinearLayout>
194
Android
<string name="app_name">SendSMSDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="phone_label">Enter Phone Number:</string>
<string name="sms_label">Enter SMS Message:</string>
<string name="send_sms_label">Send SMS</string>
</resources>
Following is the default content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendsmsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sendsmsdemo.MainActivity"
android:label="@string/app_name" >
195
Android
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your SendSMSDemo application. We assume, you have
connected your actual Android Mobile device with your computer. To run the app
from Eclipse, open one of your project's activity files and click Run icon from
the toolbar. Before starting your application, Eclipse will display following
window to select an option where you want to run your Android application.
196
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
Now you can enter a desired mobile number and a text message to be sent on
that number. Finally click on Send SMS button to send your SMS. Make sure
your GSM connection is working fine to deliver your SMS to its recipient.
You can take a number of SMS separated by comma and then inside your
program you will have to parse them into an array string and finally you can use
a loop to send message to all the given numbers. That's how you can write your
own SMS client. Next section will show you how to use existing SMS client to
send SMS.
197
Android
To experiment with this example, you will need actual Mobile device equipped
with latest Android OS, otherwise you will have to struggle with emulator which
may not work.
Step
Description
You will use Eclipse IDE to create an Android application and name it as
SendSMSDemo under a package com.example.sendsmsdemo. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Android
Run the application to launch Android emulator and verify the result of
the changes done in the aplication.
Following
is
the
content
of
the
modified
main
src/com.example.sendsmsdemo/MainActivity.java.
activity
file
package com.example.sendsmsdemo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Android
}
protected void sendSMS() {
Log.i("Send SMS", "");
Android
android:orientation="vertical" >
<Button android:id="@+id/sendSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/compose_sms"/>
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new
constants:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SendSMSDemo</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="compose_sms">Compose SMS</string>
</resources>
Following is the default content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendsmsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
201
Android
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sendsmsdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your SendSMSDemo application. We assume, you have
connected your actual Android Mobile device with your computer. To run the app
from Eclipse, open one of your project's activity files and click Run icon from
the toolbar. Before starting your application, Eclipse will display following
window to select an option where you want to run your Android application.
202
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
203
Android
Now use Compose SMS button to launch Android built-in SMS clients which is
shown below:
You can modify either of the given default fields and finally use send SMS button
(marked with red rectangle) to send your SMS to the mentioned recipient.
204
Android
To experiment with this example, you will need actual Mobile device equipped
with latest Android OS, otherwise you will have to struggle with emulator which
may not work.
205
Android
Step
Description
You will use Eclipse IDE to create an Android application and name it as
PhoneCallDemo under a package com.example.phonecalldemo. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Run the application to launch Android emulator and verify the result of
the changes done in the application.
Following
is
the
content
of
the
modified
main
src/com.example.phonecalldemo/MainActivity.java.
activity
file
package com.example.phonecalldemo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
Android
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void makeCall() {
Log.i("Make call", "");
try {
startActivity(phoneIntent);
finish();
Log.i("Finished making a call...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this,
"Call faild, please try again later.",
Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
207
Android
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following will be the content of res/layout/activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/makeCall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/make_call"/>
</LinearLayout>
Following will be the content of res/values/strings.xml to define two new
constants:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">PhoneCallDemo</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="make_call">Call 91-800-001-0101</string>
</resources>
Following is the default content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.phonecalldemo"
208
Android
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.phonecalldemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your PhoneCallDemo application. We assume, you have
connected your actual Android Mobile device with your computer. To run the app
from Eclipse, open one of your project's activity files and click Run icon from
the toolbar. Before starting your application, Eclipse will display following
window to select an option where you want to run your Android application.
209
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
Now use Call 91-800-001-0101 button to make phone call as shown below:
210
Android
211
Android
Once you have developed and fully tested your Android Application, you can
start selling or distributing free using Google Play (A famous Android
marketplace). You can also release your applications by sending them directly to
users or by letting users download them from your own website.
You can check a detailed publishing process at Android official website, but this
tutorial will take you through simple steps to launch your application on Google
Play. Here is a simplified check list which will help you in launching your Android
application:
Step
Activity
Targeted Regions Google Play lets you control what countries and
territories where your application will be sold. Accordingly you must
take care of setting up time zone, localization or any other specific
requirement as per the targeted region.
Android
213
Android
Next, select Export Android Application option as shown in the above screen
shot and then click Next and again Next so that you get following screen where
you will choose Create new keystore to store your application.
214
Android
Enter your password to protect your application and click on Next button once
again. It will display following screen to let you create a key for your application:
215
Android
Once you filled up all the information, click Next button and finally it will ask you
a location where Application will be exported:
216
Android
Finally, you click on Finish button to generate your Android Application Package
File which will be uploaded at Google Play marketplace.
217
Android
218
Android
Some times in your application, if you wanted to ask the user about taking a
decision between yes or no in response of any particular action taken by the
user, by remaining in the same activity and without changing the screen, you
can use Alert Dialog.
In order to make an alert dialog, you need to make an object of
AlertDialogBuilder which an inner class of AlertDialog. Its syntax is given below:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
Now you have to set the positive (yes) or negative (no) button using the object
of the AlertDialogBuilder class. Its syntax isalertDialogBuilder.setPositiveButton(CharSequence text,
DialogInterface.OnClickListener listener)
alertDialogBuilder.setNegativeButton(CharSequence text,
DialogInterface.OnClickListener listener)
Apart from this, you can use other functions provided by the builder class to
customize the alert dialog. These are listed below:
Sr.No
setIcon(Drawable icon)
This method set the icon of the alert dialog box.
setCancelable(boolean cancelable)
This method sets the property that the dialog can be cancelled or not.
setMessage(CharSequence message)
This method sets the message to be displayed in the alert dialog.
setMultiChoiceItems(CharSequence[]
items,
boolean[]
checkedItems,
DialogInterface.OnMultiChoiceClickListener
listener)
This method sets list of items to be displayed in the dialog as the
content. The selected option will be notified by the listener.
219
Android
setOnCancelListener(DialogInterface.OnCancelListener
onCancelListener)
This method Sets the callback that will be called if the dialog is
canceled.
setTitle(CharSequence title)
This method sets the title that will appear in the dialog.
After creating and setting the dialog builder, you will create an alert dialog by
calling the create() method of the builder class. Its syntax is:
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
This will create the alert dialog and will show it on the screen.
Example:
The following example demonstrates the use of AlertDialog in android. It uses three
different activities to demonstrate it. The dialog asks you to jump to positive activity
or negative activity or cancel it.
To experiment with this example, you need to run this on an emulator or an actual
device.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as AlertDialog under a package com.example.alertdialog. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify
layout
XML
file
of
the
newly
created
activity
220
Android
res/layout/activity_positive.xml
required.
and
add
any
GUI
component
if
Modify
layout
XML
file
of
the
newly
created
activity
res/layout/activity_negative.xml and add any GUI component if
required.
Run the application and choose a running android device and install
the application on it and verify the results.
import com.example.alertdialog.*;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
221
Android
@Override
public void onClick(DialogInterface arg0, int arg1) {
Intent
positveActivity
=
new
Intent(getApplicationContext(),com.example.alertdialog.Positi
veActivity.c
lass);
startActivity(positveActivity);
}
});
alertDialogBuilder.setNegativeButton(R.string.negative_button,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent
negativeActivity
=
new
Intent(getApplicationContext(),com.example.alertdialog.Negati
veActivity.c
lass);
startActivity(negativeActivity);
}
});
@Override
222
Android
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_positive);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.positive, menu);
return true;
}
223
Android
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_negative);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.negative, menu);
return true;
}
}
Here is the modified code of res/layout/activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
224
Android
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="170dp"
android:onClick="open"
android:text="@string/hello_world" />
</RelativeLayout>
Here is the modified code of res/layout/activity_positive.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PositiveActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
225
Android
android:layout_marginTop="20dp"
android:text="@string/positive"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Here is the modified code of res/layout/activity_negative.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".NegativeActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="17dp"
android:text="@string/negative"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Here is the modified code of Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
226
Android
<string name="app_name">AlertDialog</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_positive">PositiveActivity</string>
<string name="title_activity_negative">NegativeActivity</string>
<string name="positive">Positive Activity</string>
<string name="negative">Negative Activity</string>
<string name="decision">Are you sure, you wanted to make this
decision</string>
<string name="positive_button">+ive</string>
<string name="negative_button">-ive</string>
</resources>
Here is the default code of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alertdialog"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.alertdialog.MainActivity"
android:label="@string/app_name" >
227
Android
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Let's try to run your Camera application. We assume, you have connected your
actual Android Mobile device with your computer. To run the app from Eclipse,
open one of your project's activity files and click Run
icon from the toolbar.
Before starting your application, Eclipse will display following window to select
an option where you want to run your Android application.
228
Android
229
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
230
Android
Now just tap the button hello world to see the alert box, which would be
something like this:
231
Android
Now select any of the two buttons and see the respective activity loading up. In
case you select positive button, this screen would appear:
232
Android
Now press back button on your device, and this time select negative from your
alert dialog. The following screen would appear this time:
233
27. ANIMATIONS
Android
Animation in android is possible in many ways. We will discuss one easy and
widely used way of making animation called tweened animation.
Tween Animation
Tween Animation takes some parameters such as start value, end value, size,
time duration, rotation angle etc., and perform the required animation on that
object. It can be applied to any type of object. So in order to use this, android
has povided us a class called Animation.
In order to perform animation in android, we are going to call a static function
loadAnimation() of the class AnimationUtils. We are going to receive the result in
an instance of Animation Object. Its syntax is as follows:
Animation animation =
AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.myanimation);
Note the second parameter. It is the name of the our animation xml file. You
have to create a new folder called anim under res directory and make an xml file
under anim folder.
This animation class has many useful functions which are listed below:
Sr.No
start()
This method starts the animation.
setDuration(long duration)
This method sets the duration of an animation.
getDuration()
This method gets the duration which is set by above method.
end()
This method ends the animation.
234
Android
cancel()
This method cancels the animation.
Zoom in animation
In order to perform a zoom in animation, create an XML file under anim folder
under res directory and put this code in the file.
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.5"
android:toXScale="3.0"
android:fromYScale="0.5"
android:toYScale="3.0"
android:duration="5000"
android:pivotX="50%"
android:pivotY="50%" >
</scale>
</set>
The parameter fromXScale and fromYScale defines the start point and the
parameters toXScale and toYScale defines the end point. The duration defines
the time of animation and the pivotX,pivotY defines the center from where the
animation would start.
Example:
The following example demonstrates the use of Animation in android. You would
be able to choose different type of animation from the menu and the selected
animation will be applied on an imageView on the screen.
To experiment with this example, you need to run this on an emulator or an
actual device.
235
Android
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Animation under a package com.example.animation. While creating
this project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Create a new folder under res directory and call it anim. Confirm it by
visiting res/anim.
Right click on anim and click on new and select Android XML file You
have to create three different files that are listed below.
Modify res/values/string.xml
components.
Run the application and choose a running android device and install
the application on it and verify the results.
file
and
add
necessary
string
import com.example.animation.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
236
Android
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Android
}
return false;
}
}
Here is the modified code of res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
238
Android
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="179dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
Here is the code of res/anim/myanimation.xml.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.5"
android:toXScale="3.0"
android:fromYScale="0.5"
android:toYScale="3.0"
android:duration="5000"
android:pivotX="50%"
android:pivotY="50%" >
</scale>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="5000"
android:fromXScale="3.0"
android:toXScale="0.5"
android:fromYScale="3.0"
android:toYScale="0.5"
android:duration="5000"
android:pivotX="50%"
239
Android
android:pivotY="50%" >
</scale>
</set>
Here is the code of res/anim/clockwise.xml.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" >
</rotate>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="5000"
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" >
</rotate>
</set>
Here is the code of res/anim/fade.xml.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
240
Android
android:interpolator="@android:anim/accelerate_interpolator" >
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000" >
</alpha>
<alpha
android:startOffset="2000"
android:fromAlpha="1"
android:toAlpha="0"
android:duration="2000" >
</alpha>
</set>
Here is the modified code of res/menu/main.xml.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/rotate360"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/rotate_String"/>
<item
android:id="@+id/zoomInOut"
android:orderInCategory="100"
android:title="@string/zoom_In_Out"/>
<item
241
Android
android:id="@+id/fadeInOut"
android:orderInCategory="100"
android:title="@string/fade_String"/>
</menu>
Here is the modified code of res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Animation</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="zoom_In_Out">Zoom In/Out</string>
<string name="rotate_String">Clockwise/Anti Clockwise</string>
<string name="fade_String">Fade In/Out</string>
</resources>
Here is the default code of AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.animation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
242
Android
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.animation.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Let's try to run your Animation application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
243
Android
244
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
245
Android
Now just select the menu from your mobile, and a menu would appear which
would be something like this:
246
Android
Now just select the Zoom in, Zoom out option from menu and an animation
would appear which would be something like this:
247
Android
Now just select the clockwise option from menu and an animation would appear
which would be something like this:
248
Android
Now just select the fade in/out option from menu and an animation would
appear which would be something like this:
Note: If you run it in emulator, you may not experience smooth animation
effect. You have to run it in your android mobile in order to experience the
smooth animation.
249
Android
Android has a built-in microphone through which you can capture audio and
store it, or play it in your phone. There are many ways to do that but the most
common way is through MediaRecorder class.
Android provides MediaRecorder class to record audio or video. To use
MediaRecorder class, you will first create an instance of MediaRecorder class. Its
syntax is given below.
MediaRecorder myAudioRecorder = new MediaRecorder();
Now you will set the source, output and encoding format and output file. Their
syntax is given below.
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
After specifying the audio source and format and its output file, we can then call
the two basic methods perpare and start to start recording the audio.
myAudioRecorder.prepare();
myAudioRecorder.start();
Apart from these methods, there are other methods listed in the MediaRecorder
class that allows you more control over audio and video recording.
Sr.No
setAudioSource()
This method specifies the source of audio to be recorded.
setVideoSource()
This method specifies the source of video to be recorded.
setOutputFormat()
This method specifies the audio format in which audio to be stored.
250
Android
setAudioEncoder()
This method specifies the audio encoder to be used.
setOutputFile()
This method configures the path to the file into which the recorded
audio is to be stored.
stop()
This method stops the recording process.
release()
This method should be called when the recorder instance is needed.
Example:
This example provides demonstration of MediaRecorder class to capture audio and
then MediaPlayer class to play that recorded audio.
To experiment with this example, you need to run this on an actual device.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as AudioCapture under a package com.example.audiocapture. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify res/values/string.xml
components.
Run the application and choose a running android device and install
the application on it and verify the results.
file
and
add
necessary
string
251
Android
import java.io.File;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
stop.setEnabled(false);
play.setEnabled(false);
outputFile = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/myrecording.3gp";;
252
Android
= null;
stop.setEnabled(false);
play.setEnabled(true);
253
Android
}
Here is the content of activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
254
Android
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="@string/Recording"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:scaleType="fitXY"
android:src="@android:drawable/presence_audio_online" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_marginTop="67dp"
android:layout_toLeftOf="@+id/imageView1"
android:onClick="start"
android:text="@string/start" />
<Button
255
Android
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/textView1"
android:layout_marginRight="40dp"
android:onClick="stop"
android:text="@string/stop" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:onClick="play"
android:text="@string/play" />
</RelativeLayout>
Here is the content of Strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AudioCapture</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Recording">Android Audio Recording Application</string>
<string name="start">start</string>
<string name="stop">stop</string>
<string name="play">play</string>
256
Android
</resources>
Here is the content of AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.audiocapture"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.audiocapture.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
257
Android
258
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
259
Android
Now by default you will see that the stop and play buttons are disabled. Just
press the start button and your application will start recording the audio. It will
display the following screen:
260
Android
Now just press stop button and it will save the recorded audio to external sd
card. When you click on stop button, the following screen would appear:
261
Android
Now just press the play button and and recorded audio will just start playing on
the device. The following message appears when you click on play button:
262
Android
You can easily control your ringer volume and ringer profile i.e. (silent, vibrate,
loud etc.) in android. Android provides AudioManager class that provides access
to these controls.
In order to use AndroidManager class, you have to first create an object of
AudioManager class by calling the getSystemService() method. Its syntax is
given below.
private AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Once you instantiate the object of AudioManager class, you can
use setRingerMode method to set the audio or ringer profile of your device. Its
syntax is given below.
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
The method setRingerMode takes an integer number as a parameter. For each
mode, an integer number is assigned that will differentiate between different
modes. The possible modes are.
Sr.No
RINGER_MODE_VIBRATE
This Mode sets the device at vibrate mode.
RINGER_MODE_NORMAL
This Mode sets the device at normal (loud) mode.
RINGER_MODE_SILENT
This Mode sets the device at silent mode.
Once you have set the mode, you can call the getRingerMode() method to get
the set state of the system. Its syntax is given below.
int mod = myAudioManager.getRingerMode();
263
Android
Apart from the getRingerMode method, there are other methods available in the
AudioManager class to control the volume and other modes. They are listed
below:
Sr.No
getMode()
This method returns the current audio mode.
getStreamMaxVolume(int streamType)
This method returns the maximum volume index for a particular
stream.
getStreamVolume(int streamType)
This method returns the current volume index for a particular stream.
isMusicActive()
This method checks whether any music is active.
startBluetoothSco()
This method Starts bluetooth SCO audio connection.
stopBluetoothSco()
This method stops bluetooth SCO audio connection.
Example:
The below example demonstrates the use of AudioManager class. It creates a basic
application that allows you to set different ringer modes for your device.
To experiment with this example, you need to run this on an actual device.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as AudioManager under a package com.example.audiomanager. While
264
Android
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
2
Modify res/values/string.xml
components.
Run the application and choose a running android device and install
the application on it and verify the results.
file
and
add
necessary
string
import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
Android
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Vibrate = (Button)findViewById(R.id.button2);
Ring = (Button)findViewById(R.id.button4);
Silent = (Button)findViewById(R.id.button3);
Mode = (Button)findViewById(R.id.button1);
Status = (TextView)findViewById(R.id.textView2);
myAudioManager =
(AudioManager)getSystemService(Context.AUDIO_SERVICE);
Android
}
else{
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here is the content of activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
267
Android
android:text="@string/audio"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_alignRight="@+id/textView1"
android:onClick="vibrate"
android:text="@string/Vibrate" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="144dp"
android:layout_marginLeft="40dp"
android:layout_toLeftOf="@+id/button2"
android:onClick="silent"
android:text="@string/Silent" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:onClick="ring"
android:text="@string/Ring" />
268
Android
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignLeft="@+id/button3"
android:layout_marginBottom="15dp"
android:onClick="mode"
android:text="@string/Mode" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="@string/Status"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Here is the content of Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AudioManager</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="audio">Set Audio Profiles</string>
<string name="Ring">Ring</string>
<string name="Vibrate">Vibrate</string>
<string name="Silent">Silent</string>
269
Android
</resources>
Here is the content of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.audiomanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.audiomanager.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
270
Android
271
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
272
Android
Now just select the ring button and then press the current mode button to see
that if its status has been set.
273
Android
Now press the silent button and then press the current mode button to see
that if it is set or not. It will display the following screen:
274
Android
Now press the vibrate button and then press the current mode button to see
that if it is set or not. It will display the following screen:
275
30. AUTOCOMPLETE
Android
If you want to get suggestions, when you type in an editable text field, you can
do this via AutoCompleteTextView. It provides suggestions automatically when
the user is typing. The list of suggestions is displayed in a drop down menu from
which the user can choose an item to replace the content of the edit box.
In order to use AutoCompleteTextView you have to first
AutoCompletTextView Field in the xml. Its syntax is given below.
create
an
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:ems="10" >
After that, you have to get a reference of this textview in java. Its syntax is
given below.
private AutoCompleteTextView actv;
actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
Thenext thing you need to do is to specify the list of suggestion items to be
displayed. You can specify the list items as a string array in java or in
strings.xml. Its syntax is given below.
String[] countries = getResources().
getStringArray(R.array.list_of_countries);
ArrayAdapter adapter = new ArrayAdapter
(this,android.R.layout.simple_list_item_1,countries);
actv.setAdapter(adapter);
The array adapter class is responsible for displaying the data as list in the
suggestion box of the text field. The setAdapter method is used to set the
adapter of the autoCompleteTextView. Apart from these methods, the other
methods of AutoCompelte are listed below.
276
Android
Sr.No
getAdapter()
This method returns a filterable list adapter used for auto completion.
getCompletionHint()
This method returns optional hint text displayed at the bottom of the
matching list.
getDropDownAnchor()
This method returns the id for the view that the auto-complete drop
down list is anchored to.
getListSelection()
This method returns the position of the dropdown view selection, if
there is one.
isPopupShowing()
This method indicates whether the popup menu is showing.
showDropDown()
This method displays the drop down on screen.
Example:
The below example demonstrates the use of AutoCompleteTextView class. It
creates a basic application that allows you to type in and it displays suggestions
on your device.
To experiment with this example, you need to run this on an actual device or in
an emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
277
Android
Modify res/values/string.xml
components.
Run the application and choose a running android device and install
the application on it and verify the results.
file
and
add
necessary
string
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
278
Android
setContentView(R.layout.activity_main);
actv = (AutoCompleteTextView)
findViewById(R.id.autoCompleteTextView1);
mactv = (MultiAutoCompleteTextView) findViewById
(R.id.multiAutoCompleteTextView1);
actv.setAdapter(adapter);
mactv.setAdapter(adapter);
mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here is the content of activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
279
Android
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:ems="10" >
<requestFocus />
</AutoCompleteTextView>
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/autoCompleteTextView1"
android:layout_centerVertical="true"
android:ems="10" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
280
Android
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/auto_complete"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/multiAutoCompleteTextView1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="19dp"
android:text="@string/multi_auto_complete"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Here is the content of Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AutoComplete</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="auto_complete">AutoComplete</string>
<string name="multi_auto_complete">Multi AutoComplete</string>
<string-array name="list_of_countries">
<item >USA</item>
<item >Uk</item>
<item >Canada</item>
<item >Australia</item>
<item >France</item>
<item >Italy</item>
<item >China</item>
281
Android
<item >Japan</item>
<item >Spain</item>
</string-array>
</resources>
Here is the content of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.autocomplete"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.autocomplete.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
282
Android
</manifest>
Let's try to run your Androidmanager application. We assume, you have
connected your AVD while doing environment setup. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Ecclipse will install this application in your AVD and your AVD will display
following screen.
283
Android
Now just type in the text view to see suggestions of the country. As we type two
letters which are ca, it shows us suggestion of Canada:
284
Android
285
Android
There are some practices that you can follow while developing android
application. These are suggested by the android itself and they keep on
improving with respect to time.
These best practices include interaction design features, performance, security
and privacy, compatibility, testing, distributing and monetizing tips. They are
narrowed down and are listed as below.
AsyncTask Vs Services.
Both are used for doing background tasks, but the service is not affected by
most user interface life cycle events, so it continues to run in circumstances that
would shut down an AsyncTask.
286
Android
Use internal storage rather than external for storing applications files
Example:
The below example demonstrates some of the best practices you should follow when
developing android application. It creates a basic application that allows you to
specify how to use text fields and how to increase performance by checking the
charging status of the phone.
To experiment with this example, you need to run this on an actual device.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as BestPractices under a package com.example.autocomplete. While
creating this project, make sure you Target SDK and Compile With at
287
Android
Modify res/values/string.xml
components.
Run the application and choose a running android device and install
the application on it and verify the results.
file
and
add
necessary
string
import android.os.BatteryManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
Android
setContentView(R.layout.activity_main);
Check = (Button)findViewById(R.id.button1);
}
if(usbCharge){
Toast.makeText(getApplicationContext(),"Mobile is
charging on USB",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"Mobile is
charging on AC",Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
289
Android
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here is the content of activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="20dp"
android:text="@string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/message"
android:layout_width="wrap_content"
290
Android
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="textCapSentences|textAutoCorrect" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="34dp"
android:ems="10"
android:hint="@string/password_hint"
android:inputType="textPassword" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/message"
android:layout_marginTop="50dp"
android:text="@string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
291
Android
android:layout_below="@+id/password"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:onClick="check"
android:text="@string/check" />
</RelativeLayout>
Here is the content of Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">BestPractices</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="username">Username</string>
<string name="password">Password</string>
<string name="password_hint">Hello world!</string>
<string name="check">Charging check</string>
</resources>
Here is the content of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bestpractices"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
292
Android
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.bestpractices.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Let's try to run your BestPractices application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
293
Android
294
Android
Select your mobile device as an option and then check your mobile device which
will display following screen.
295
Android
Now jsut type on the username field and you will see the built in android
suggestions from the dictionary will start coming up. This is shown below:
296
Android
Now you will see the hint in the password field. It would disappear as soon as
you start writing in the field. It is shown below.
297
Android
In the end, just connect your device to AC cable or USB cable and press on
charging check button. In our case, we connect it with a PC via USB cable so it
shows the following message:
298
32. BLUETOOTH
Android
Among many ways, Bluetooth is a way to send or receive data between two
different devices. Android platform includes support for the Bluetooth framework
that allows a device to wirelessly exchange data with other Bluetooth devices.
Android provides Bluetooth API to perform these different operations.
ACTION_REQUEST_DISCOVERABLE
This constant is used to turn on discovering of Bluetooth.
ACTION_STATE_CHANGED
This constant will notify that Bluetooth state has been changed.
ACTION_FOUND
This constant is used for receiving information about each device that
is discovered.
Once you enable the Bluetooth, you can get a list of paired devices by calling
getBondedDevices() method. It returns a set of bluetooth devices. Its syntax is.
299
Android
private Set<BluetoothDevice>pairedDevices;
pairedDevices = BA.getBondedDevices();
Apart form the pairedDevices, there are other methods in the API that gives
more control over Bluetooth. They are listed below.
Sr.No
enable()
This method enables the adapter if not enabled.
isEnabled()
This method returns true if adapter is enabled.
disable()
This method disables the adapter.
getName()
This method returns the name of the Bluetooth adapter.
setName(String name)
This method changes the Bluetooth name.
getState()
This method returns the current state of the Bluetooth Adapter.
startDiscovery()
This method starts the discovery process of the Bluetooth for 120
seconds.
Example:
This example provides demonstration of BluetoothAdapter class to manipulate
Bluetooth and show list of paired devices by the Bluetooth.
To experiment with this example, you need to run this on an actual device.
300
Android
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as AudioCapture under a package com.example.audiocapture. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify res/values/string.xml
components.
Run the application and choose a running android device and install
the application on it and verify the results.
file
and
add
necessary
string
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
301
Android
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
lv = (ListView)findViewById(R.id.listView1);
BA = BluetoothAdapter.getDefaultAdapter();
}
Android
Toast.LENGTH_LONG).show();
}
}
public void list(View view){
pairedDevices = BA.getBondedDevices();
}
public void off(View view){
BA.disable();
Toast.makeText(getApplicationContext(),"Turned off" ,
Toast.LENGTH_LONG).show();
}
public void visible(View view){
Intent getVisible = new Intent(BluetoothAdapter.
ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
303
Android
}
Here is the content of activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
304
Android
android:text="@string/app_name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="on"
android:text="@string/on" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="visible"
android:text="@string/Visible" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="list"
android:text="@string/List" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="off"
android:text="@string/off" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
305
Android
android:layout_height="wrap_content"
android:visibility="visible" >
</ListView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Here is the content of Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Bluetooth</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="on">Turn On</string>
<string name="off">Turn Off</string>
<string name="Visible">Get Visible</string>
<string name="List">List Devices</string>
</resources>
Here is the content of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetooth"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
306
Android
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.bluetooth.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Let's try to run your AndroidCapture application. We assume, you have
connected your actual Android Mobile device with your computer. To run the app
from Eclipse, open one of your project's activity files and click Run icon from
the toolbar. Before starting your application, Eclipse will display following
window to select an option where you want to run your Android application.
307
Android
308
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
309
Android
Now select Turn On to turn on the bluetooth. But as you select it, your
Bluetooth will not be turned on. In fact, it will ask your permission to enable the
Bluetooth.
310
Android
Now just select the Get Visible button to turn on your visibility. The following
screen would appear asking your permission to turn on discovery for 120
seconds.
311
Android
Now just select the List Devices option. It will list down the paired devices in the
list view. In our case, we have only one paired device. It is shown below:
312
Android
Now just select the Turn off button to switch off the Bluetooth. Following
message would appear when you switch off the bluetooth indicating the
successful switching off of Bluetooth.
313
33. CAMERA
Android
These are the following two ways, in which you can use camera in your
application
ACTION_IMAGE_CAPTURE_SECURE
It returns the image captured from the camera, when the device is
secured.
ACTION_VIDEO_CAPTURE
It calls the existing video application in android to capture video.
EXTRA_SCREEN_ORIENTATION
It is used to set the orientation of the screen to vertical or landscape.
EXTRA_FULL_SCREEN
It is used to control the user interface of the ViewImage.
INTENT_ACTION_VIDEO_CAMERA
This intent is used to launch the camera in the video mode.
EXTRA_SIZE_LIMIT
314
Android
Now you will use the function startActivityForResult() to launch this activity and
wait for its result. Its syntax is given below:
startActivityForResult(intent,0)
This method has been defined in the activity class. We are calling it from main
activity. There are methods defined in the activity class that does the same job,
but used when you are not calling from the activity but from somewhere else.
They are listed below:
Sr.No
No matter which function you used to launch the activity, they all return the
result. The result can be obtained by overriding the function onActivityResult
315
Android
Example:
Here is an example that shows how to launch the exisitng camera application to
capture an image and display the result in the form of bitmap.
To experiment with this example, you need to run this on an actual device on which
camera is supported.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Camera under a package com.example.camera. While creating this
project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Run the application and choose a running android device and install
the application on it and verify the results.
Following
is
the
content
of
the
modified
src/com.example.camera/MainActivity.java.
main
activity
file
package com.example.camera;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
316
Android
ImageView imgFavorite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgFavorite = (ImageView)findViewById(R.id.imageView1);
imgFavorite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
open();
}
});
}
public void open(){
Intent intent = new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Bitmap bp = (Bitmap) data.getExtras().get("data");
imgFavorite.setImageBitmap(bp);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
317
Android
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following will be the content of res/layout/activity_main.xml file:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="34dp"
android:layout_marginTop="36dp"
android:contentDescription="@string/hello_world"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/imageView1"
android:text="@string/tap"
318
Android
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Following will be the content of res/values/strings.xml to define one new
constants
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Camera</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="tap">Tap the image to open the camera!!</string>
</resources>
Following is the default content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.camera"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.camera.MainActivity"
android:label="@string/app_name" >
<intent-filter>
319
Android
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your Camera application. We assume, you have connected your
actual Android Mobile device with your computer. To run the app from Eclipse,
open one of your project's activity files and click Run
icon from the toolbar.
Before starting your application, Eclipse will display following window to select
an option where you want to run your Android application.
320
Android
Select your mobile device as an option and then check your mobile device which
will display the following screen:
321
Android
Now just tap on the image of android icon and the camera will be opened. Just
capture a picture. After capturing it, two buttons will appear asking you to
discard it or to keep it:
322
Android
Just press the tic (green) button and you will be brought back to your application
with the captured image in place of android icon.
323
Android
getNumberOfCameras()
It returns an integer number defining of cameras availaible on device.
lock()
It is used to lock the camera, so no other application can access it.
release()
It is used to release the lock on camera, so other applications can
access it.
open(int cameraId)
It is used to open particular camera when multiple cameras are
supported.
enableShutterSound(boolean enabled)
It is used to enable/disable default shutter sound of image capture.
Now you need to make a seperate class and extend it with SurfaceView and
implement SurfaceHolder interface.
The two classes that have been used have the following purpose:
Class
Description
Camera
SurfaceView
You have to call the preview method of the camera class to start the preview of
the camera to the user.
public
class
ShowCamera
SurfaceHolder.Callback {
extends
SurfaceView
implements
324
Android
startFaceDetection()
This function starts the face detection in the camera.
stopFaceDetection()
It is used to stop the face detection which is enabled by the above
function.
startSmoothZoom(int value)
It takes an integer value and zoom the camera very smoothly to that
value.
stopSmoothZoom()
It is used to stop the zoom of the camera.
stopPreview()
It is used to stop the preview of the camera to the user.
takePicture(Camera.ShutterCallback
shutter,
325
Android
Example:
Following example demonstrates the usage of the camera API in the application.
To experiment with this example, you will need actual Mobile device equipped with
latest Android OS, because camera is not supported by the emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Camera under a package com.example.camera1. While creating
this project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Run the application and choose a running android device and install
the application on it and verify the results.
Following
is
the
content
of
the
modified
src/com.example.camera1/MainActivity.java.
main
activity
file
package com.example.camera1;
326
Android
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
@Override
public void onPictureTaken(byte[] data, Camera camera) {
327
Android
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pic = (ImageView)findViewById(R.id.imageView1);
cameraObject = isCameraAvailiable();
showCamera = new ShowCamera(this, cameraObject);
FrameLayout preview = (FrameLayout)
findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
public void snapIt(View view){
cameraObject.takePicture(null, null, capturedIt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
328
Android
}
}
Create
the
new
java
file
called
as
src/com.example.camera1/ShowCamera.java. and add the following code:
package com.example.camera1;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int
arg3) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try
{
329
Android
theCamera.setPreviewDisplay(holder);
theCamera.startPreview();
} catch (IOException e) {
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
}
Modify the content of the res/layout/activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.30"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="199dp" />
<Button
android:id="@+id/button_capture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
330
Android
android:onClick="snapIt"
android:text="@string/Capture" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />
</LinearLayout>
< /LinearLayout>
Modify the content of the res/values/string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Camera1</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Capture">Capture</string>
</resources>
Modify the content of the AndroidManifest.xml and
permissions as shown below.
add
the
necessary
<uses-sdk
android:minSdkVersion="8"
331
Android
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.camera1.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your SendSMSDemo application. We assume, you have
connected your actual Android Mobile device with your computer. To run the app
from Eclipse, open one of your project's activity files and click Run icon from
the toolbar. Before starting your application, Eclipse will display following
window to select an option where you want to run your Android application.
332
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
333
Android
The camera would start showing its preview in the upper half panel. Just click
the capture button. You can now either store the captured image, upload it to
the web or either discard it.
334
34. CLIPBOARD
Android
Android provides the clipboard framework for copying and pasting different types
of data. The data could be text, images, binary stream data or other complex
data types.
Android provides the library of ClipboardManager and ClipData and ClipData.item
to use the copying and pasting framework. To use clipboard framework, you
need to put data into clip object, and then put that object into system wide
clipboard.
In order to use clipboard, you need to instantiate an object of ClipboardManager
by calling the getSystemService() method. Its syntax is given below:
ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
Copying data
The next thing you need to do is to instantiate the ClipData object by calling the
respective type of data method of the ClipData class. In case of text data,
the newPlainText method will be called. After that you have to set that data as
the clip of the Clipbaoard Manager object. Its syntax is given below:
ClipData myClip;
String text = "hello world";
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
The ClipData object can take these three form and following functions are used
to create those forms.
Sr.No
Text
newPlainText(label, text)
Returns a ClipData object whose single ClipData.Item object contains
a text string.
URI
335
Android
Intent
newIntent(label, intent)
Returns a ClipData object whose single ClipData.Item object contains
an Intent.
Pasting data
In order to paste the data, we will first get the clip by calling
the getPrimaryClip() method. And from that click we will get the item in
ClipData.Item object. And from the object we will get the data. Its syntax is
given below:
ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString();
Apart from these methods, there are other methods provided by the
ClipboardManager class for managing clipboard framework. These methods are
listed below:
Sr.No
getPrimaryClip()
This method just returns the current primary clip on the clipboard.
getPrimaryClipDescription()
This method returns a description of the current primary clip on the
clipboard but not a copy of its data.
hasPrimaryClip()
This method returns true if there is currently a primary clip on the
clipboard.
setPrimaryClip(ClipData clip)
336
Android
setText(CharSequence text)
This method can be directly used to copy text into the clipboard.
getText()
This method can be directly used to get the copied text from the
clipboard.
Example:
Here is an example demonstrating the use of ClipboardManager class. It creates a
basic copy paste application that allows you to copy the text and then paste it via
clipboard.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Clipboard under a package com.example.clipboard. While creating
this project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results
res/layout/activity_main
to
add
Following
is
the
content
of
the
modified
src/com.example.clipboard/MainActivity.java.
respective
main
activity
XML
file
package com.example.clipboard;
337
Android
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
@SuppressLint("NewApi")
public void copy(View view){
String text = copyField.getText().toString();
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
Toast.makeText(getApplicationContext(), "Text Copied",
338
Android
Toast.LENGTH_SHORT).show();
}
@SuppressLint("NewApi")
public void paste(View view){
ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString();
pasteField.setText(text);
Toast.makeText(getApplicationContext(), "Text Pasted",
Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
339
Android
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="19dp"
android:text="@string/copytext"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="20dp"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_centerVertical="true"
android:text="@string/pastetext"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
340
Android
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="65dp"
android:layout_marginTop="20dp"
android:onClick="copy"
android:text="@string/copy" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="39dp"
android:ems="10" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/editText2"
android:layout_marginTop="34dp"
android:onClick="paste"
android:text="@string/paste" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
341
Android
<string name="app_name">Clipboard</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="copy">Copy Text</string>
<string name="paste">Paste Text</string>
<string name="copytext">Text to copy</string>
<string name="pastetext">Copied Text</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.clipboard"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.clipboard.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Android
</activity>
</application>
</manifest>
Let's try to run our Clipboard application we just modified. We assume, you had
created your AVD while doing environment setup. To run the app from Eclipse,
open one of your project's activity files and click Run
icon from the toolbar.
Eclipse installs the app on your AVD and starts it and if everything is fine with
your setup and application, it will display following Emulator window:
343
Android
Now just enter any text in the Text to copy field and then select the copy text
button. The following notification will be displayed which is shown below:
344
Android
Now just press the paste button, and you will see the text which is copied is now
pasted in the field of Copied Text. It is shown below:
345
Android
In android, you can define your own custom fonts for the strings in your
application. You just need to download the required font from the internet, and
then place it in assets/fonts folder.
After putting fonts in the assets folder under fonts folder, you can access it in
your java code through Typeface class. First, get the reference of the text view
in the code. Its syntax is given below:
TextView tx = (TextView)findViewById(R.id.textview1);
The next thing you need to do is to call static method of Typeface
class createFromAsset() to get your custom font from assets. Its syntax is
given below:
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font
name.ttf");
The last thing you need to do is to set this custom font object to your TextView
Typeface property. You need to call setTypeface() method to do that. Its
syntax is given below:
tx.setTypeface(custom_font);
Apart from these Methods, there are other methods defined in the Typeface
class, that you can use to handle Fonts more effectively.
Sr.No
createFromFile(String path)
Creates a new Typeface from the specified font file.
346
Android
defaultFromStyle(int style)
Returns one of the default Typeface objects, based on the specified
style.
getStyle()
Returns the Typeface's intrinsic style attributes.
Example:
Here is an example demonstrating the use of Typeface to handle CustomFont. It
creates a basic application that displays a custom font that you have specified in
the fonts file.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as CustomFonts under a package com.example.customfonts. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
Following
is
the
content
of
the
modified
src/com.example.customfonts/MainActivity.java.
add
respective
main
activity
XML
file
347
Android
package com.example.customfonts;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tx = (TextView)findViewById(R.id.hello);
Typeface custom_font = Typeface.createFromAsset(getAssets(),
"fonts/Erika Type.ttf");
tx.setTypeface(custom_font);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
348
Android
<TextView
android:id="@+id/hello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="70dip"
android:text="@string/hello_world" />
</LinearLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">CustomFonts</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.customfonts"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
349
Android
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.customfonts.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
350
Android
As you can see that the text that appeared on the AVD does not have a default
android font, rather it has the custom font that you have specified in the fonts
folder.
Note: You need to take care of the size and the character supported by the font,
when using custom fonts.
351
Android
Android allows you to backup your application data to remote "cloud" storage, in
order to provide a restore point for the application data and settings. You can
only backup your application data. To access the other applications data, you
need to root your phone.
In order to make a data backup application, you need to register your
application with google backup service. This has been explained in the example.
After registering, you have to specify its key in the AndroidManifest.XML
<application
android:allowBackup="true"
android:backupAgent="MyBackupPlace">
<meta-data
android:name="com.google.android.backup.api_key"
android:value="AEdPqrEAAAAIErlxFByGgNz2ywBeQb6TsmLpp5Ksh1PW-ZSexg"
/>
</application>
Android provides BackUpAgentHelper class to handle all the operations of data
backup. To use this class, you have to extend your class with it. Its syntax is
given below:
public class MyBackUpPlace extends BackupAgentHelper {
}
The persistent data that you want to backup is in either of the two forms. Either
it could be SharedPreferences or it could be File. Android supports both types of
backup
in
the
respective
classes
of SharedPreferencesBackupHelper and FileBackupHelper.
In order to use SharedPerefernceBackupHelper, you need to instantiate its
object with the name of your sharedPreferences File. Its syntax is given below:
static final String File_Name_Of_Preferences = "myPreferences";
SharedPreferencesBackupHelper helper = new
SharedPreferencesBackupHelper(this, File_Name_Of_Preferences);
352
Android
The last thing you need to do is to call addHelper method by specifying the
backup key string, and the helper object. Its syntax is given below:
addHelper(PREFS_BACKUP_KEY, helper);
The addHelper method will automatically add a helper to a given data subset to
the agent's configuration.
Apart from these methods, there are other
BackupAgentHelper class. They are defined below:
methods
Sr.No
onBackup(ParcelFileDescriptor oldState,
data, ParcelFileDescriptor newState)
defined
in
the
BackupDataOutput
onRestore(BackupDataInput
data,
ParcelFileDescriptor newState)
int
appVersionCode,
performBackup(ParcelFileDescriptor
oldState,
BackupDataOutput data, ParcelFileDescriptor newState)
Backs up the configured SharedPreferences groups.
restoreEntity(BackupDataInputStream data)
Restores one entity from the restore data stream to its proper shared
preferences file store.
Example:
The following example demonstrates the use of BackupAgentHelper class to create
backup of your application data.
To experiment with this example, you need to run this on an actual device or in an
emulator.
353
Android
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Backup under a package com.example.backup. While creating this
project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
class
with
the
name
you
specify
at
Register you android application with google backup service. To do that, visit this
link. You must agree to the terms of service, and then enter the application
package name. It is shown below:
Then click on Register with android backup service. It would give you your key,
along with your AndroidManifest code to copy. Just copy the key. It is shown
below:
Once you copy the key, you need to write it in your AndroidManifest.XML file. Its
code is given below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
354
Android
package="com.example.backup"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:backupAgent="MyBackUpPlace"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.backup.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</application>
</manifest>
Here is the code of BackUpAgentHelper class. The name of the class should be
the same as you specified in the backupAgent tag under application in
AndroidManifest.XML
355
Android
package com.example.backup;
import android.app.backup.BackupAgentHelper;
import android.app.backup.SharedPreferencesBackupHelper;
@Override
public void onCreate() {
SharedPreferencesBackupHelper helper = new
SharedPreferencesBackupHelper(this,
File_Name_Of_Prefrences);
addHelper(PREFS_BACKUP_KEY, helper);
}
356
Android
Performing backup
For testing purposes, you can also make a request with the following bmgr
command:
adb shell bmgr backup your.package.name
Initiate a backup operation by typing the following command.
adb shell bmgr run
This forces the Backup Manager to perform all backup requests that are in its
queue.
357
Android
The android developer tools lets you create interactive and powerful application
for android platform. The tools can be generally categorized into two types.
SDK tools
Platform tools
SDK tools
SDK tools are generally platform independent and are required no matter which
android platform you are working on. When you install the Android SDK into
your system, these tools get automatically installed. The list of SDK tools has
been given below:
Sr.No
android
This tool lets you manage
components of the SDK.
AVDs,
projects,
and
the
installed
ddms
This tool lets you debug Android applications.
Draw 9-Patch
This tool allows you to easily create a NinePatch graphic using a
WYSIWYG editor.
emulator
This tools let you test your applications without using a physical
device.
mksdcard
Helps you create a disk image (external sdcard storage) that you can
use with the emulator.
proguard
Shrinks, optimizes, and obfuscates your code by removing unused
358
Android
code.
7
sqlite3
Lets you access the SQLite data files created and used by Android
applications.
traceview
Provides a graphical viewer for execution logs saved by your
application.
We will discuss three important tools here that are android, ddms and sqlite3.
Android
Android is a development tool that lets you perform these tasks:
DDMS
DDMS stands for Dalvik Debug Monitor Server that provides many services on
the device. The service could include message formation, call spoofing, capturing
screenshot, exploring internal threads and file systems etc.
Running DDMS
From eclipse click on Window, Open Perspective, Other ... DDMS. Or simply
look on the left most top corner and click on ddms.
How it works
In android, each application runs in its own process and each process run in the
virtual machine. Each VM exposes a unique port that a debugger can attach to.
When DDMS starts, it connects to adb. When a device is connected, a VM
monitoring service is created between adb and DDMS, which notifies DDMS
when a VM on the device is started or terminated.
359
Android
Using DDMS
You can use DDMS for many tasks. For example, here we are using it to make
sms, make call, and capture screenshot.
Making SMS
In the DDMS, select the Emulator Control tab. In the emulator control tab, click
on SMS and start typing the SMS and then the incoming number. It is shown in
the picture below.
Now click on send button, and you will see an sms notification in the emulator
window. It is shown below:
360
Android
Making Call
In the DDMS, select the Emulator Control tab. In the emulator control tab, click
on voice and then start typing the incoming number. It is shown in the picture
below:
361
Android
Now click on the call button to make a call to your emulator. It is shown below:
362
Android
Capturing ScreenShot
You can also capture screenshot of your emulator. For this look for the camera
icon on the right side under Devices tab. Just point your mouse over it and
select it.
As soon as you select it, it will start the screen capturing process and will
capture whatever screen of the emulator is currently active. It is shown below:
363
Android
The eclipse orientation can be changed using Ctrl + F11 key. Now you can save
the image or rotate it and then select done to exit the screen capture dialog.
Sqlite3
Sqlite3 is a command line program which is used to manage the SQLite
databases created by Android applications. The tool also allow us to execute the
SQL statements on the fly.
There are two way through which you can use SQLite, either from remote shell
or you can use locally.
364
Android
Platform tools
The platform tools are customized to support the features of the latest android
platform.
The platform tools are typically updated every time you install a new SDK
platform. Each update of the platform tools is backward compatible with older
platforms.
Some of the platform tools are listed below:
365
38. EMULATOR
Android
Emulator lets you emulate the real device with all its functionalities without
purchasing the real device. Android emulator lets you emulate different android
configurations by creating android virtual devices.
We are going to explore different functionalities in the emulator that are present
in the real android device in this chapter.
Creating AVD
If you want to emulate a real device, first create an AVD with the same device
configurations as real device, then launch this AVD from AVD manager.
Creating Snapshots
Creating snapshots mean saving an emulator state to a file that enables the
emulator to be started quickly the next time you try to launch it. One of the
biggest advantage of creating snapshots is that it saves the boot up time.
In order to create snapshot, check mark the option of snapshot while creating
your AVD. It is shown below:
The first time you launch the emulator, it will take the usual time of loading. But
when you close it and start it again, you will see a considerable amount of time
reduction in appearing of emulator.
Changing Orientation
Usually by default when you launch the emulator, its orientation is vertical, but
you can change its orientation by pressing Ctrl+F11 key from keyboard.
First launch the emulator. It is shown in the picture below:
366
Android
367
Android
Emulator Commands.
Apart from just orientation commands, there are other very useful commands of
emulator that you should keep in mind while using emulator. They are listed
below:
Sr.No
Home
Shifts to main screen
F2
Toggles context sensitive menu
F3
Brings out call log
F4
End call
F5
Search
F6
Toggle trackball mode
F7
Power button
F8
Toggle data network
Ctrl+F5
Ring Volume up
10
Ctrl+F6
368
Android
You will see that you are now connected to your emulator. Now type this
command to send message.
sms send +1234567 your sms goes here
Once you type this command, hit enter. Now look at the AVD. You will receive a
notification displaying that you got a new text message. It is shown below:
369
Android
370
Android
Browse through the explorer and make new folder, view existing contents etc.
etc.
371
Android
Android allows your application to connect to Facebook and share data or any
kind of updates on Facebook. This chapter is about integrating Facebook into
your application.
There are two ways through which you can integrate Facebook and share
something from your application. These ways are listed below:
Facebook SDK
Intent Share
-keystore
"your
path"
Once you enter it, you will be prompted for password. Give android as the
password and then copy the key that is given to you. It is shown in the image
below:
372
Android
Now move to the native android app section and fill in your project and class
name and paste the hash that you copied in step 1. It is shown below:
If everything works fine, you will receive an application ID with the secret. Just
copy the application id and save it somewhere. It is shown in the image below:
373
Android
call(Session
session,
SessionState
state,
Exception
{
if (session.isOpened()) {
// make request to;2 the /me API
Request.executeMeRequestAsync(session, new Request.
GraphUserCallback() {
Intent share
Intent share is used to share data between applications. In this strategy, we will
not handle the SDK stuff, but let the Facebook application handle it. We will
simply call the facebook application and pass the data to share. This way, we
can share something on Facebook.
Android provides intent library to share data between activities and applications.
In order to use it as share intent, we have to specify the type of the share intent
to ACTION_SEND. Its syntax is given below:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
374
Android
Next thing you need is to define the type of data to pass, and then pass the
data. Its syntax is given below:
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, from tutorialspoint");
startActivity(Intent.createChooser(shareIntent, "Share your thoughts"));
Apart from these methods, there are other methods available that allows intent
handling. They are listed below:
Sr.No
addCategory(String category)
This method adds a new category to the intent.
getAction()
This method retrieve the general action to be performed, such as
ACTION_VIEW.
getCategories()
This method returns the set of all categories in the intent.nt and the
current scaling event.
toString()
This method returns a string containing a concise, human-readable
description of this object.
Example:
Here is an example demonstrating the use of IntentShare to share data on
Facebook. It creates a basic application that allows you to share some text on
Facebook.
375
Android
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as IntentShare under a package com.example.intentshare. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
add
Following
is
the
content
of
the
modified
src/com.example.intentshare/MainActivity.java.
respective
main
activity
XML
file
package com.example.intentshare;
import java.io.File;
import java.io.FileOutputStream;
import com.example.intentshare.R;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
376
Android
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void open(View view){
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, from
tutorialspoint");
startActivity(Intent.createChooser(shareIntent, "Share your
thoughts"));
}
377
Android
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="98dp"
android:layout_marginTop="139dp"
android:onClick="open"
android:src="@drawable/tp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="@string/tap"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
378
Android
<string name="app_name">IntentShare</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="tap">Tap the button to share something</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentshare"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.intentshare.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Android
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your IntentShare application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which
will display your default screen:
380
Android
Now just tap on the image logo and you will see a list of share providers.
381
Android
Now just select Facebook from that list and then write any message. It is shown
in the image below:
382
Android
Now just select the post button and then it would be posted on your wall. It is
shown below:
383
40. GESTURES
Android
Android provides special types of touch screen events such as pinch, double tap,
scrolls, long presses and flinch. These are all known as gestures.
Android provides GestureDetector class to receive motion events and tell us that
these events correspond to gestures or not. To use it, you need to create an
object
of
GestureDetector
and
then
extend
another
class
with
GestureDetector.SimpleOnGestureListener to act as a listener and override
some methods. Its syntax is given below:
GestureDetector myG;
myG = new GestureDetector(this,new Gesture());
384
Android
ScaleGestureDetector SGD;
SGD = new ScaleGestureDetector(this,new ScaleListener());
The first parameter is the context and the second parameter is the event
listener. We have to define the event listener and override a
function OnTouchEvent to make it working. Its syntax is given below:
public boolean onTouchEvent(MotionEvent ev) {
SGD.onTouchEvent(ev);
return true;
}
private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float scale = detector.getScaleFactor();
return true;
}
}
Apart from the pinch gestures, there are other methods available that notify
more about touch events. They are listed below:
Sr.No
getEventTime()
This method gets the event time of the current event being processed.
getFocusX()
This method gets the X coordinate of the current gesture's focal point.
getFocusY()
This method gets the Y coordinate of the current gesture's focal point.
getTimeDelta()
This method returns the time difference in milliseconds between the
previous accepted scaling event and the current scaling event.
385
Android
isInProgress()
This method returns true if a scale gesture is in progress.
onTouchEvent(MotionEvent event)
This method accepts MotionEvents and dispatches events when
appropriate.
Example:
Here is an example demonstrating the use of ScaleGestureDetector class. It creates
a basic application that allows you to zoom in and out through pinch.
To experiment with this example, you can run this on an actual device or in an
emulator with touch screen enabled.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Gestures under a package com.example.gestures. While creating
this project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
add
Following
is
the
content
of
the
modified
src/com.example.gestures/MainActivity.java.
respective
main
activity
XML
file
package com.example.gestures;
import android.app.Activity;
import android.graphics.Matrix;
386
Android
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;
@Override
public boolean onTouchEvent(MotionEvent ev) {
SGD.onTouchEvent(ev);
return true;
}
Android
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/imageView1"
388
Android
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textView1"
android:scaleType="matrix"
android:src="@android:drawable/sym_def_app_icon" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Gestures</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Pinch to zoom in or out!</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gestures"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
389
Android
<activity
android:name="com.example.gestures.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Let's try to run your Gestures application. We assume, you have connected your
actual Android Mobile device with your computer. To run the app from Eclipse,
open one of your project's activity files and click Run
icon from the toolbar.
Before starting your application, Eclipse will display following window to select
an option where you want to run your Android application.
390
Android
391
Android
Select your mobile device as an option and then check your mobile device which
will display your default screen:
392
Android
Now just place two fingers over android screen, and separate them apart and
you will see that the android image is zooming. It is shown in the image below:
393
Android
Now again place two fingers over android screen, and try to close them and you
will see that the android image is now shrinking. It is shown in the image below:
394
Android
Android allows us to integrate google maps in our application. You can show any
location on the map, or can show different routes on the map etc. etc. You can
also customize the map according to your choices.
Android
<uses-permission
/>
android:name="android.permission.ACCESS_NETWORK_STATE"
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDKymeBXNeiFWY5jRUejv6zItpmr2MVyQ0" />
Adding Marker
You can place a marker with some text over it displaying your location on the
map. It can be done by via addMarker() method. Its syntax is given below:
final LatLng TutorialsPoint = new LatLng(21, 57);
Marker TP = googleMap.addMarker(new
MarkerOptions().position(TutorialsPoint).title("TutorialsPoint"));
Android
Enable/Disable zoom
You can also enable or disable the zoom gestures in the map by calling the
setZoomControlsEnabled(boolean) method. Its syntax is given below:
googleMap.getUiSettings().setZoomGesturesEnabled(true);
Apart from these customization, there are other methods available in the
GoogleMap class that helps you more to customize the map. They are listed
below:
Sr.No
addCircle(CircleOptions options)
This method adds a circle to the map.
addPolygon(PolygonOptions options)
This method adds a polygon to the map.
addTileOverlay(TileOverlayOptions options)
This method adds tile overlay to the map.
animateCamera(CameraUpdate update)
This method moves the map according to the update with an
animation.
clear()
This method removes everything from the map.
getMyLocation()
This method returns the currently displayed user location.
moveCamera(CameraUpdate update)
This method repositions the camera according to the instructions
defined in the update.
setTrafficEnabled(boolean enabled)
This method toggles the traffic layer on or off.
397
Android
snapshot(GoogleMap.SnapshotReadyCallback callback)
This method takes a snapshot of the map.
10
stopAnimation()
This method stops the camera animation if there is one in progress
Example:
Here is an example demonstrating the use of GoogleMap class. It creates a basic M
application that allows you to navigate through the map.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as GoogleMaps under a package com.example.googlemaps. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
add
respective
XML
Android
399
Android
400
Android
Copy the SHA1 key because you need it in the next step.
Now again go to the left tab and select API access. And click on create new
android key. Now paste the key that you copied and put a semicolon and paste
your project name and click create. It would be like this.
Now copy the API key that has been given to your by android, because you have
to paste it into your manifest file.
Android
android:value="API_KEY"/>
In the second line replace API_KEY with your api key and you are done. You
need to add some permissions in your manifest too, which are given below in
the manifest file.
main
activity
file
package com.example.googlemaps;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Toast;
Android
} catch (Exception e) {
e.printStackTrace();
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission
403
Android
android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<permission
android:name="com.example.googlemaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.
gsf.permission.
READ_GSERVICES" />
<uses-permission android:name="android.permission.
WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.
ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
404
Android
<activity
android:name="com.example.googlemaps.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDKymeBXNeiFWY5jRUejv6zItpmr2MVyQ0" />
</application>
</manifest>
405
Android
Let's try to run your GoogleMaps application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
406
Android
Now what you need to do is to tap on the balloon to see the text.
Now you can customize the google map according to your choice with the
functions given in the GoogleMap API.
407
Android
abmp = (BitmapDrawable)img.getDrawable();
408
Android
This method copy this bitmap's pixels into the new bitmap.
2
createBitmap(DisplayMetrics
Bitmap.Config config)
display, int
createBitmap(Bitmap src)
Returns an immutable bitmap from the source bitmap.
extractAlpha()
Returns a new bitmap that captures the alpha values of the original.
getConfig()
This method returns config, otherwise returns null.
getDensity()
Returns the density for this bitmap
getRowBytes()
Return the number of bytes between rows in the bitmap's pixels.
10
setDensity(int density)
This method specifies the density for this bitmap
Example:
The below example demonstrates some of the image effects on the bitmap. It
creates a basic application that allows you to convert the picture into grayscale and
much more.
409
Android
To experiment with this example, you need to run this on an actual device.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as ImageEffects under a package com.example.imageeffects. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
Following
is
the
content
of
the
modified
src/com.example.imageeffects/MainActivity.java.
add
respective
main
activity
XML
file
package com.example.imageeffects;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
Android
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.imageView1);
BitmapDrawable
abmp = (BitmapDrawable)img.getDrawable();
bmp = abmp.getBitmap();
r = (int) red * r;
g = (int) green * g;
b = (int) blue * b;
Android
}
img.setImageBitmap(operation);
}
r = 100
r;
g = 100
+ g;
b = 100
+ b;
Android
r =
r - 50;
g =
g - 50;
b =
b - 50;
}
}
img.setImageBitmap(operation);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
413
Android
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="19dp"
android:onClick="dark"
android:text="@string/dark" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="87dp"
android:layout_marginRight="17dp"
android:layout_toLeftOf="@+id/button3"
android:onClick="gray"
android:text="@string/gray" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
414
Android
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_centerHorizontal="true"
android:onClick="bright"
android:text="@string/bright" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="114dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ImageEffects</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="gray">Gray</string>
<string name="bright">bright</string>
<string name="dark">dark</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
415
Android
package="com.example.imageeffects"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.imageeffects.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
416
Android
Let's try to run our Image Effects application we just modified. We assume, you
had created your AVD while doing environment setup. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Eclipse installs the app on your AVD and starts it and if everything is
fine with your setup and application, it will display following Emulator window:
417
Android
Now if you will look at your device screen, you will see an image of android
along with three buttons.
Now just select the gray button that will convert your image into grayscale and
will update the UI. It is shown below:
418
Android
Now tap on the bright button, that will add some value to each pixel of the
image and thus makes an illusion of brightness. It is shown below:
419
Android
Now tap on the dark button, that will subtract some value to each pixel of the
image and thus makes an illusion of dark. It is shown below:
420
Android
Sometimes you don't want an image to appear abruptly on the screen, rather
you want to apply some kind of animation to the image when it transitions from
one image to another. This is supported by android in the form of
ImageSwitcher.
An image switcher allows you to add some transitions on the images through the
way they appear on screen. In order to use image Switcher, you need to define
its XML component first. Its syntax is given below:
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ImageSwitcher>
Now we create an instance of ImageSwithcer in java file and get a reference of
this XML component. Its sytnax is given below:
private ImageSwitcher imageSwitcher;
imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
The next thing we need to do is implement the ViewFactory interface and
implement unimplemented method that returns an imageView. Its syntax is
below:
imageSwitcher.setImageResource(R.drawable.ic_launcher);
imageSwitcher.setFactory(new ViewFactory() {
public View makeView() {
ImageView myView = new ImageView(getApplicationContext());
return myView;
}
}
The last thing you need to do is to add Animation to the ImageSwitcher. You
need to define an object of Animation class through AnimationUtilities class by
calling a static method loadAnimation. Its syntax is given below:
421
Android
Animation in =
AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
The method setInAnimaton sets the animation of the appearance of the object
on the screen whereas setOutAnimation does the opposite. The method
loadAnimation() creates an animation object.
Apart from these methods, there are other
ImageSwitcher class. They are defined below:
methods
Sr.No
setImageDrawable(Drawable drawable)
defined
in
the
Sets an image with image switcher. The image is passed in the form of
bitmap.
2
setImageResource(int resid)
Sets an image with image switcher. The image is passed in the form of
integer id.
setImageURI(Uri uri)
Sets an image with image switcher. The image is passed in the form of
URI.
422
Android
Example:
The below example demonstrates some of the image switcher effects on the
bitmap. It creates a basic application that allows you to view the animation
effects on the images.
To experiment with this example, you need to run this on an actual device.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as ImageSwitcher under a package com.example.imageswitcher.
While creating this project, make sure you Target SDK and Compile
With at the latest version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
add
respective
Following
is
the
content
of
the
modified
main
src/com.example.imageswithcer/MainActivity.java.
activity
XML
file
package com.example.imageswitcher;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
423
Android
import android.widget.ViewSwitcher.ViewFactory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageButton)findViewById(R.id.imageButton1);
imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
imageSwitcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView myView = new ImageView(getApplicationContext());
myView.setScaleType(ImageView.ScaleType.FIT_CENTER);
myView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.
FILL_PARENT,LayoutParams.FILL_PARENT));
return myView;
}
});
Android
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
425
Android
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:onClick="next"
android:src="@android:drawable/ic_menu_send" />
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ImageSwitcher>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="85dp"
android:onClick="previous"
android:src="@android:drawable/ic_menu_revert" />
426
Android
</RelativeLayout>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.imageswitcher.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
427
Android
Let's try to run our Image Switcher application we just modified. We assume,
you had created your AVD while doing environment setup. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Eclipse installs the app on your AVD and starts it and if everything is
fine with your setup and application, it will display following Emulator window:
428
Android
Now if you will look at your device screen, you will see the two buttons.
Now just select the upper button with right arrow. An image would appear from
right and move towards left. It is shown below:
429
Android
Now tap on the below button, that will bring back the previous image with some
transition. It is shown below:
430
Android
Android provides many kinds of storage for applications to store their data. The
storage places are shared preferences, internal and external storage, SQLite
storage, and storage via network connection.
We are going to look at the internal storage in this chapter. Internal storge is the
storage of the private data on the device memory.
By default these files are private and are accessed by only your application and
get deleted, when user delete your application.
Writing file
In order to use internal storage to write some data in the file, call the
openFileOutput() method with the name of the file and the mode. The mode
could be private, public etc. Its syntax is given below:
FileOutputStream fOut = openFileOutput("file name
here",MODE_WORLD_READABLE);
The method openFileOutput() returns an instance of FileOutputStream. So you
receive it in the object of FileInputStream. After that you can call write method
to write data on the file. Its syntax is given below:
String str = "data";
fOut.write(str.getBytes());
fOut.close();
Reading file
In order to read from the file you just created, call the openFileInput() method
with the name of the file. It returns an instance of FileInputStream. Its syntax is
given below:
FileInputStream fin = openFileInput(file);
After that, you can call read method to read one character at a time from the file
and then you can print it. Its syntax is given below:
int c;
String temp="";
while( (c = fin.read()) != -1){
431
Android
getChannel()
This method returns a write-only FileChannel that shares its position
with this stream.
getFD()
This method returns the underlying file descriptor.
Apart from the methods of read and close, there are other methods provided by
the FileInputStream class for better reading files. These methods are listed
below:
Sr.No
available()
This method returns an estimated number of bytes that can be read or
skipped without blocking for more input.
getChannel()
This method returns a read-only FileChannel that shares its position
432
Android
getFD()
This method returns the underlying file descriptor.
Example:
Here is an example demonstrating the use of internal storage to store and read
files. It creates a basic storage application that allows you to read and write
from internal storage.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Storage under a package com.example.storage. While creating this
project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install the
application on it and verify the results.
res/layout/activity_main
to
add
Following
is
the
content
of
the
modified
src/com.example.storage/MainActivity.java.
respective
main
activity
XML
file
433
Android
package com.example.storage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
Android
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read(View view){
try{
FileInputStream fin = openFileInput(file);
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
et.setText(temp);
Toast.makeText(getBaseContext(),"file read",
Toast.LENGTH_SHORT).show();
}catch(Exception e){
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
435
Android
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="182dp"
android:onClick="save"
android:text="@string/save" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="46dp"
android:onClick="read"
android:text="@string/read" />
<EditText
436
Android
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Storage</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="save">save to internal storage</string>
<string name="read">load from internal storage</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.storage"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
437
Android
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.storage.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
438
Android
Let's try to run our Storage application we just modified. We assume, you had
created your AVD while doing environment setup. To run the app from Eclipse,
open one of your project's activity files and click Run
icon from the toolbar.
Eclipse installs the app on your AVD and starts it and if everything is fine with
your setup and application, it will display following Emulator window:
439
Android
Now what you need to do is to enter any text in the field. For example, we have
entered some text. Press the save button. The following notification would
appear in you AVD:
440
Android
Now when you press the load button, the application will read the file, and
display the data. In our case, following data would be returned:
Note, you can actually view this file by switching to DDMS tab. In DDMS, select
file explorer and navigate this path.
data>data>com.example.storage>files>mydata
This has also been shown in the image below.
441
45. JETPLAYER
Android
The Android platform includes a JET engine that lets you add interactive
playback of JET audio content in your applications. Android provides JetPlayer
class to handle this stuff.
In order to Jet Content, you need to use the JetCreator tool that comes with
AndroidSDK. The usage of jetCreator has been discussed in the example. In
order to play the content created by JetCreator, you need JetPlayer class
supported by android.
In order to use JetPlayer, you need to instantiate an object of JetPlayer class. Its
syntax is given below:
JetPlayer jetPlayer = JetPlayer.getJetPlayer();
The next thing you need to do is to call loadJetFile method and pass in the path
of your Jet file. After that you have to add this into the Queue of JetPlayer. Its
syntax is given below:
jetPlayer.loadJetFile("/sdcard/level1.jet");
byte segmentId = 0;
// queue segment 5, repeat once, use General MIDI, transpose by -1 octave
jetPlayer.queueJetSegment(5, -1, 1, -1, 0, segmentId++);
The method queueJetSegment Queues the specified segment in the JET Queue.
The last thing you need is to call the play method to start playing the music. Its
syntax is given below:
jetPlayer.play();
Apart from these methods, there are other methods defined in the JetPlayer
class. They are defined below:
Sr.No
clearQueue()
Empties the segment queue, and clears all clips that are scheduled for
playback.
closeJetFile()
442
Android
getJetPlayer()
Factory method for the JetPlayer class.
loadJetFile(String path)
Loads a .jet file from a given path.
pause()
Pauses the playback of the JET segment queue.
release()
Stops the current JET playback, and releases all associated native
resources.
Example:
The following example demonstrates the use of JetCreator tool to create Jet content.
Once that content is created, you can play it through JetPlayer.
To experiment with this example, you need to run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as JetPlayer under a package com.example.jetplayer. While creating
this project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
443
Android
Using JetCreator
Installing python
The first step that you need while using JetCreator is to install the python. The
python can be installed from its official website https://www.python.org/ or
from anywhere else on the internet.
Please keep in mind the version number of the python should either be 2.6 or
2.7 because this example follows that.
Once you download python, install it. After installing you have to set path to the
python. Open your command prompt and type the following command. It is
shown in the image below:
Once path is set, you can verify it by typing python and hit enter. It is shown
below:
Installing WxPython
The next thing you need to do is to install the wxPython. It can be
downloaded here. Once downloaded, you will install it. It will be automatically
installed in the python directory.
Running JetCreator
The next thing you need is to move to the path where JetCreator is present. It is
in the tools, SDK folder of the android. It is shown below:
444
Android
As soon as you hit enter, Jet Creator window will open. It would be something
like this.
Creating JetContent
In the above Jet Window, click on the import button. And select
JetCreator_demo_1 or 2 from the JetFolder from the demo content folder in the
Jet folder. It is shown in the image below:
445
Android
Once you import the content, you will see the content in the JetCreator window.
It is shown below:
Now you can explore different options of JetCreator by visiting the JetCreator
link http://developer.android.com/guide/topics/media/jet/jetcreator_manual.htm
l. Finally in order to create .jet file, you need to save the content from the file
menu.
Verifying Results
Once you got the jet file, you can play it using jet player. The main code of
playing it has been given below:
446
Android
447
Android
448
Android
JSON - Elements
A JSON file consist of many components. Here is the table defining the
compoents of a JSON file and their description:
Sr.No
Array([)
In a JSON file, square bracket ([) represents a JSON array.
Objects({)
In a JSON file, curly bracket ({) represents a JSON object.
Key
A JSON object contains a key that is just a string. Pairs of key/value
make up a JSON object.
Value
Each key has a value that could be string, integer or double etc.
JSON - Parsing
For parsing a JSON object, we will create an object of class JSONObject and
specify a string containing JSON data to it. Its syntax is:
String in;
JSONObject reader = new JSONObject(in);
The last step is to parse the JSON. A JSON file consist of different object with
different key/value pair etc. So JSONObject has a separate function for parsing
each of the component of JSON file. Its syntax is given below:
JSONObject sys
= reader.getJSONObject("sys");
country = sys.getString("country");
JSONObject main
= reader.getJSONObject("main");
temperature = main.getString("temp");
The
method getJSONObject returns
the
JSON
method getString returns the string value of the specified key.
object.
The
449
Android
Apart from these methods, there are other methods provided by this class for
better parsing JSON files. These methods are listed below:
Sr.No
get(String name)
This method just Returns the value but in the form of Object type.
getBoolean(String name)
This method returns the boolean value specified by the key.
getDouble(String name)
This method returns the double value specified by the key.
getInt(String name)
This method returns the integer value specified by the key.
getLong(String name)
This method returns the long value specified by the key.
length()
This method returns the number of name/value mappings in this
object.
names()
This method returns an array containing the string names in this
object.
Example:
Here is an example demonstrating the use of JSONObject class. It creates a
basic Weather application that allows you to parse JSON from google weather
api and shows the result.
To experiment with this example, you can run this on an actual device or in an
emulator.
450
Android
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as JSONParser under a package com.example.jsonparser. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
Following
is
the
content
of
the
modified
src/com.example.jsonparser/MainActivity.java.
add
respective
main
activity
XML
file
package com.example.jsonparser;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
Android
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items
//to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
while(obj.parsingComplete);
country.setText(obj.getCountry());
temperature.setText(obj.getTemperature());
humidity.setText(obj.getHumidity());
pressure.setText(obj.getPressure());
452
Android
}
}
Following is the content of src/com.example.jsonparser/HandleXML.java.
package com.example.jsonparser;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.annotation.SuppressLint;
Android
@SuppressLint("NewApi")
public void readAndParseJSON(String in) {
try {
JSONObject reader = new JSONObject(in);
JSONObject sys
= reader.getJSONObject("sys");
country = sys.getString("country");
JSONObject main
= reader.getJSONObject("main");
temperature = main.getString("temp");
pressure = main.getString("pressure");
humidity = main.getString("humidity");
parsingComplete = false;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
454
Android
}
public void fetchJSON(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
readAndParseJSON(data);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new
455
Android
java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="@string/location"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:ems="10" />
456
Android
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="68dp"
android:text="@string/country"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginTop="19dp"
android:text="@string/temperature"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="32dp"
android:text="@string/humidity"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
457
Android
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="21dp"
android:text="@string/pressure"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/editText2"
android:ems="10" />
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView5"
android:layout_alignLeft="@+id/editText1"
android:ems="10" />
458
Android
<EditText
android:id="@+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_alignRight="@+id/editText4"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText1"
android:onClick="open"
android:text="@string/weather" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">JSONParser</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="location">Location</string>
<string name="country">Country:</string>
<string name="temperature">Temperature:</string>
<string name="humidity">Humidity:</string>
<string name="pressure">Pressure:</string>
<string name="weather">Weather</string>
</resources>
459
Android
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.jsonparser.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
460
Android
Let's try to run our JSONParser application we just modified. We assume, you
had created your AVD while doing environment setup. To run the app from
Eclipse, open one of your project's activity files and click Run . icon from the
toolbar. Eclipse installs the app on your AVD and starts it and if everything is
fine with your setup and application, it will display following Emulator window:
461
Android
Now what you need to do is to enter any location in the location field. For
example, we have entered newyork. Press the weather button, when you enter
the location. The following screen would appear in you AVD:
Now when you press the weather button, the application will contact the Google
Weather API and will request for your necessary JSON location file and will parse
it. In case of newyork following file would be returned:
London Temperature from google weather api
Note that this temperature is in kelvin, so if you want to convert it into more
understandable format, you have to convert it into Celsius.
462
Android
Android allows your application to connect to LinkedIn and share data or any
kind of updates on LinkedIn. This chapter is about integrating LinkedIn into your
application.
There are two ways through which you can integrate LinkedIn and share
something from your application. These ways are listed below.
Intent Share
Now fill in your application name, description and your website url. It is shown
below:
463
Android
If everything works fine, you will receive an API key with the secret. Just copy
the API key and save it somewhere. It is shown in the image below:
Intent share
Intent share is used to share data between applications. In this strategy, we will
not handle the SDK stuff, but let the LinkedIn application handle it. We will
simply call the LinkedIn application and pass the data to share. This way, we can
share something on LinkedIn.
Android provides intent library to share data between activities and applications.
In order to use it as share intent, we have to specify the type of the share intent
to ACTION_SEND. Its syntax is given below:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Next thing you need is to define the type of data to pass, and then pass the
data. Its syntax is given below:
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, from tutorialspoint");
startActivity(Intent.createChooser(shareIntent, "Share your thoughts"));
Apart from these methods, there are other methods available that allows intent
handling. They are listed below:
464
Android
Sr.No
addCategory(String category)
This method adds a new category to the intent.
getAction()
This method retrieve the general action to be performed, such as
ACTION_VIEW.
getCategories()
This method returns the set of all categories in the intent.nt and the
current scaling event.
toString()
This method returns a string containing a concise, human-readable
description of this object.
Example:
Here is an example demonstrating the use of IntentShare to share data on
LinkedIn. It creates a basic application that allows you to share some text on
LinkedIn.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as IntentShare under a package com.example.intentshare. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
465
Android
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
add
Following
is
the
content
of
the
modified
src/com.example.intentshare/MainActivity.java.
respective
main
activity
XML
file
package com.example.intentshare;
import java.io.File;
import java.io.FileOutputStream;
import com.example.intentshare.R;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
Android
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.imageView1);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void open(View view){
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, from
tutorialspoint");
startActivity(Intent.createChooser(shareIntent, "Share your
thoughts"));
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
467
Android
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="98dp"
android:layout_marginTop="139dp"
android:onClick="open"
android:src="@drawable/tp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="@string/tap"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">IntentShare</string>
468
Android
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="tap">Tap the button to share something</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentshare"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.intentshare.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
469
Android
Let's try to run your IntentShare application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
470
Android
Select your mobile device as an option and then check your mobile device which
will display your default screen:
471
Android
Now just tap on the image logo and you will see a list of share providers.
472
Android
Now just select LinkedIn from that list and then write any message. It is shown
in the image below:
Now, select the arrow button and then it would be posted on your LinkedIn
page. It is shown below:
473
Android
You can show progress of a task in android through loading progress bar. The
progress bar comes in two shapes. Loading bar and Loading Spinner. In this
chapter we will discuss spinner.
Spinner is used to display progress of those tasks whose total time of completion
is unknown. In order to use that, you just need to define it in the xml like this.
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
After defining it in xml, you have to get its reference in java file through
ProgressBar class. Its syntax is given below:
private ProgressBar spinner;
spinner = (ProgressBar)findViewById(R.id.progressBar1);
After that you can make it disappear, and bring it back when needed through
setVisibility Method. Its syntax is given below:
spinner.setVisibility(View.GONE);
spinner.setVisibility(View.VISIBLE);
Apart from these Methods, there are other methods defined in the ProgressBar
class, that you can use to handle spinner more effectively.
Sr.No
isIndeterminate()
Indicate whether this progress bar is in indeterminate mode.
postInvalidate()
Cause an invalidate to happen on a subsequent cycle through the
event loop.
474
Android
setIndeterminate(boolean indeterminate)
Change the indeterminate mode for this progress bar.
invalidateDrawable(Drawable dr)
Invalidates the specified Drawable.
incrementSecondaryProgressBy(int diff)
Increase the progress bar's secondary progress by the specified
amount.
getProgressDrawable()
Get the drawable used, to draw the progress bar in progress mode.
Example:
Here is an example demonstrating the use of ProgressBar to handle spinner. It
creates a basic application that allows you to turn on the spinner on clicking the
button.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Spinner under a package com.example.spinner. While creating this
project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
add
respective
XML
475
Android
Following
is
the
content
of
the
modified
src/com.example.spinner/MainActivity.java.
main
activity
file
package com.example.spinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
}
Following is the modified content of the xml res/layout/activity_main.xml.
476
Android
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="103dp"
android:onClick="load"
android:text="@string/hello_world" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true" />
</RelativeLayout>
477
Android
<string name="app_name">Spinner</string>
<string name="action_settings">Settings</string>
<string name="hello_world">load spinner</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.spinner"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.spinner.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Android
</activity>
</application>
</manifest>
Let's try to run our Loading Spinner application we just modified. We assume,
you had created your AVD while doing environment setup. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Eclipse installs the app on your AVD and starts it and if everything is
fine with your setup and application, it will display following Emulator window:
Now click on the load spinner button to turn on the loading spinner. It is shown
in the image below:
479
Android
480
49. LOCALIZATION
Android
Localizing Strings
In order to localize the strings used in your application, make a new folder
under res with name of values-local where local would be the replaced with the
region.
For example, in the case of italy, the values-it folder would be made under res.
It is shown in the image below:
Once that folder is made, copy the strings.xml from default folder to the folder
you have created and change its contents. For example, we have changed the
value of hello_world string.
Italy, res/values-it/strings.xml
<;?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Ciao mondo!</string>
</resources>
481
Android
Spanish, res/values-it/strings.xml
<;?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Hola Mundo!</string>
</resources>
French, res/values-it/strings.xml
<;?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Bonjour le monde !</string>
</resources>
Apart from these languages, the region code of other languages have been given
in the table below:
Sr.No
Afrikanns
Code: af. Folder name: values-af
Arabic
Code: ar. Folder name: values-ar
Bengali
Code: bn. Folder name: values-bn
Czech
Code: cs. Folder name: values-cs
Chinese
Code: zh. Folder name: values-zh
German
Code: de. Folder name: values-de
482
Android
French
Code: fr. Folder name: values-fr
Japanese
Code: ja. Folder name: values-ja
Example:
Here is an example demonstrating the use of localization of strings. It creates a
basic application that allows you to customize your application according to US
and Italy region.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Locals under a package com.example.locals. While creating this
project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify the
components.
Create the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
res/values-it/string.xml
to
to
add
add
Following
is
the
content
of
the
modified
src/com.example.locals/MainActivity.java.
respective
necessary
main
XML
string
activity
file
package com.example.locals;
483
Android
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
484
Android
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="174dp"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Locals</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
Following is the content of the res/values-it/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Locals</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Ciao mondo!</string>
</resources>
Following is the content of AndroidManifest.xml file.
485
Android
486
Android
Let's try to run our Localization application we just modified. We assume, you
had created yourAVD while doing environment setup. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Eclipse installs the app on your AVD and starts it and if everything is
fine with your setup and application, it will display following Emulator window:
487
Android
488
Android
Android
Example:
Here is an example demonstrating a login application. It creates a basic
application that gives you only three attempts to login to an application.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as LoginScreen under a package com.example.loginscreen. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify the
components.
res/layout/activity_main
to
add
respective
XML
490
Android
Run the application and choose a running android device and install
the application on it and verify the results.
Following
is
the
content
of
the
modified
src/com.example.loginscreen/MainActivity.java.
main
activity
file
package com.example.loginscreen;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
private EditText
username=null;
private EditText
password=null;
Android
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
492
Android
Android
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_marginLeft="32dp"
android:layout_toRightOf="@+id/textView2"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="38dp"
android:text="@string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
494
Android
android:layout_centerHorizontal="true"
android:layout_marginTop="94dp"
android:onClick="login"
android:text="@string/Login" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginLeft="30dp"
android:layout_marginTop="48dp"
android:text="@string/attempts"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_alignTop="@+id/textView4"
android:text="TextView" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">LoginScreen</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Login Screen</string>
<string name="username">Username:</string>
495
Android
<string name="password">Password:</string>
<string name="Login">Login:</string>
<string name="attempts">Attempts Left:</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.loginscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.loginscreen.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
496
Android
Let's try to run our Login application we just modified. We assume, you had
created your AVD while doing environment setup. To run the app from Eclipse,
open one of your project's activity files and click Run icon from the toolbar.
Eclipse installs the app on your AVD and starts it and if everything is fine with
your setup and application, it will display following Emulator window:
497
Android
Type anything in the username and password field except admin, and then press
the login button. We put admin in the username field and nimda in the password
field. We got failed attempt. This is shown below:
498
Android
Do this two more time, and you will see that you have 0 login attempts left and
your login button is disabled.
499
Android
Now open the application again, and this time enter correct username as admin
and password as admin and click on login. You will be successfully logged in.
500
Android
isPlaying()
This method just returns true/false indicating the song is playing or
not.
seekTo(positon)
This method takes an integer, and move song to that particular
501
Android
second.
3
getCurrentDuration()
This method returns the current position of song in milliseconds.
getDuration()
This method returns the total time duration of song in milliseconds.
reset()
This method resets the media player.
release()
This method releases any resource attached with MediaPlayer object.
setDataSource(FileDescriptor fd)
This method sets the data source of audio/video file.
selectTrack(int
index)
This method takes an integer, and select the track from the list on
that particular index.
10
getTrackInfo()
This method returns an array of track information.
Example:
Here is an example demonstrating the use of MediaPlayer class. It creates a
basic media player that allows you to forward, backward, play and pause a song.
To experiment with this example, you need to run this on an actual device to
hear the audio sound.
502
Android
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as MediaPlayer under a package com.example.mediaplayer. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify the
components.
Create a new folder under MediaPlayer with name as raw and place an
mp3 music file in it with name as song.mp3.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
add
Following
is
the
content
of
the
modified
src/com.example.mediaplayer/MainActivity.java.
respective
main
activity
XML
file
package com.example.mediaplayer;
import java.util.concurrent.TimeUnit;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SeekBar;
503
Android
import android.widget.TextView;
import android.widget.Toast;
Android
Toast.LENGTH_SHORT).show();
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
if(oneTimeOnly == 0){
seekbar.setMax((int) finalTime);
oneTimeOnly = 1;
}
Android
toMinutes((long) startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(this, 100);
}
};
public void pause(View view){
Toast.makeText(getApplicationContext(), "Pausing sound",
Toast.LENGTH_SHORT).show();
mediaPlayer.pause();
pauseButton.setEnabled(false);
playButton.setEnabled(true);
}
public void forward(View view){
int temp = (int)startTime;
if((temp+forwardTime)<=finalTime){
startTime = startTime + forwardTime;
mediaPlayer.seekTo((int) startTime);
}
else{
Toast.makeText(getApplicationContext(),
"Cannot jump forward 5 seconds",
Toast.LENGTH_SHORT).show();
}
}
public void rewind(View view){
int temp = (int)startTime;
if((temp-backwardTime)>0){
startTime = startTime - backwardTime;
mediaPlayer.seekTo((int) startTime);
}
else{
506
Android
Toast.makeText(getApplicationContext(),
"Cannot jump backward 5 seconds",
Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
507
Android
android:layout_alignParentLeft="true"
android:layout_marginBottom="14dp"
android:onClick="forward"
android:src="@android:drawable/ic_media_ff" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageButton2"
android:layout_marginLeft="22dp"
android:layout_toRightOf="@+id/imageButton2"
android:onClick="rewind"
android:src="@android:drawable/ic_media_rew" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton1"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/imageButton1"
android:onClick="pause"
android:src="@android:drawable/ic_media_pause" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton3"
android:layout_marginLeft="24dp"
android:layout_toRightOf="@+id/imageButton3"
android:onClick="play"
508
Android
android:src="@android:drawable/ic_media_play" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/imageButton3"
android:layout_toLeftOf="@+id/textView2"
android:layout_toRightOf="@+id/textView1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/imageButton3"
android:layout_alignTop="@+id/seekBar1"
android:text="@string/inital_Time"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton4"
android:layout_alignTop="@+id/seekBar1"
android:text="@string/inital_Time"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton3"
android:text="@string/hello_world"
509
Android
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView3"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_toRightOf="@+id/imageButton1"
android:text="TextView" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MediaPlayer</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Now Playing:</string>
<string name="inital_Time">0 min, 0 sec</string>
</resources>
Following is the content of AndroidManifest.xml file.
510
Android
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mediaplayer.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
511
Android
Let's try to run your MediaPlayer application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
512
Android
Select your mobile device as an option and then check your mobile device which
will display your default screen:
513
Android
By default you would see the pause button disabled. Now press play button to
disable it, and it would enable the pause button. It is shown in the picture
below:
514
Android
Uptill now, the music has been playing. Now press the pause button and see the
pause notification. This is shown below:
515
Android
Now when you press the play button again, the song will not play from the
beginning but from where it was paused. Now press the fast forward or
backward button to jump the song forward or backward to 5 seconds. A time
would come when the song cannot be jumped forward. At this point, the
notification would appear which would be something like this:
Your music would remain playing in the background while you are doing other
tasks in your mobile. In order to stop it, you have to exit this application from
background activities.
516
52. MULTITOUCH
Android
Multi-touch gesture happens when more than one finger touches the screen at
the same time. Android allows us to detect these gestures.
Android system generates the following touch events whenever multiple fingers
touches the screen at the same time.
Sr.No
ACTION_DOWN
For the first pointer that touches the screen. This starts the gesture.
ACTION_POINTER_DOWN
For extra pointers that enter the screen beyond the first.
ACTION_MOVE
A change has happened during a press gesture.
ACTION_POINTER_UP
Sent when a non-primary pointer goes up.
ACTION_UP
Sent when the last pointer leaves the screen.
So in order to detect any of the above mentioned event, you need to override
onTouchEvent() method and check these events manually. Its syntax is given
below:
public boolean onTouchEvent(MotionEvent ev){
final int actionPeformed = ev.getAction();
switch(actionPeformed){
case MotionEvent.ACTION_DOWN:{
break;
}
517
Android
case MotionEvent.ACTION_MOVE:{
break;
}
return true;
}
In these cases, you can perform any calculation you like. For example zooming,
shrinking etc. In order to get the co-ordinates of the X and Y axis, you can
call getX() and getY()method. Its syntax is given below:
final float x = ev.getX();
final float y = ev.getY();
Apart from these methods, there are other methods provided by this
MotionEvent class for better dealing with multitouch. These methods are listed
below:
Sr.No
getAction()
This method returns the kind of action being performed.
getPressure()
This method returns the current pressure of this event for the first
index.
getRawX()
This method returns the original raw X coordinate of this event.
getRawY()
This method returns the original raw Y coordinate of this event.
getSize()
This method returns the size for the first pointer index.
getSource()
This method gets the source of the event.
518
Android
getXPrecision()
This method returns the precision of the X coordinates being reported.
getYPrecision()
This method returns the precision of the Y coordinates being reported.
Example:
Here is an example demonstrating the use of Multitouch. It creates a basic
Multitouch gesture application that allows you to view the co-ordinates when
multitouch is performed.
To experiment with this example, you need to run this on an actual device.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Multitouch under a package com.example.multitouch. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
Following
is
the
content
of
the
modified
src/com.example.multitouch/MainActivity.java.
add
respective
main
activity
XML
file
package com.example.multitouch;
import android.app.Activity;
import android.os.Bundle;
519
Android
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xText = (EditText)findViewById(R.id.editText2);
yText = (EditText)findViewById(R.id.editText3);
moveX = (EditText)findViewById(R.id.editText1);
moveY = (EditText)findViewById(R.id.editText4);
}
@Override
public boolean onTouchEvent(MotionEvent ev){
final int actionPeformed = ev.getAction();
switch(actionPeformed){
case MotionEvent.ACTION_DOWN:{
final float x = ev.getX();
final float y = ev.getY();
lastXAxis = x;
lastYAxis = y;
xText.setText(Float.toString(lastXAxis));
yText.setText(Float.toString(lastYAxis));
break;
520
Android
}
case MotionEvent.ACTION_MOVE:{
final float x = ev.getX();
final float y = ev.getY();
final float dx = x - lastXAxis;
final float dy = y - lastYAxis;
xAxis += dx;
yAxis += dy;
moveX.setText(Float.toString(xAxis));
moveY.setText(Float.toString(yAxis));
break;
}
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
521
Android
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:ems="10" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="25dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:ems="10" >
</EditText>
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
522
Android
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:ems="10" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText3"
android:layout_alignParentLeft="true"
android:text="@string/xaxis"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText3"
android:layout_alignRight="@+id/textView2"
android:text="@string/yaxis"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText4"
android:layout_alignLeft="@+id/textView3"
android:text="@string/MoveX"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
523
Android
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText4"
android:layout_alignBottom="@+id/editText4"
android:layout_alignRight="@+id/textView4"
android:text="@string/MoveY"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="109dp"
android:text="@string/perform"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Gestures</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Pinch to zoom in or out!</string>
<string name="xaxis">X-Axis</string>
<string name="yaxis">Y-Axis</string>
<string name="MoveX">Move X</string>
<string name="MoveY">Move Y</string>
<string name="perform">Touch here</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
524
Android
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.multitouch"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.multitouch.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
525
Android
Let's try to run your Multitouch application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
526
Android
Select your mobile device as an option and then check your mobile device which
will display your default screen:
527
Android
By default you will see nothing in any field. Now just tap on the Touch here area
and see some data in the fields. It is shown below:
528
Android
You will see that the data in the Move field is 0, because only a single touch
gesture has been performed. Now tap on the screen and start dragging your
finger. You will see the change in the data of the move field. It is shown below:
529
53. NAVIGATION
Android
We will see how you can provide navigation forward and backward between an
application in this chapter. We will first look at how to provide up navigation in
an application.
Providing Up Navigation
The up navigation will allow our application to move to previous activity from the
next activity. It can be done like this.
To implement Up navigation, the first step is to declare which activity is the
appropriate
parent
for
each
activity.
You
can
do
it
by
specifying parentActivityName attibute in an activity. Its syntax is given
below:
android:parentActivityName="com.example.test.MainActivity"
After
that
you
need
to
call setDisplayHomeAsUpEnabled method
of getActionBar() in the onCreate method of the activity. This will enable the
back button in the top action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
The last thing you need to do is to override onOptionsItemSelected method.
When
the
user
presses
it,
your
activity
receives
a
call
to
onOptionsItemSelected(). The ID for the action is android.R.id.home. Its
syntax is given below:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
}
Android
It
can
be
done
by
overriding onBackPressed and
calling moveTaskToBack and finish method. Its syntax is given below:
then
@Override
public void onBackPressed() {
moveTaskToBack(true);
MainActivity2.this.finish();
}
Apart from this setDisplayHomeAsUpEnabled method, there are other methods
available in ActionBar API class. They are listed below:
Sr.No
getSelectedTab()
This method returns the currently selected tab if in tabbed navigation
mode and at least one tab is present there. If there is none than it
returns null.
hide()
This method hides the ActionBar if it is currently showing.
removeAllTabs()
This method remove all tabs from the action bar and deselects the
current tab.
selectTab(ActionBar.Tab tab)
This method selects the specified tab.
Example:
The below example demonstrates the use of Navigation. It creates a basic
application that allows you to navigate within your application.
To experiment with this example, you need to run this on an actual device or in
an emulator.
531
Android
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as test under a package com.example.test. While creating this project,
make sure you Target SDK and Compile With at the latest version of
Android SDK to use higher levels of APIs.
Create a new activity with the name of MainActivity2 and edit it to add
activity code.
Modify res/values/string.xml
components.
Run the application and choose a running android device and install
the application on it and verify the results.
file
and
add
necessary
string
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
532
Android
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Here is the content of src/com.example.test/MainActivity2.java.
package com.example.test;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
533
Android
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
moveTaskToBack(true);
MainActivity2.this.finish();
}
}
Here is the content of activity_main.xml.
534
Android
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="87dp"
android:text="@string/test1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="activity2"
android:text="@string/go2" />
</RelativeLayout>
Here is the content of activity_main_activity2.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
535
Android
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity2" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="125dp"
android:text="@string/test2"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Here is the content of Strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">test</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="test1">This is activity 1</string>
<string name="test2">This is activity 2</string>
<string name="go1">Go to activity 1</string>
<string name="go2">Go to activity 2</string>
<string name="title_activity_main_activity2">MainActivity2</string>
536
Android
</resources>
Here is the content of AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Android
</manifest>
Let's try to run your Navigation application. We assume, you had created
your AVD while doing environment setup. To run the app from Eclipse, open one
of your project's activity files and click Run
icon from the toolbar. Eclipse
installs the app on your AVD and starts it and if everything is fine with your
setup and application, it will display following Emulator window:
538
Android
Now just press the go to activity2 button and the following screen will be shown
to you.
539
Android
Now at the top right corner, you will see the back button. Just press the back
button and you will be brought to the first activity.
540
Android
Now again go to activity2 and this time press the device exit button. You will see
your application will be closed. It is shown in the image below:
541
Android
Android lets your application connect to the internet or any other local network
and allows you to perform network operations.
A device can have various types of network connections. This chapter focuses on
using either a Wi-Fi or a mobile network connection.
State
Connecting
542
Android
Disconnected
Disconnecting
Suspended
Unknown
543
Android
Sr.No
disconnect()
This method releases this connection so that its resources may be
either reused or closed.
getRequestMethod()
This method returns the request method which will be used to make
the request to the remote HTTP server.
getResponseCode()
This method returns response code returned by the remote HTTP
server.
setRequestMethod(String method)
This method Sets the request command which will be sent to the
remote HTTP server.
usingProxy()
This method returns whether this connection uses a proxy server or
not.
Example:
The below example demonstrates the use of HttpURLConnection class. It creates
a basic application that allows you to download HTML from a given webpage.
To experiment with this example, you need to run this on an actual device on
which wifi internet is connected.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as
NetworkConnection
under
a
package
com.example.networkconnection. While creating this project, make
sure you Target SDK and Compile With at the latest version of Android
SDK to use higher levels of APIs.
Android
Modify res/values/string.xml
components.
Run the application and choose a running android device and install
the application on it and verify the results.
file
and
add
necessary
Here
is
the
of src/com.example.networkconnection/MainActivity.java.
string
content
package com.example.networkconnection;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
Android
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Here
is
the
content
of src/com.example.networkconnection/DownloadWebPage.java.
package com.example.networkconnection;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.widget.EditText;
import android.widget.TextView;
546
Android
import android.widget.Toast;
}
else{
Toast.makeText(context, "not conencted to internet",
Toast.LENGTH_SHORT).show();
}
}
protected void onPreExecute(){
547
Android
checkInternetConenction();
}
@Override
protected String doInBackground(String... arg0) {
try{
String link = (String)arg0[0];
URL url = new URL(link);
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader
(is, "UTF-8") );
String data = null;
String webPage = "";
while ((data = reader.readLine()) != null){
webPage += data + "\n";
}
return webPage;
}catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result){
this.dataField.setText(result);
}
}
548
Android
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="@string/url"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
549
Android
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="69dp"
android:onClick="download"
android:text="@string/click" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/button1"
android:layout_marginTop="56dp"
android:text="@string/google"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
Here is the content of Strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">NetworkConnection</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="url">URL here</string>
<string name="click">Download WebPage</string>
<string name="google">http://www.tutorialspoint.com</string>
</resources>
550
Android
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.networkconnection.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
551
Android
552
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
553
Android
Now just type in your website whose HTML you want to fetch. In our case we are
typing tutorialspoint.com. It is shown in the figure:
554
Android
Now press the Download WebPage button and wait for a few seconds and HTML
will be downloaded and will be shown to you. It is shown in the figure below:
555
Android
NFC stands for Near Field Communication, and as the name implies it
provides a wireless communication mechanism between two compatible devices.
NFC is a short range wireless technology having a range of 4cm or less for two
devices to share data.
How It Works:
Like Bluetooth and WiFi, and all manner of other wireless signals, NFC works on
the principle of sending information over radio waves. NFC data is send through
electromagnetic induction between two devices.
NFC works on the basis of tags, it allows you to share some amount of data
between an NFC tag and an android powered device or between two android
powered devices. Tags have various set of complexities. The Data stored in the
tag can be written in a variety of formats, but android APIs are based around a
NFC standard called as NFC Data Exchange Format (NDEF).
The transmission frequency for data across NFC is 13.56 megahertz, and data
can be sent at either 106, 212 or 424 kilobytes per second, which is quick
enough for a range of data transfers from contact details to swapping pictures,
songs and videos.
Android powered devices with NFC supports following three main modes of
operations:
Reader/Writer Mode:
It allows the NFC device to read or write passive NFC tags.
P2P mode:
This mode allows NFC device to exchange data with other NFC peers.
556
Android
<uses-sdk android:minSdkVersion="10"/>
First thing to note is that not all android powered devices provide NFC
technology. So to make sure that your application shows up in google play for
only those devices that have NFC Hardware, add the following line in
your Android.Manifest file.
<uses-feature android:name="android.hardware.nfc"
android:required="true"/>
Android provides an android.nfc package for communicating with another device.
This package contains following classes:
Sr.No
Classes
NdefMessage
It represents an immutable NDEF Message. .
NdefRecord
It represents an immutable NDEF Record.
NfcAdapter
It represents the local NFC adapter.
NfcEvent
It wraps information associated with any NFC event.
NfcManager
It is a high level manager used to obtain an instance of an NfcAdapter.
Tag
It represents an NFC tag that has been discovered.
NFC tags system works in android with the help of some intent filters that are
listed below:
557
Android
Sr.No
ACTION_NDEF_DISCOVERED
This intent is used to start an Activity when a tag contains an NDEF
payload.
ACTION_TECH_DISCOVERED
This intent is used to start an activity if the tag does not contain NDEF
data, but is of known technology.
ACTION_TAG_DISCOVERED
This
intent
is
started
if
no
activities
handle
the
ACTION_NDEF_DISCOVERED or ACTION_TECH_DISCOVERED intents.
Future Applications
With this technology growing day by day and due to introduction of contact-less
payment systems this technology is getting a boom. A service known as Google
Wallet is already introduced in the US whose purpose is to make our
smartphones a viable alternative to credit and transport cards.
558
56. PHP/MYSQL
Android
Here, in this chapter, we are going to explain, how you can integrate PHP and
MYSQL with your android application. This is very useful in case you have a
webserver, and you want to access its data on your android application.
MYSQL is used as a database at the webserver and PHP is used to fetch data
from the database. Our application will communicate with the PHP page with
necessary parameters and PHP will contact MYSQL database and will fetch the
result and return the results to us.
PHP - MYSQL
Creating Database
MYSQL database can be created easily using this simple script. The CREATE
DATABASE statement creates the database.
<?php
$con=mysqli_connect("example.com","username","password");
$sql="CREATE DATABASE my_db";
if (mysqli_query($con,$sql))
{
echo "Database my_db created successfully";
}
?>
Creating Tables
Once database is created, it is time to create some tables in the database.
The CREATE TABLE statement creates the database.
<?php
$con=mysqli_connect("example.com","username","password","my_db");
$sql="CREATE TABLE table1(Username CHAR(30),Password CHAR(30),Role
CHAR(30))";
if (mysqli_query($con,$sql))
{
echo "Table have been created successfully";
559
Android
}
?>
FROM
table1
where
$row = mysqli_fetch_array($result);
560
Android
$data = $row[0];
if($data){
echo $data;
}
mysqli_close($con);
?>
The second method is to use POST method. The only change in the above script
is to replace $_GET with $_POST. In Post method, the variables are not passed
through URL.
= URLEncoder.encode("username", "UTF-8")
Android
Example:
The below example is a complete example of connecting your android application
with MYSQL database via PHP page. It creates a basic application that allows you
to login using GET and POST method.
Android
echo $data;
}
mysqli_close($con);
?>
Android Part
To experiment with this example, you need to run this on an actual device on
which wifi internet is connected.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as PHPMYSQL under a package com.example.phpmysql. While creating
this project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify res/values/string.xml
components.
Run the application and choose a running android device and install
the application on it and verify the results.
file
and
add
necessary
string
import android.app.Activity;
import android.os.Bundle;
563
Android
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameField = (EditText)findViewById(R.id.editText1);
passwordField = (EditText)findViewById(R.id.editText2);
status = (TextView)findViewById(R.id.textView6);
role = (TextView)findViewById(R.id.textView7);
method = (TextView)findViewById(R.id.textView9);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void login(View view){
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
method.setText("Get Method");
new SigninActivity(this,status,role,0).execute(username,password);
}
564
Android
}
Here is the content of src/com.example.phpmysql/SigninActivity.java.
package com.example.phpmysql;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
extends AsyncTask<String,Void,String>{
Android
}
@Override
protected String doInBackground(String... arg0) {
if(byGetOrPost == 0){ //means by Get Method
try{
String username = (String)arg0[0];
String password = (String)arg0[1];
String link =
"http://myphpmysqlweb.hostei.com/login.php?username="
+username+"&password="+password;
URL url = new URL(link);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
Android
break;
}
in.close();
return sb.toString();
}catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
else{
try{
String username = (String)arg0[0];
String password = (String)arg0[1];
String link="http://myphpmysqlweb.hostei.com/loginpost.php";
String data
= URLEncoder.encode("username", "UTF-8")
Android
return sb.toString();
}catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
}
@Override
protected void onPostExecute(String result){
this.statusField.setText("Login Successful");
this.roleField.setText(result);
}
}
Here is the content of activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="textPassword" >
568
Android
</EditText>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="44dp"
android:ems="10" >
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="@string/Username" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/App"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView7"
569
Android
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView5"
android:layout_alignLeft="@+id/textView6"
android:text="@string/Role"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="10sp" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView6"
android:layout_marginTop="27dp"
android:layout_toLeftOf="@+id/editText1"
android:text="@string/LoginRole" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView6"
android:layout_alignLeft="@+id/textView5"
android:layout_marginBottom="27dp"
android:text="@string/method" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView8"
android:layout_below="@+id/button1"
android:layout_marginTop="86dp"
android:text="@string/LoginStatus" />
570
Android
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView4"
android:layout_centerHorizontal="true"
android:text="@string/Status"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="10sp" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView8"
android:layout_alignLeft="@+id/textView6"
android:text="@string/Choose"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="10sp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/textView6"
android:onClick="loginPost"
android:text="@string/LoginPost" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
571
Android
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignLeft="@+id/textView2"
android:onClick="login"
android:text="@string/LoginGet" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
android:text="@string/Password" />
</RelativeLayout>
Here is the content of Strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">PHPMYSQL</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Username">Username</string>
<string name="Password">Password</string>
<string name="LoginGet">Login - Get</string>
<string name="LoginPost">Login - Post</string>
<string name="App">Login Application</string>
<string name="LoginStatus">Login Status</string>
<string name="LoginRole">Login Role</string>
<string name="Status">Not login</string>
<string name="Role">Not assigned</string>
<string name="method">Login Method</string>
572
Android
</resources>
Here is the content of AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.phpmysql"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.phpmysql.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
573
Android
Let's try to run your PHPMYSQL application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
574
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
575
Android
Now just type in your username and password. In our case we are typing admin
as username and password. It is shown in the figure:
576
Android
Now press the Get button, wait a few seconds and response will be downloaded
and will be shown to you. In this case, the response is the ROLE that is fetched
in case of admin as username and password. It is shown in the figure below:
577
Android
Now again press the POST button and same result woud appear. It is shown in
the figure below:
578
Android
The easiest way to make a progress circle is using a class called ProgressDialog.
The loading bar can also be made through that class. The only logical difference
between bar and circle is, that the former is used when you know the total time
for waiting for a particular task whereas the latter is used when you do not know
the waiting time.
In order to this, you need to instantiate an object of this class. Its syntax is.
ProgressDialog progress = new ProgressDialog(this);
Now you can set some properties of this dialog. Such as, its style,its text etc.
progress.setMessage("Downloading Music :) ");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
Apart from these methods, there are other methods that are provided by the
ProgressDialog class.
Sr. No
getMax()
This method returns the maximum value of the progress.
incrementProgressBy(int diff)
This method increment the progress bar by the difference of value
passed as a parameter.
setIndeterminate(boolean indeterminate)
This method sets
indeterminate.
the
progress
indicator
as
determinate
or
setMax(int max)
This method sets the maximum value of the progress dialog.
setProgress(int value)
This method is used to update the progress dialog with some specific
579
Android
value.
6
Example:
This example demonstrates the spinning use of the progress dialog. It display a
spinning progress dialog on pressing the button.
To experiment with this example, you need to run this on an actual device on
after developing the application according to the steps below.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as ProgressDialog under a package com.example.progressdialog.
While creating this project, make sure you Target SDK and Compile
With at the latest version of Android SDK to use higher levels of APIs.
Run the application and choose a running android device and install
the application on it and verify the results.
Following
is
the
content
of
the
modified
main
src/com.example.progressdialog/MainActivity.java.
activity
file
package com.example.progressdialog;
import com.example.progressdialog.R;
580
Android
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
@Override
public void run(){
int jumpTime = 0;
while(jumpTime < totalProgressTime){
try {
sleep(200);
581
Android
jumpTime += 5;
progress.setProgress(jumpTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Modify the content of res/layout/activity_main.xml to the following:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
582
Android
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:onClick="open"
android:text="@string/download_button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="19dp"
android:text="@string/download_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Modify the res/values/string.xml to the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ProgressDialog</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="download_button">Download</string>
<string
name="download_text">Press
music</string>
the
button
to
download
</resources>
583
Android
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.progressdialog.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
584
Android
Let's try to run your ProgressDialog application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
585
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
586
Android
Just press the button to start the Progress Dialog. After pressing, following
screen would appear:
587
Android
getMax()
This method returns the maximum value of the progress.
incrementProgressBy(int diff)
This method increments the progress bar by the difference of value
passed as a parameter.
setIndeterminate(boolean indeterminate)
This method sets
indeterminate.
the
progress
indicator
as
determinate
or
setMax(int max)
This method sets the maximum value of the progress dialog.
setProgress(int value)
588
Android
This method is used to update the progress dialog with some specific
value.
6
show(Context
message)
context,
CharSequence
title,
CharSequence
Example:
This example demonstrates the horizontol use of the progress dialog which is in
fact a progress bar. It display a progress bar on pressing the button.
To experiment with this example, you need to run this on an actual device after
developing the application according to the steps below.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as ProgressDialog under a package com.example.progressdialog.
While creating this project, make sure you Target SDK and Compile
With at the latest version of Android SDK to use higher levels of APIs.
Run the application and choose a running android device and install
the application on it and verify the results.
Following
is
the
content
of
the
modified
main
src/com.example.progressdialog/MainActivity.java.
activity
file
package com.example.progressdialog;
import com.example.progressdialog.R;
589
Android
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
@Override
public void run(){
int jumpTime = 0;
while(jumpTime < totalProgressTime){
try {
590
Android
sleep(200);
jumpTime += 5;
progress.setProgress(jumpTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Modify the content of res/layout/activity_main.xml to the following:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
591
Android
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:onClick="open"
android:text="@string/download_button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="19dp"
android:text="@string/download_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Modify the res/values/string.xml to the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ProgressDialog</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="download_button">Download</string>
<string
name="download_text">Press
music</string>
the
button
to
download
</resources>
592
Android
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.progressdialog.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
593
Android
Let's try to run your ProgressDialog application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
594
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
595
Android
Just press the button to start the Progress bar. After pressing, following screen
would appear:
596
Android
It will continuously update itself, and after few seconds, it would appear
something like this.
597
Android
598
Android
Sr.No
cancel(int id)
This method cancels a previously shown notification.
cancelAll()
This method cancels all previously shown notifications.
Example:
The below example demonstrates the use of NotificationManager class. It
creates a basic application that allows you to create a notification.
To experiment with this example, you need to run this on an actual device or in
an emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Status under a package com.example.status. While creating this
project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify
res/values/string.xml
file
and
add
necessary
string
599
Android
components.
5
Run the application and choose a running android device and install
the application on it and verify the results.
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
NotificationManager NM;
EditText one,two,three;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (EditText)findViewById(R.id.editText1);
two = (EditText)findViewById(R.id.editText2);
three = (EditText)findViewById(R.id.editText3);
}
@Override
600
Android
@SuppressWarnings("deprecation")
public void notify(View vobj){
String title = one.getText().toString();
String subject = two.getText().toString();
String body = three.getText().toString();
NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVIC
E);
Notification notify=new Notification(android.R.drawable.
stat_notify_more,title,System.currentTimeMillis());
PendingIntent pending=PendingIntent.getActivity(
getApplicationContext(),0, new Intent(),0);
notify.setLatestEventInfo(getApplicationContext(),subject,body,pendi
ng);
NM.notify(0, notify);
}
}
Here is the content of activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
601
Android
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="86dp"
android:onClick="notify"
android:text="@string/notification" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="53dp"
android:ems="10" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="28dp"
android:ems="10" />
<EditText
android:id="@+id/editText3"
602
Android
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="23dp"
android:ems="10" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText1"
android:layout_marginRight="14dp"
android:layout_toLeftOf="@+id/editText1"
android:text="@string/title" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText3"
android:layout_alignRight="@+id/textView1"
android:text="@string/heading" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText3"
android:layout_alignLeft="@+id/textView2"
android:text="@string/body" />
<TextView
android:id="@+id/textView4"
603
Android
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView2"
android:text="@string/create"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Here is the content of Strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Status</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="notification">Notify</string>
<string name="title">Title</string>
<string name="heading">Subject</string>
<string name="body">Body</string>
<string name="create">Create Notification</string>
</resources>
Here is the content of AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.status"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
604
Android
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.status.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
605
Android
Let's try to run your TextToSpeech application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
606
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
607
Android
Now fill in the field with the title, subject and the body. This has been shown
below in the figure:
608
Android
Now click on the notify button and you will see a notification in the top
notification bar. It has been shown below:
609
Android
Now scroll down the notification bar and see the notification. This has been
shown below in the figure:
610
60. RENDERSCRIPT
Android
This chapter teaches you about Android RenderScript. Usually the apps on
android are designed as to consume as minimum resources as possible. But
some applications like some 3D games need high level processing on android.
To provide these applications high performance android introduced the
RenderScript. It is android based framework which is used for running
applications that perform very highly computational tasks. The development on
this framework is done in Native Development Kit (NDK) provided by android.
RenderScript is extremely useful for applications which performs following types
of actions:
3D Rendering
Image Processing
Computational Photography
Computer Vision
How to Begin:
To use the RenderScript Framework you must have following two things:
A RenderScript Kernel
RenderScript APIs
A RenderScript Kernel
A kernel is a program which manages data processing instructions and manage
workload on Central Processing Units. A kernel is a fundamental part of the
operating system.
Similarly to run the RenderScript framework we need a written script named as
Kernel to manage all the data processing requests from our app and utilize more
features of the android OS provided by the NDK and as mentioned earlier that
611
Android
Elements
A Language declaration
It declares the version of RenderScript Kernel language used in this
script.
A package declaration
This declaration names the package name of the Java class which will
be affected by this Kernel Code.
Invokable functions
You can call these invokable functions from your JAVA code with
arbitrary arguments.
RenderScript APIs
If you want to use RenderScript in your API, you can do it in following two ways:
612
Android
Sr.No
APIs
android.renderscript
This API is available on devices running Android 3.0 and higher.
android.support.v8.renderscript
This API is available on devices running Android 2.2 and higher.
Project properties
renderscript.target
It specifies the bytecode version to be generated.
renderscript.support.mode
It specifies a compatible version for the generated bytecode to fall
back.
613
Android
sdk.buildtools
It specifies the versions of Android SDK build tools to use.
Now call your RenderScript Kernel functions and compute complex algorithms in
your app.
614
Android
RSS stands for Really Simple Syndication. RSS is an easy way to share your
website updates and content with your users so that users might not have to
visit your site daily for any kind of updates.
RSS Example
RSS is a document that is created by the website with .xml extension. You can
easily parse this document and show it to the user in your application. An RSS
document looks like this.
<rss version="2.0">
<channel>
<title>Sample RSS</title>
<link>http://www.google.com</link>
<description>World's best search engine</description>
</channel>
</rss>
RSS Elements
An RSS document such as above has the following elements.
Sr.No
channel
This element is used to describe the RSS feed.
title
Defines the title of the channel.
link
Defines the hyperlink to the channel.
description
615
Android
Parsing RSS
Parsing an RSS document is more like parsing XML. So now lets see how to
parse an XML document.
For this, we will create XMLPullParser object, but in order to create that we will
first create XmlPullParserFactory object and then call its newPullParser() method
to create XMLPullParser. Its syntax is given below:
private XmlPullParserFactory xmlFactoryObject =
XmlPullParserFactory.newInstance();
private XmlPullParser myparser = xmlFactoryObject.newPullParser();
The next step involves specifying the file for XmlPullParser that contains XML. It
could be a file or could be a Stream. In our case it is a stream. Its syntax is
given below:
myparser.setInput(stream, null);
The last step is to parse the XML. An XML file consist of events, Name, Text,
AttributesValue etc. So XMLPullParser has a separate function for parsing each of
the component of XML file. Its syntax is given below:
int event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT)
{
String name=myParser.getName();
switch (event){
case XmlPullParser.START_TAG:
break;
case XmlPullParser.END_TAG:
if(name.equals("temperature")){
temperature = myParser.getAttributeValue(null,"value");
}
break;
}
event = myParser.next();
}
616
Android
The method getEventType returns the type of event that happens. e.g.:
Document start, tag start etc. The method getName returns the name of the
tag. Since we are only interested in temperature, we just check in conditional
statement to get a temperature tag, we call the method getAttributeValue to
return us the value of temperature tag.
Apart from these methods, there are other methods provided by this class for
better parsing XML files. These methods are listed below:
Sr.No
getAttributeCount()
This method just Returns the number of attributes of the current start
tag.
getAttributeName(int index)
This method returns the name of the attribute specified by the index
value.
getColumnNumber()
This method returns the current column number, starting from 0.
getDepth()
This method returns the current depth of the element.
getLineNumber()
Returns the current line number, starting from 1.
getNamespace()
This method returns the namespace URI of the current element.
getPrefix()
This method returns the prefix of the current element.
getName()
This method returns the name of the tag.
getText()
617
Android
isWhitespace()
This method checks whether the current TEXT event contains only
whitespace characters.
Example:
Here is an example demonstrating the use of XMLPullParser class. It creates a
basic Parsing application that allows you to parse an RSS document present here
at http://tutorialspoint.com/android/sampleXML.xml and then shows the result.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as RSSReader under a package com.example.rssreader. While creating
this project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
add
respective
XML
618
Android
Following
is
the
content
of
the
modified
src/com.example.rssreader/MainActivity.java.
main
activity
file
package com.example.rssreader;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
private String
finalUrl="http://tutorialspoint.com/android/sampleXML.xml";
private HandleXML obj;
private EditText title,link,description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = (EditText)findViewById(R.id.editText1);
link = (EditText)findViewById(R.id.editText2);
description = (EditText)findViewById(R.id.editText3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
619
Android
the
java
package com.example.rssreader;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.util.Log;
Android
}
public String getTitle(){
return title;
}
public String getLink(){
return link;
}
public String getDescription(){
return description;
}
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text=null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name=myParser.getName();
switch (event){
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if(name.equals("title")){
title = text;
}
else if(name.equals("link")){
link = text;
}
else if(name.equals("description")){
description = text;
}
else{
621
Android
}
break;
}
event = myParser.next();
}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void fetchXML(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,
false);
myparser.setInput(stream, null);
parseXMLAndStoreIt(myparser);
stream.close();
} catch (Exception e) {
}
622
Android
}
});
thread.start();
}
}
Modify the content of res/layout/activity_main.xml to the following:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="48dp"
623
Android
android:layout_toLeftOf="@+id/textView1"
android:text="@string/title" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="27dp"
android:text="@string/link" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/editText1"
android:ems="10" >
</EditText>
<EditText
android:id="@+id/editText3"
624
Android
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/editText2"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:onClick="fetch"
android:text="@string/fetch" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/description" />
</RelativeLayout>
Modify the res/values/string.xml to the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">RSSReader</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Sample RSS Reader</string>
<string name="title">title</string>
<string name="link">link</string>
625
Android
<string name="description">Description</string>
<string name="fetch">Fetch Feed</string>
</resources>
This is the default AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rssreader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.rssreader.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
626
Android
Let's try to run your RSSReader application. We assume, you had created
your AVD while doing environment setup. To run the app from Eclipse, open one
of your project's activity files and click Run
icon from the toolbar. Eclipse
installs the app on your AVD and starts it and if everything is fine with your
setup and application, it will display following Emulator window:
627
Android
Just press the Fetch Feed button to fetch RSS feed. After pressing, following
screen would appear which would show the RSS data.
628
Android
Description
Step 1
You
can
download
the
latest
from http://developer.android.com/sdk/index.html .
android
SDK
Step 2
Turn on USB debugging feature on your device. It is usually found under settings
and developer options.
Step 3
Just connect your PC with your phone via the USB data cable.
Step 4
Install Java run time 5 or later, if you have not installed already. You can install
it from http://www.oracle.com/technetwork/java/javase/downloads/index.html .
629
Android
Step 5
Finally install the androidScreenCast application. You can download it
from https://code.google.com/p/androidscreencast/ . Once you download it,
click to open. It is shown below:
Just wait for a few seconds for the application to load and following pop-up will
appear asking your permission to launch this application. Click on accept check
box and click on run. It is shown below:
630
Android
If everything work fine, you will now see your phone on your pc. Navigate
through your phone and you will see your mobile working on your pc. It is
shown below:
631
Android
You can see the message application in the above picture, that's because we
have opened the messaging application in our mobile. Now type something from
your mobile.
As you can see, we have written some text in the sms from our mobile and it
appears on PC. So this way you can use this ScreenCast application.
632
Android
To download and install latest android APIs and development tools from the
internet, android provide us with android SDK manager. Android SDK Manager
separates the APIs, tools and different platforms into different packages which
you can download.
Android SDK manager comes with the Android SDK bundle. You can't download
it
separately.
You
can
download
the
android
sdk
from
http://developer.android.com/sdk/index.html .
Double Click on the SDK Manager.exe file in the Android SDK folder.
You can select which package you want to download by selecting the checkboxes
and then click Install to install those packages. By default SDK Manager keeps it
up to date with latest APIs and other packages.
Once you download the SDK, following packages are available, but first three are
necessary to run your SDK and others are recommended.
633
Android
Recommended Packages
Sr.No
Package
SDK Tools
This is necessary package to run your SDK.
SDK Platform-tools
This package will be installed once when you first run the SDK
manager.
SDK Platform
Atleast one platform must be installed in your environment to run your
application.
System Image
It's a good practice to download system images for all of the android
versions so you can test your app on them with the Android Emulator.
SDK Samples
This will give you some sample codes to learn about android.
634
Android
635
Android
Just add the URL of Add-on.xml file and click Ok. Now you can download the
Third Party Add-on in your development environment and use it.
636
64. SENSORS
Android
Most of the android devices have built-in sensors that measure motion,
orientation, and various environmental condition. The android platform supports
three broad categories of sensors.
Motion Sensors
Environmental sensors
Position sensors
Some of the sensors are hardware based and some are software based sensors.
Whatever the sensor is, android allows us to get the raw data from these
sensors and use it in our application. For this, android provides us with some
classes.
Android provides SensorManager and Sensor classes to use the sensors in our
application. In order to use sensors, first thing you need to do is to instantiate
the object of SensorManager class. It can be achieved as follows.
SensorManager sMgr;
sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
The next thing you need to do is to instantiate the object of Sensor class by
calling the getDefaultSensor() method of the SensorManager class. Its syntax is
given below:
Sensor light;
light = sMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
Once that sensor is declared, you need to register its listener and override two
methods which are onAccuracyChanged and onSensorChanged. Its syntax is as
follows:
sMgr.registerListener(this, light,SensorManager.SENSOR_DELAY_NORMAL);
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
}
637
Android
getDefaultSensor(int type)
This method gets the default sensor for a given type.
getInclination(float[] I)
This method computes the geomagnetic inclination angle in radians
from the inclination matrix.
Android
matrix.
7
Example:
Here is an example demonstrating the use of SensorManager class. It creates a
basic application that allows you to view the list of sensors on your device.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Sensors under a package com.example.sensors. While creating this
project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
Following
is
the
content
of
the
modified
src/com.example.sensors/MainActivity.java.
add
respective
main
activity
XML
file
package com.example.sensors;
import java.util.List;
import android.app.Activity;
639
Android
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
List list = sMgr.getSensorList(Sensor.TYPE_ALL);
}
sensorsData.setText(data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
640
Android
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
641
Android
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sensors</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="list">List of sensors supported</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sensors"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
642
Android
<activity
android:name="com.example.sensors.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Let's try to run our Sensor application we just modified. We assume, you had
created your AVD while doing environment setup. To run the app from Eclipse,
open one of your project's activity files and click Run icon from the toolbar.
Eclipse installs the app on your AVD and starts it and if everything is fine with
your setup and application, it will display following Emulator window:
643
Android
Now if you will look at your device screen, you will see the list of sensors
supported by your device along with their name and version and other
information.
If you would run this application on different devices, the output would be
different because the output depends upon the number of sensors supported by
your device.
644
Android
Session help you when want to store user data outside your application, so that
when the next time user use your application, you can easily get back his details
and perform accordingly.
This can be done in many ways. But the most easiest and nicest way of doing
this is through Shared Preferences.
Shared Preferences
Shared Preferences allows you to save and retrieve data in the form of key value
pair. In order to use shared preferences, you have to call a method
getSharedPreferences() that returns a SharedPreference instance pointing to the
file that contains the values of preferences.
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES,
Context.MODE_PRIVATE);
You
can
save
something
in
the
shared
preferences
by
using
SharedPreferences.Editor class. You will call the edit method of
SharedPreference instance and will receive it in an editor object. Its syntax is:
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
Apart from the putString method, there are methods availaible in the editor
class that allows manipulation of data inside shared preferences. They are listed
as follows:
Sr.
No.
apply()
It is an abstract method. It will commit your changes back from
editor to the sharedPreference object you are calling.
clear()
It will remove all values from the editor.
645
Android
remove(String key)
It will remove the value whose key has been passed as a parameter.
Description
You will use Eclipse IDE to create an Android application and name it
as
SessionManagement
under
a
package
com.example.sessionmanagement. While creating this project, make
sure you Target SDK and Compile With at the latest version of Android
SDK to use higher levels of APIs.
646
Android
Run the application and choose a running android device and install
the application on it and verify the results.
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
Android
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
}
@Override
protected void onResume() {
sharedpreferences=getSharedPreferences(MyPREFERENCES,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(name))
{
if(sharedpreferences.contains(pass)){
Intent i = new Intent(this,com.example.sessionmanagement.
Welcome.class);
startActivity(i);
}
}
super.onResume();
}
Android
}
Here is the content
of src/com.example.sessionmanagement/Welcome.java.
package com.example.sessionmanagement;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.welcome, menu);
649
Android
return true;
}
public void logout(View view){
SharedPreferences sharedpreferences = getSharedPreferences
(MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
moveTaskToBack(true);
Welcome.this.finish();
}
public void exit(View view){
moveTaskToBack(true);
Welcome.this.finish();
}
}
Here is the content of activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
650
Android
android:layout_below="@+id/textView2"
android:ems="10"
android:inputType="textPassword" >
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="52dp"
android:text="@string/Username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:layout_marginRight="16dp"
android:layout_marginTop="27dp"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/editText1"
android:text="@string/Password"
android:textAppearance="?android:attr/textAppearanceMedium" />
651
Android
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:text="@string/Signin"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:onClick="login"
android:text="@string/Login" />
</RelativeLayout>
Here is the content of activity_welcome.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Welcome" >
652
Android
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="146dp"
android:onClick="logout"
android:text="@string/logout" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="64dp"
android:text="@string/title_activity_welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:onClick="exit"
android:text="@string/exit" />
</RelativeLayout>
653
Android
<string name="app_name">SessionManagement</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Username">Username</string>
<string name="Password">Password</string>
<string name="Signin">Sign In</string>
<string name="Login">Login</string>
<string name="logout">Logout</string>
<string name="title_activity_welcome">Welcome</string>
<string name="exit">Exit without logout</string>
</resources>
Here is the content of AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sessionmanagement"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
654
Android
<activity
android:name="com.example.sessionmanagement.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
655
Android
656
Android
Type in your username and password (type anything you like, but remember
what you type), and click on login button. It is shown in the image below:
657
Android
As soon as you click on login button, you will be brought to this Welcome screen.
Now your login information is stored in shared preferences.
658
Android
Now click on Exit without logout button and you will be brought back to the
home screen. This is shown in the image below:
659
Android
Now Start the application again. And this time you will not be brought to the
login screen, but directly to the welcome screen.This is shown in the image
below:
660
Android
Now click on logout button, and the application will be closed. Now open the
application again, and since you have logout your session, so you will be brought
back to the front login screen. This is shown in the image below:
661
Android
SIP stands for (Session Initiation Protocol). It is a protocol that let applications
easily set up outgoing and incoming voice calls, without having to manage
sessions, transport-level communication, or audio record or playback directly.
Applications
Some of the common applications of SIP are.
Video conferencing
Instant messaging
Requirements
Here are the requirements for developing a SIP application:
SIP Classes
Here is a summary of the classes that are included in the Android SIP API:
Sr.
NO
SipAudioCall
Handles an Internet audio call over SIP.
SipErrorCode
Defines error codes returned during SIP actions.
SipManager
Provides APIs for SIP tasks, such as initiating SIP connections, and
provides access to related SIP services.
662
Android
SipProfile
Defines a SIP profile, including a SIP account, domain and server.
information
SipSession
Represents a SIP session that is associated with a SIP dialog or a
standalone transaction not within a dialog.
Functions of SIP
SIP has following major functions.
SIP provides feature negotiation, so that all the parties in the call can
agree to the features supported among them
Components of SIP
SIP has two major components which are listed below.
UAC
UAC or User Agent Client are those end users who generates requests and send
those requests to the server.These requests are generated by the client
applications running on their systems.
UAS
UAS or User Agent Server are those systems which get the request generated by
UAC. The UAS process those requests and then according to the requests it
generates responses accordingly.
SipManager
SipManager is an android API for SIP tasks, such as initiating SIP connections,
and provides access to related SIP services. This class is the starting point for
any SIP actions. You can acquire an instance of it with newInstance().
663
Android
The SipManager has many functions for managing SIP tasks. Some of the
functions are listed below.
Sr.
NO
close(String localProfileUri)
Closes the specified profile to not make/receive calls.
getCallId(Intent incomingCallIntent)
Gets the call ID from the specified incoming call broadcast intent.
isOpened(String localProfileUri)
Checks if the specified profile is opened in the SIP service for making
and/or receiving calls.
isSipWifiOnly(Context context)
Returns true if SIP is only available on WIFI.
isRegistered(String localProfileUri)
Checks if the SIP service has successfully registered the profile to the
SIP provider (specified in the profile) for receiving calls.
isVoipSupported(Context context)
Returns true if the system supports SIP-based VOIP API.
unregister(SipProfile
listener)
localProfile,
SipRegistrationListener
664
Android
The Android platform offers a spelling checker framework that lets you
implement and access spell checking in your application.
To use spelling checker, you need to implement SpellCheckerSessionListener
interface and override its methods. Its syntax is given below:
public class HelloSpellCheckerActivity extends Activity implements
SpellCheckerSessionListener {
@Override
public void onGetSuggestions(final SuggestionsInfo[] arg0) {
// TODO Auto-generated method stub
}
@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) {
// TODO Auto-generated method stub
}
}
Next thing you need to do is to create an object of SpellCheckerSession class.
This object can be instantiated by calling newSpellCheckerSession method of
TextServicesManager class. This class handles interaction between application
and text services. You need to request system service to instantiate it. Its
syntax is given below:
private SpellCheckerSession mScs;
final TextServicesManager tsm = (TextServicesManager) getSystemService(
Context.TEXT_SERVICES_MANAGER_SERVICE);
mScs = tsm.newSpellCheckerSession(null, null, this, true);
The last thing you need to do is to call getSuggestions method to get
suggestion for any text, you want. The suggestions will be passed onto
the onGetSuggestions method from where you can do whatever you want.
mScs.getSuggestions(new TextInfo(editText1.getText().toString()), 3);
665
Android
This method takes two parameters. First parameter is the string in the form of
TextInfo object, and second parameter is the cookie number used to distinguish
suggestions.
Apart from the methods, there are other methods provided by the
SpellCheckerSession class for better handling suggestions. These methods are
listed below:
Sr.No
cancel()
Cancels pending and running spell check tasks.
close()
Finish this session and allow
disconnect the bound spell checker.
TextServicesManagerService
to
getSpellChecker()
Get the spell checker service info, this spell checker session has.
isSessionDisconnected()
True if the connection to a text service of this session is disconnected
and not alive.
Example:
Here is an example demonstrating the use of Spell Checker. It creates a basic
spell checking application that allows you to write text and get suggestions.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as
HelloSpellCheckerActivity
under
a
package
com.example.hellospellchecker. While creating this project, make sure
666
Android
you Target SDK and Compile With at the latest version of Android SDK
to use higher levels of APIs.
2
Run the application and choose a running android device and install
the application on it and verify the results.
Following
is
the
content
of
the
modified
main
src/com.example.hellospellchecker/MainActivity.java.
activity
file
package com.example.android.hellospellchecker;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.textservice.SentenceSuggestionsInfo;
import android.view.textservice.SpellCheckerSession;
import
android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener;
import android.view.textservice.SuggestionsInfo;
import android.view.textservice.TextInfo;
import android.view.textservice.TextServicesManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
extends
Activity
implements
Android
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMainView = (TextView)findViewById(R.id.main);
editText1 = (EditText)findViewById(R.id.editText1);
}
@Override
public void onResume() {
super.onResume();
final TextServicesManager tsm = (TextServicesManager)
getSystemService(
Context.TEXT_SERVICES_MANAGER_SERVICE);
mScs = tsm.newSpellCheckerSession(null, null, this, true);
}
@Override
public void onPause() {
super.onPause();
if (mScs != null) {
mScs.close();
}
}
Android
3);
}
@Override
public void onGetSuggestions(final SuggestionsInfo[] arg0) {
final StringBuilder sb = new StringBuilder();
}
@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) {
// TODO Auto-generated method stub
}
}
669
Android
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pre"
/>
<Button
android:id="@+id/mainbtn"
android:layout_width="150dip"
android:layout_height="50dip"
android:onClick="go"
android:text="@string/suggest" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
670
Android
<resources>
<string name="app_name">HelloSpellChecker</string>
<string name="suggest">suggest</string>
<string name="pre">Suggestions</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.hellospellchecker"
android:versionCode="1"
android:versionName="1.0" >
<application
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".HelloSpellCheckerActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
671
Android
</manifest>
Let's try to run our Spell Checker application we just modified. We assume, you
had created your AVD while doing environment setup. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Eclipse installs the app on your AVD and starts it and if everything is
fine with your setup and application, it will display following Emulator window:
672
Android
Now what you need to do is to enter any text in the field. For example, we have
entered some text. Press the suggestions button. The following notification
would appear in your AVD along with suggestions:
673
Android
Now change the text and press the button again, like we did. And this is what
comes on screen.
674
Android
SQLite is an open source SQL database that stores data to a text file on a
device. Android comes in with built in SQLite database implementation.
SQLite supports all the relational database features. To access this database,
you don't need to establish any kind of connections for it like JDBC, ODBC etc.
Database - Package
The main package is android.database.sqlite that contains the classes to manage
your own databases.
Database - Creation
In order to create a database you need to call the method
openOrCreateDatabase with your database name and mode as a parameter. It
returns an instance of SQLite database which you have to receive in your own
object. Its syntax is given below:
SQLiteDatabse mydatabase = openOrCreateDatabase("your database name",
MODE_PRIVATE, null);
Apart from this, there are other functions available in the database package that
does this job. They are listed below
Sr.No
openOrCreateDatabase(String path,
675
Android
SQLiteDatabase.CursorFactory factory)
It not only opens, but creates the database if it does not exists. This
method is equivalent to openDatabase method.
4
Database - Insertion
We can create table or insert data into table using execSQL method defined in
SQLiteDatabase class. Its syntax is given below:
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS TutorialsPoint(Username
VARCHAR,Password VARCHAR);");
mydatabase.execSQL("INSERT INTO TutorialsPoint
VALUES('admin','admin');");
This will insert some values into our table in our database. Another method that
also does the same job but take some additional parameter is given below
Sr.No
Database - Fetching
We
will
the
the
can retrieve anything from database using an object of the Cursor class. We
call a method of this class called rawQuery and it will return a resultset with
cursor pointing to the table. We can move the cursor forward and retrieve
data.
Android
There are other functions available in the Cursor class that allows us to
effectively retrieve the data. That includesSr.No
getColumnCount()
This method returns the total number of columns of the table.
getColumnIndex(String columnName)
This method returns the index number of a column by specifying the
name of the column.
getColumnName(int columnIndex)
This method returns the name of the column by specifying the index of
the column.
getColumnNames()
This method returns the array of all the column names of the table.
getCount()
This method returns the total number of rows in the cursor.
getPosition()
This method returns the current position of the cursor in the table.
isClosed()
This method returns true if the cursor is closed and returns false
otherwise.
Android
}
public void onCreate(SQLiteDatabase db) {}
public void onUpgrade(SQLiteDatabase database, int oldVersion, int
newVersion) {}
}
Example:
Here is an example demonstrating the use of SQLite Database. It creates a basic
contacts applications that allows insertion, deletion and modification of contacts.
To experiment with this example, you need to run this on an actual device on
which camera is supported.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as AddressBook under a package com.example.addressbook. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify the
components.
res/layout/activity_main
to
add
respective
XML
678
Android
10
Run the application and choose a running android device and install
the application on it and verify the results.
Following
is
the
content
of
the
modified
src/com.example.addressbook/MainActivity.java.
main
activity
file
package com.example.addressbook;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Android
ArrayAdapter arrayAdapter =
new ArrayAdapter(this,android.R.layout.simple_list_item_1,
array_list);
obj.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new
Intent(getApplicationContext(),com.example.addressbook.DisplayCo
ntact.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
680
Android
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.item1:
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", 0);
Intent intent = new
Intent(getApplicationContext(),com.example.addressbook.Displa
yContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
}
return super.onKeyDown(keycode, event);
}
}
Following
is
the
modified
content
of
display
src/com.example.addressbook/DisplayContact.java
contact
activity
package com.example.addressbook;
import android.os.Bundle;
681
Android
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
int from_Where_I_Am_Coming = 0;
private DBHelper mydb ;
TextView name ;
TextView phone;
TextView email;
TextView street;
TextView place;
int id_To_Update = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_contact);
name = (TextView) findViewById(R.id.editTextName);
phone = (TextView) findViewById(R.id.editTextPhone);
email = (TextView) findViewById(R.id.editTextStreet);
street = (TextView) findViewById(R.id.editTextEmail);
place = (TextView) findViewById(R.id.editTextCity);
682
Android
name.setText((CharSequence)nam);
name.setFocusable(false);
name.setClickable(false);
683
Android
phone.setText((CharSequence)phon);
phone.setFocusable(false);
phone.setClickable(false);
email.setText((CharSequence)emai);
email.setFocusable(false);
email.setClickable(false);
street.setText((CharSequence)stree);
street.setFocusable(false);
street.setClickable(false);
place.setText((CharSequence)plac);
place.setFocusable(false);
place.setClickable(false);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
int Value = extras.getInt("id");
if(Value>0){
getMenuInflater().inflate(R.menu.display_contact, menu);
}
else{
getMenuInflater().inflate(R.menu.main, menu);
}
}
return true;
684
Android
phone.setEnabled(true);
phone.setFocusableInTouchMode(true);
phone.setClickable(true);
email.setEnabled(true);
email.setFocusableInTouchMode(true);
email.setClickable(true);
street.setEnabled(true);
street.setFocusableInTouchMode(true);
street.setClickable(true);
place.setEnabled(true);
place.setFocusableInTouchMode(true);
place.setClickable(true);
return true;
case R.id.Delete_Contact:
Android
builder.setMessage(R.string.deleteContact)
.setPositiveButton(R.string.yes, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted
Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new
Intent(getApplicationContext(),com.example.addressbook.MainAc
tivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Android
687
Android
Following
is
the
content
class src/com.example.addressbook/DBHelper.java
of
Database
package com.example.addressbook;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
688
Android
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table contacts " +
"(id integer primary key, name text,phone text,email text, street
text,place text)"
);
}
@Override
public void
newVersion) {
onUpgrade(SQLiteDatabase
db,
int
oldVersion,
int
contentValues.put("name", name);
contentValues.put("phone", phone);
contentValues.put("email", email);
contentValues.put("street", street);
contentValues.put("place", place);
Android
Cursor res =
id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db,
CONTACTS_TABLE_NAME);
return numRows;
}
public boolean updateContact (Integer id, String name, String phone,
String email, String street,String place)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("phone", phone);
contentValues.put("email", email);
contentValues.put("street", street);
contentValues.put("place", place);
db.update("contacts", contentValues, "id = ? ", new String[] {
Integer.toString(id) } );
return true;
}
Android
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAM
E)));
res.moveToNext();
}
return array_list;
}
}
Following is the content of the res/layout/activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
691
Android
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="370dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<EditText
android:id="@+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="82dp"
android:ems="10"
android:inputType="text" >
</EditText>
<EditText
android:id="@+id/editTextEmail"
692
Android
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editTextStreet"
android:layout_below="@+id/editTextStreet"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="textEmailAddress" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editTextName"
android:layout_alignParentLeft="true"
android:text="@string/name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editTextCity"
android:layout_alignParentBottom="true"
android:layout_marginBottom="28dp"
android:onClick="run"
android:text="@string/save" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editTextEmail"
android:layout_alignLeft="@+id/textView1"
android:text="@string/email"
693
Android
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editTextPhone"
android:layout_alignLeft="@+id/textView1"
android:text="@string/phone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editTextEmail"
android:layout_alignLeft="@+id/textView5"
android:text="@string/street"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editTextCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editTextName"
android:layout_below="@+id/editTextEmail"
android:layout_marginTop="30dp"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
694
Android
android:layout_alignBaseline="@+id/editTextCity"
android:layout_alignBottom="@+id/editTextCity"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/editTextEmail"
android:text="@string/country"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editTextStreet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editTextName"
android:layout_below="@+id/editTextPhone"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editTextPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editTextStreet"
android:layout_below="@+id/editTextName"
android:ems="10"
android:inputType="phone|text" />
</RelativeLayout>
</ScrollView>
Following is the content of the res/value/string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
695
Android
<string name="name">Name</string>
<string name="phone">Phone</string>
<string name="email">Email</string>
<string name="street">Street</string>
<string name="country">City/State/Zip</string>
<string name="deleteContact">Are
it.</string>
you
sure,
you
want
to
delete
<string name="yes">Yes</string>
<string name="no">No</string>
</resources>
Following is the content of the res/menu/mainmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item1"
android:icon="@drawable/add"
android:title="@string/Add_New"
android:showAsAction="ifRoom|withText">
</item>
</menu>
Following is the content of the res/menu/display_contact.xml
696
Android
</menu>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.addressbook.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
697
Android
</manifest>
Let's try to run your Camera application. We assume, you have connected your
actual Android Mobile device with your computer. To run the app from Eclipse,
open one of your project's activity files and click Run
icon from the toolbar.
Before starting your application, Eclipse will display following window to select
an option where you want to run your Android application.
698
Android
Select your mobile device as an option and then check your mobile device which
will display following screen:
699
Android
Click on the add button on the top right corner of the menu screen to add a new
contact. It will display the following screen:
700
Android
It will display the following fields. Please enter the required information and click
on save contact. It will bring you back to main screen.
701
Android
702
Android
Now our contact john has been added. Tap on this to edit or delete the contact.
It will bring you to the following screen. Now select menu from your mobile. And
there will be two options there.
703
Android
Select delete contact and a dialog box would appear asking you about deleting
this contact. It would be like this -
704
Android
Select Yes from the above screen that appears and a notification will appear that
the contact has been deleted successfully. It would appear like this -
In order to see where your database is created, open your eclipse, connect your
mobile, go to right corner and select DDMS. Now browse the file explorer tab.
Now
browse
this
folder /data/data/<your.package.name>/databases<database-name>.
705
Android
When you develop an app on a latest version of android like 4.0 and you also
want it to run on those devices which are running older versions of android like
3.2 etc. you can't do that until you add backward compatability to your code.
To provide this backward compatability android provides you the Android
Support Library package. The Android Support Library package is a set of code
libraries that provide backward-compatible versions of Android framework APIs
as well as features that are only available through the library APIs. Each Support
Library is backward-compatible to a specific Android API level.
Including the Support Libraries in your Android project is considered a best
practice for application developers, depending on the range of platform versions
your app is targeting and the APIs that it uses.
v4 Support Library
This library is designed to be used with Android 1.6 (API level 4) and
higher.
v7 Support Library
There are several libraries designed to be used with Android 2.1 (API
level 7) and higher.
v8 Support Library
This library is designed to be used with Android (API level 8) and
higher.
706
Android
Please remember that use of Android Support Library in your app code is
encouraged and preferred. By using these libraries you can increase your target
market and target audience.
In the SDK Manager window, scroll to the end of the Packages list, find
the Extras folder.
707
Android
After downloading, the tool installs the Support Library files to your existing
Android SDK directory. The library files are located in the following sub-directory
of your SDK:/extras/android/support/ directory.
Changes in Android.Manifest
If you are increasing the backward compatibility of your existing application to
an earlier version of the Android API with the Support Library, make sure to
update your application's manifest. Specifically, you should update
the android:minSdkVersion element of the tag in the manifest to the new,
lower version number, as shown below:
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
This change tells Google Playstore app that your application can be installed on
devices with Android 2.1 (API level 7) and higher.
API Version
Note: If you are including the v4 support and v7 support libraries in your
application, you should specify a minimum SDK version of "7" (and not "4"). The
highest support library level you include in your application determines the
lowest API version in which it can operate.
708
70. TESTING
Android
The Android framework includes an integrated testing framework that help you
test all aspects of your application. The SDK tools include tools for setting up
and running test applications. Whether you are working in Eclipse with ADT or
working from the command line, the SDK tools help you set up and run your
tests within an emulator or the device you are targeting.
Test Structure
Android's build and test tools assume that test projects are organized into a
standard structure of tests, test case classes, test packages, and test projects.
Android
test android applications. In this chapter we are going to explain these two tools
to test android applications.
JUnit
Monkey
JUnit
You can use the JUnit TestCase class to do unit testing on a class that doesn't
call Android APIs. TestCase is also the base class for AndroidTestCase, which you
can use to test Android-dependent objects. Besides providing the JUnit
framework, AndroidTestCase offers Android-specific setup, teardown, and helper
methods.
To use TestCase, extend your class with TestCase class and implement a method
call setUp(). Its syntax is given below:
public class MathTest extends TestCase {
protected double fValue1;
protected double fValue2;
Android
Monkey
The UI/Application Exerciser Monkey, usually called "monkey", is a commandline tool that sends pseudo-random streams of keystrokes, touches, and
gestures to a device. You run it with the Android Debug Bridge (adb) tool.
You use it to stress-test your application and report back errors that are
encountered. You can repeat a stream of events by running the tool each time
with the same random number seed.
Monkey features
Monkey has many features, but it can all be summed up to these four
categories.
Operational constraints
Debugging options
Monkey Usage
In order to use monkey, open up a command prompt and just naviagte to the
following directory.
android->sdk->platform-tools
Once inside the directory, attach your device with the PC, and run the following
command:
adb shell monkey -v 100
This command can be broken down into these steps:
adb - Android Debug Bridge. A tool used to connect and send commands
to your Android phone from a desktop or laptop computer.
711
Android
Here, you run the monkey tool on the default android UI application. Now in
order to run it to your application, here is what you have to do.
First run the example code given in the example section in your device. After
running, follow the steps of monkey usage and finally type this command.
adb shell monkey -p com.example.test -v 500
This has also been shown in the figure below. By typing this command, you are
actually generating 500 random events for testing.
Example
The below example demonstrates the use of Testing. It creates a basic
application which can be used for monkey.
To experiment with this example, you need to run this on an actual device and
then follow the monkey steps explained in the beginning.
712
Android
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Test under a package com.example.test. While creating this
project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify res/values/string.xml
components.
Run the application and choose a running android device and install
the application on it and verify the results.
file
and
add
necessary
string
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
713
Android
setContentView(R.layout.activity_main);
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
}
714
Android
}
Here is the content of activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="87dp"
android:text="@string/test1"
715
Android
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="activity2"
android:text="@string/go2" />
</RelativeLayout>
Here is the content of activity_main_activity2.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity2" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="125dp"
android:text="@string/test2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
716
Android
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="activity1"
android:text="@string/go1" />
</RelativeLayout>
Here is the content of Strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">test</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="test1">This is activity 1</string>
<string name="test2">This is activity 2</string>
<string name="go1">Go to activity 1</string>
<string name="go2">Go to activity 2</string>
<string name="title_activity_main_activity2">MainActivity2</string>
</resources>
Here is the content of AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
717
Android
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
718
Android
Select your mobile device as an option and then check your mobile device which
will display application screen. Now just follow the steps mentioned at the top
under the monkey section in order to peform testing on this application.
719
Android
Android allows you to convert your text into voice. Not only you can convert it,
but it also allows you to speak text in variety of different languages.
Android provides TextToSpeech class for this purpose. To use this class, you
need to instantiate an object of this class and also specify the initListener. Its
syntax is given below:
private EditText write;
ttobj=new TextToSpeech(getApplicationContext(), new
TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
}
}
);
In this listener, you have to specify the properties for TextToSpeech object, such
as
its
language,
pitch
etc.
Language
can
be
set
by
calling setLanguage() method. Its syntax is given below:
ttobj.setLanguage(Locale.UK);
The method setLanguage takes a Locale object as parameter. The list of some of
the locales available are given below:
Sr.No
Locale
US
CANADA_FRENCH
GERMANY
ITALY
JAPAN
720
Android
CHINA
Once you have set the language, you can call speak method of the class to
speak the text. Its syntax is given below:
ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
Apart from the speak method, there are some other methods available in the
TextToSpeech class. They are listed below:
Sr.No
getLanguage()
This method returns a Locale instance describing the language.
isSpeaking()
This method checks whether the TextToSpeech engine is busy
speaking.
setPitch(float pitch)
This method sets the speech pitch for the TextToSpeech engine.
setSpeechRate(float speechRate)
This method sets the speech rate.
shutdown()
This method releases the resources used by the TextToSpeech engine.
stop()
This method stops the speak.
Example
The below example demonstrates the use of TextToSpeech class. It creates a
basic application that allows you to set write text and speak it.
721
Android
To experiment with this example, you need to run this on an actual device.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as TextToSpeech under a package com.example.texttospeech. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify res/values/string.xml
components.
Run the application and choose a running android device and install
the application on it and verify the results.
file
and
add
necessary
string
import java.util.Locale;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
TextToSpeech ttobj;
722
Android
Android
Toast.LENGTH_SHORT).show();
ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
}
Here is the content of activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="188dp"
android:layout_marginRight="67dp"
android:onClick="speakText"
android:text="@string/text1" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
724
Android
android:layout_marginBottom="81dp"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="@string/write"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Here is the content of Strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TextToSpeech</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="text1">Text to Speech</string>
<string name="write">Write Text</string>
</resources>
725
Android
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.texttospeech.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
726
Android
Let's try to run your TextToSpeech application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
727
Android
Select your mobile device as an option and then check your mobile device which
will display the following screen:
728
Android
Now just type some text in the field and click on the text to speech button
below. A notification would appear and text will be spoken. It is shown in the
image below:
729
Android
Now type something else and repeat the step again with different locale. You will
again hear sound. This is shown below:
730
Android
If you want to display a live video stream or any content stream such as video
or an OpenGL scene, you can use TextureView provided by android in order to
do that.
In order to use TextureView, all you need to do is get its SurfaceTexture. The
SurfaceTexture can then be used to render content. To do this, you just need to
do instantiate an object of this class and implement SurfaceTextureListener
interface. Its syntax is given below:
private TextureView myTexture;
public class MainActivity extends Activity implements
SurfaceTextureListener{
protected void onCreate(Bundle savedInstanceState) {
myTexture = new TextureView(this);
myTexture.setSurfaceTextureListener(this);
setContentView(myTexture);
}
}
After that, what you need to do is to override its methods. The methods are
listed as follows:
@Override
public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1, int
arg2) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,int
arg2) {
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
}
731
Android
Any view that is displayed in the texture view can be rotated and its alpha
property can be adjusted by using setAlpha and setRotation methods. Its
syntax is given below:
myTexture.setAlpha(1.0f);
myTexture.setRotation(90.0f);
Apart from these methods, there are other methods available in TextureView
class. They are listed below:
Sr.No
getSurfaceTexture()
This method returns the SurfaceTexture used by this view.
getTransform(Matrix transform)
This method returns the transform associated with this texture view.
isOpaque()
This method indicates whether this View is opaque.
lockCanvas()
This method starts editing the pixels in the surface.
setOpaque(boolean opaque)
This method indicates whether the content of this TextureView is
opaque.
setTransform(Matrix transform)
This method sets the transform to associate with this texture view.
unlockCanvasAndPost(Canvas canvas)
This method finishes editing pixels in the surface.
732
Android
Example
The below example demonstrates the use of TextureView class. It creates a
basic application that allows you to view camera inside a texture view and
change its angle, orientation etc.
To experiment with this example, you need to run this on an actual device on
which camera is present.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as TextureView under a package com.example.textureview. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Run the application and choose a running android device and install
the application on it and verify the results.
import java.io.IOException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
733
Android
import android.widget.FrameLayout;
public
class
MainActivity
SurfaceTextureListener {
extends
Activity
implements
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTexture = new TextureView(this);
myTexture.setSurfaceTextureListener(this);
setContentView(myTexture);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressLint("NewApi")
@Override
public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1,
int arg2) {
mCamera = Camera.open();
Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
myTexture.setLayoutParams(new FrameLayout.LayoutParams(
previewSize.width, previewSize.height, Gravity.CENTER));
try {
mCamera.setPreviewTexture(arg0);
734
Android
} catch (IOException t) {
}
mCamera.startPreview();
myTexture.setAlpha(1.0f);
myTexture.setRotation(90.0f);
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
mCamera.stopPreview();
mCamera.release();
return true;
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
// TODO Auto-generated method stub
}
}
Here is the content of activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
735
Android
tools:context=".MainActivity" >
<TextureView
android:id="@+id/textureView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Here is the default content of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.textureview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.textureview.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Android
</activity>
</application>
</manifest>
Let's try to run your TextureView application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
737
Android
Select your mobile device as an option and then check your mobile device which
will display following screen. This screen has alpha property set to 0.5 and
rotation set to 45.
738
Android
This screen has alpha property set to 1.5 and rotation set to 45.
739
Android
This screen has alpha property set to 1.0 and rotation set to 90.
740
Android
Android allows your application to connect to twitter and share data or any kind
of updates on twitter. This chapter is about integrating twitter into your
application.
There are two ways through which you can integrate twitter and share
something from your application. These ways are listed below:
Intent Share
at dev.twitter.com/apps/new and
fill
all
the
741
Android
Now under setttings tab, change the access to read, write and access messages
and save the settings. It is shown below:
742
Android
If everything works fine, you will receive a consumer ID with the secret. Just
copy the application id and save it somewhere. It is shown in the image below:
Intent share
Intent share is used to share data between applications. In this strategy, we will
not handle the SDK stuff, but let the twitter application handles it. We will simply
call the twitter application and pass the data to share. This way, we can share
something on twitter.
743
Android
Android provides intent library to share data between activities and applications.
To use it as share intent, we have to specify the type of the share intent
to ACTION_SEND. Its syntax is given below:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Next thing you need is to define the type of data to pass, and then pass the
data. Its syntax is given below:
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, from tutorialspoint");
startActivity(Intent.createChooser(shareIntent, "Share your thoughts"));
Apart from these methods, there are other methods available that allows intent
handling. They are listed below:
Sr.No
addCategory(String category)
This method adds a new category to the intent.
getAction()
This method retrieves the general action to be performed, such as
ACTION_VIEW.
getCategories()
This method returns the set of all categories in the intent.nt and the
current scaling event.
toString()
This method returns a string containing a concise, human-readable
description of this object.
744
Android
Example
Here is an example demonstrating the use of IntentShare to share data on
twitter. It creates a basic application that allows you to share some text on
twitter.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as IntentShare under a package com.example.intentshare. While
creating this project, make sure you Target SDK and Compile With at
the latest version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
Following
is
the
content
of
the
modified
src/com.example.intentshare/MainActivity.java.
add
respective
main
activity
XML
file
package com.example.intentshare;
import java.io.File;
import java.io.FileOutputStream;
import com.example.intentshare.R;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
745
Android
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void open(View view){
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, from
tutorialspoint");
startActivity(Intent.createChooser(shareIntent, "Share your
thoughts"));
746
Android
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="98dp"
android:layout_marginTop="139dp"
android:onClick="open"
android:src="@drawable/tp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="@string/tap"
747
Android
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">IntentShare</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="tap">Tap the button to share something</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentshare"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.intentshare.MainActivity"
android:label="@string/app_name" >
748
Android
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Let's try to run your IntentShare application. We assume, you have connected
your actual Android Mobile device with your computer. To run the app from
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
749
Android
Select your mobile device as an option and then check your mobile device which
will display your default screen:
750
Android
Now just tap on the image logo and you will see a list of share providers.
751
Android
Now just select twitter from that list and then write any message. It is shown in
the image below:
Now just select the tweet button and then it would be posted on your twitter
page. It is shown below:
752
74. UI DESIGN
Android
UI screen components
A typical user interface of an android application consists of action bar and the
application content area.
View Control
Content Area
753
Android
Types of layout
There are many types of layout. Some of which are listed below:
Linear Layout
Absolute Layout
Table Layout
Frame Layout
Relative Layout
Linear Layout
Linear layout is further divided into horizontal and vertical layout. It means it
can arrange views in a single column or in a single row. Here is the code of
linear layout (vertical) that includes a text view.
<?xml version=1.0 encoding=utf-8?>
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=fill_parent
android:layout_height=fill_parent
android:orientation=vertical >
<TextView
android:layout_width=fill_parent
android:layout_height=wrap_content
android:text=@string/hello />
</LinearLayout>
754
Android
AbsoluteLayout
The AbsoluteLayout enables you to specify the exact location of its children. It
can be declared like this.
<AbsoluteLayout
android:layout_width=fill_parent
android:layout_height=fill_parent
xmlns:android=http://schemas.android.com/apk/res/android >
<Button
android:layout_width=188dp
android:layout_height=wrap_content
android:text=Button
android:layout_x=126px
android:layout_y=361px />
</AbsoluteLayout>
TableLayout
The TableLayout groups views into rows and columns. It can be declared like
this.
<TableLayout
xmlns:android=http://schemas.android.com/apk/res/android
android:layout_height=fill_parent
android:layout_width=fill_parent >
<TableRow>
<TextView
android:text=User Name:
android:width =120dp
/>
<EditText
android:id=@+id/txtUserName
android:width=200dp />
</TableRow>
</TableLayout>
755
Android
RelativeLayout
The RelativeLayout enables you to specify how child views are positioned relative
to each other. It can be declared like this.
<RelativeLayout
android:id=@+id/RLayout
android:layout_width=fill_parent
android:layout_height=fill_parent
xmlns:android=http://schemas.android.com/apk/res/android >
</RelativeLayout>
FrameLayout
The FrameLayout is a placeholder on screen that you can use to display a single
view. It can be declared like this.
<?xml version=1.0 encoding=utf-8?>
<FrameLayout
android:layout_width=wrap_content
android:layout_height=wrap_content
android:layout_alignLeft=@+id/lblComments
android:layout_below=@+id/lblComments
android:layout_centerHorizontal=true >
<ImageView
android:src = @drawable/droid
android:layout_width=wrap_content
android:layout_height=wrap_content />
</FrameLayout>
Apart from these attributes, there are other attributes that are common in all
views and ViewGroups. They are listed below:
Sr.No
layout_width
Specifies the width of the View or ViewGroup.
layout_height
756
Android
layout_marginTop
Specifies extra space on the top side of the View or ViewGroup.
layout_marginBottom
Specifies extra space on the bottom side of the View or ViewGroup.
layout_marginLeft
Specifies extra space on the left side of the View or ViewGroup.
layout_marginRight
Specifies extra space on the right side of the View or ViewGroup.
layout_gravity
Specifies how child Views are positioned.
layout_weight
Specifies how much of the extra space in the layout should be
allocated to the View.
Units of Measurement
When you are specifying the size of an element on an Android UI, you should
remember the following units of measurement.
Sr.No
dp
Density-independent pixel. 1 dp is equivalent to one pixel on a 160 dpi
screen.
sp
Scale-independent pixel. This is similar to dp and is recommended for
specifying font sizes.
pt
Point. A point is defined to be 1/72 of an inch, based on the physical
screen size.
757
Android
px
Pixel. Corresponds to actual pixels on the screen.
Screen Densities
Sr.No
Optimizing layouts
Here are some of the guidelines for creating efficient layouts.
758
75. UI PATTERNS
Android
This chapter tells you the different UI Patterns which are available by android to
design apps that behave in a consistent and forseeable way.
UI Patterns components
A good android application should follow following UI patterns:
Action Bar
Settings
Help
Selection
Action Bar
The action bar is a dedicated bar at the top of each screen that is generally
persistent througout the app. It provides you several key function which are as
following:
759
Android
App Icon
The app icon establishes your app's identity. It can be replaced with a
different logo or branding if you wish.
View control
If your app displays data in different views, this segment of the action
bar allows users to switch views.
Action buttons
Shows the most important actions of your app in the actions section.
Action overflow
Moves less often used actions to the action overflow.
Confirming
When we ask the user to verify that they truly want to proceed with an action
that they just invoked, it is called confirming. As you can see in the following
image:
760
Android
Acknowledging
When we display a toast to let the user know that the action they just invoked
has been completed, is called acknowledging, as you can see in the following
image:
Settings
The place in your app where users can indicate their preferences for how your
app should behave is called as Settings. The use of settings can benefit your
app's users in the following ways:
Placement of Settings
It is preferred by the android developers to always make "settings" option part
of action overflow which is mentioned above. As users did not frequently use this
options so the common practice is to place it below all other items except
"Help". As you can see in the following picture:
Help
Some of your app users may run into some difficulty while using your app and
they will be looking for some answers which they want within the app. So always
make "help" a part of your app.
761
Android
Placement of Help
Like "Settings" the standard design of placing "Help" option is in action
overflow. Always make it very last item in the menu and always label it "Help".
Even if your app screen has no other action overflow items, "Help" should
appear there. As you can see this in the following picture:
Selection
Android 3.0 version changed the long press gesture to the global gesture to
select data. The long press gesture is now used to select data, combining
contextual actions and selection management functions for selected data into a
new element called the contextual action bar (CAB).
Triggers an action from the CAB that applies to all highlighted data items.
Dismiss the CAB via the navigation bar's Back button or the CAB's
checkmark button.
762
76. UI TESTING
Android
uiautomatorviewer
uiautomator
uiautomatorviewer
A GUI tool to scan and analyze the UI components of an Android application.
The uiautomatorviewer tool provides a convenient visual interface to inspect the
layout hierarchy and view the properties of the individual UI components that
are displayed on the test device. Using this information, you can later create
uiautomator tests with selector objects that target specific UI components to
test.
To analyze the UI components of the application that you want to test, perform
the following steps after installing the application given in the example.
763
Android
You will see the following window appear. It is the default window of the UI
Automator Viewer.
Click on the devices icon at the top right corner. It will start taking the UI
XML snapshot of the screen currently opened in the device. It would be
something like this.
764
Android
the
snapshot
of
your
device
screen
in
the
On the right side of this window, you will see two partitions. The upper partition
explains the Nodes structure, the way the UI components are arranged and
contained. Clicking on each node gives detail in the lower partition.
As an example, consider the below figure. When you click on the button, you can
see in the upper partition that Button is selected, and in the lower partition, its
details are shown. Since this button is clickable, its property of clickable is set to
true.
765
Android
766
Android
uiautomator
Now you can create your own test cases and run it with uiautomatorviewer to
examine them. In order to create your own test case, you need to perform the
following steps:
From the Project Explorer, right-click on the new project that you created,
then select Properties > Java Build Path, and do the following:
Click Add Library > JUnit then select JUnit3 to add JUnit support.
Click Add External JARs... and navigate to the SDK directory. Under the
platforms directory, select the latest SDK version and add both the
uiautomator.jar and android.jar files.
Once you have coded your test, follow these steps to build and deploy
your test JAR to your target Android test device.
Create the required build configuration files to build the output JAR. To
generate the build configuration files, open a terminal and run the
following command:
<android-sdk>/tools/android create uitest-project -n <name> -t 1 -p
<path>
<name> is the name of the project that contains your uiautomator
test source files, and <path> is the path to the corresponding project
directory.
Go to the project directory where your build.xml file is located and build
your test JAR.
ant build
Deploy your generated test JAR file to the test device by using the adb
push command.
adb push
/data/local/tmp/
Android
Example
The below example demonstrates the use of UITesting. It creates a basic
application which can be used for uiautomatorviewer.
To experiment with this example, you need to run this on an actual device and
then follow the uiautomatorviewer steps explained in the beginning.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Test under a package com.example.test. While creating this
project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify res/values/string.xml
components.
Run the application and choose a running android device and install
the application on it and verify the results.
file
and
add
necessary
string
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
768
Android
import android.view.Menu;
import android.view.View;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Here is the content of src/com.example.test/MainActivity2.java.
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
769
Android
import android.view.View;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
}
}
Here is the content of activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
770
Android
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="87dp"
android:text="@string/test1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="activity2"
android:text="@string/go2" />
</RelativeLayout>
Here is the content of activity_main_activity2.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity2" >
771
Android
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="125dp"
android:text="@string/test2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="activity1"
android:text="@string/go1" />
</RelativeLayout>
Here is the content of Strings.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">test</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="test1">This is activity 1</string>
<string name="test2">This is activity 2</string>
<string name="go1">Go to activity 1</string>
<string name="go2">Go to activity 2</string>
<string name="title_activity_main_activity2">MainActivity2</string>
</resources>
772
Android
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Android
Eclipse, open one of your project's activity files and click Run
icon from the
toolbar. Before starting your application, Eclipse will display following window to
select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which
will display application screen. Now just follow the steps mentioned at the top
under the uiautomatorviewer section in order to perform uitesting on this
application.
774
77. WEBVIEW
Android
WebView is a view that display web pages inside your application. You can also
specify HTML string and can show it inside your application using WebView.
WebView turns your application to a web application.
In
order
to
add
WebView
to
your
application,
you
have
add <WebView> element to your xml layout file. Its syntax is as follows:
<WebView
to
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
In order to use it, you have to get a reference of this view in Java file. To get a
reference, create an object of the class WebView. Its syntax is:
WebView browser = (WebView) findViewById(R.id.webview);
In order to load a web url into the WebView, you need to call a
method loadUrl(String url) of the WebView class, specifying the required url.
Its syntax is:
browser.loadUrl("http://www.tutorialspoint.com");
Apart from just loading url, you can have more control over your WebView by
using the methods defined in WebView class. They are listed as follows:
Sr.No
canGoBack()
This method specifies whether the WebView has a back history item.
canGoForward()
This method specifies whether the WebView has a forward history
item.
clearHistory()
775
Android
destroy()
This method destorys the internal state of WebView.
findAllAsync(String find)
This method find all instances of string and highlight them.
getProgress()
This method gets the progress of the current page.
getTitle()
This method returns the title of the current page.
getUrl()
This method returns the url of the current page.
If you click on any link inside the webpage of the WebView, that page will not be
loaded inside your WebView. To do that you need to extend your class from
WebViewClient and override its method. Its syntax is:
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Example
Here is an example demonstrating the use of WebView Layout. It creates a basic
web application that will ask you to specify a url and will load this url website in
the WebView.
To experiment with this example, you need to run this on an actual device on
which internet is running.
776
Android
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as WebView under a package com.example.webview. While creating
this project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
Following
is
the
content
of
the
modified
src/com.example.webview/MainActivity.java.
add
respective
main
activity
XML
file
package com.example.webview;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;
777
Android
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
field = (EditText)findViewById(R.id.urlField);
browser = (WebView)findViewById(R.id.webView1);
browser.setWebViewClient(new MyBrowser());
}
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
778
Android
return true;
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/urlField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_centerHorizontal="true"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
779
Android
android:layout_height="wrap_content"
android:layout_below="@+id/urlField"
android:layout_centerHorizontal="true"
android:onClick="open"
android:text="@string/browse" />
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentBottom="true"
android:layout_below="@+id/button1" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WebView</string>
<string name="action_settings">Settings</string>
<string name="hello_world">URL:</string>
<string name="browse">Browse</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webview"
android:versionCode="1"
android:versionName="1.0" >
780
Android
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.webview.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
781
Android
Let's try to run your WebView application. We assume, you have connected your
actual Android Mobile device with your computer. To run the app from Eclipse,
open one of your project's activity files and click Run
icon from the toolbar.
Before starting your application, Eclipse will display following window to select
an option where you want to run your Android application.
782
Android
Select your mobile device as an option and then check your mobile device which
will display your default screen:
783
Android
Now just specify a url on the url field and press the browse button that appears,
to launch the website. But before that please make sure that you are connected
to the internet. After pressing the button, the following screen would appear:
Note: By just changing the url in the url field, your WebView will open your
desired website.
784
78. WI-FI
Android
Android allows applications to view the access of the state of the wireless
connections at a very low level. Application can access almost all the information
of a wi-fi connection.
The information that an application can access includes connected network's link
speed, IP address, negotiation state, other networks information. Applications
can also scan, add, save, terminate and initiate Wi-Fi connections.
Android provides WifiManager API to manage all aspects of WIFI connectivity.
We can instantiate this class by calling getSystemService method. Its syntax is
given below:
WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
In order to scan a list of wireless networks, you also need to register your
BroadcastReceiver. It can be registered using registerReceiver method with
argument of your receiver class object. Its syntax is given below:
class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
}
}
WifiScanReceiver wifiReceiver = new WifiScanReceiver();
registerReceiver(wifiReceiver, new
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
The wi-fi scan can be started by calling the startScan method of the
WifiManager class. This method returns a list of ScanResult objects. You can
access any object by calling the get method of list. Its syntax is given below:
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
String data = wifiScanList.get(0).toString();
785
Android
Apart from just Scanning, you can have more control over your WIFI by using
the methods defined in WifiManager class. They are listed as follows:
Sr.No
addNetwork(WifiConfiguration config)
This method adds a new network description to the set of configured
networks.
createWifiLock(String tag)
This method creates a new WifiLock.
disconnect()
This method disassociates from the currently active access point.
getWifiState()
This method gets the Wi-Fi enabled state.
isWifiEnabled()
This method returns whether Wi-Fi is enabled or disabled.
setWifiEnabled(boolean enabled)
This method enables or disables Wi-Fi.
updateNetwork(WifiConfiguration config)
This method updates the network description of an existing configured
network.
Example
Here is an example demonstrating the use of WIFI. It creates a basic application
that scans a list of wirless networks and populate them in a list view.
786
Android
To experiment with this example, you need to run this on an actual device on
which wi-fi is turned on.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as WIFI under a package com.example.wifi. While creating this
project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
Following
is
the
content
of
the
modified
src/com.example.wifi/MainActivity.java.
add
respective
main
activity
XML
file
package com.example.wifi;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
787
Android
import android.widget.Toast;
WifiManager mainWifiObj;
WifiScanReceiver wifiReceiver;
ListView list;
String wifis[];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.listView1);
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReceiver = new WifiScanReceiver();
mainWifiObj.startScan();
}
Android
list.setAdapter(new
ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,wifis));
}
}
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:drawSelectorOnTop="false"
android:background="@android:color/background_dark"
android:listSelector="@android:color/darker_gray" >
789
Android
</ListView>
</RelativeLayout>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wifi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"
/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.wifi.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Android
android:name="com.example.wifi.ListWifiActivity"
android:label="@string/title_activity_list_wifi" >
</activity>
</application>
</manifest>
Let's try to run your WIFI application. We assume, you have connected your
actual Android Mobile device with your computer. To run the app from Eclipse,
open one of your project's activity files and click Run
icon from the toolbar.
Before starting your application, Eclipse will display following window to select
an option where you want to run your Android application.
791
Android
Select your mobile device as an option and then check your mobile device which
will display your mobile screen filled with wireless networks around you. It is
shown below:
Note the information that has been returned to you. It contains much
information about each of the wireless network detected.
792
79. WIDGETS
Android
Android
onDisabled(Context context)
This is called when the last instance of AppWidgetProvider is deleted
onEnabled(Context context)
This is called when an instance of AppWidgetProvider is created.
Android
Example
Here is an example demonstrating the use of application Widget. It creates a
basic widget applications that will open this current website in the browser.
To experiment with this example, you need to run this on an actual device on
which internet is running.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as Widget under a package com.example.widget. While creating this
project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify the
components.
Run the application and choose a running android device and install
the application on it and verify the results.
res/layout/activity_main
to
Following
is
the
content
of
the
modified
src/com.example.widget/MainActivity.java.
add
respective
main
activity
XML
file
package com.example.widget;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
795
Android
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Toast;
@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager,
int[] appWidgetIds) {
for(int i=0; i<appWidgetIds.length; i++){
int currentWidgetId = appWidgetIds[i];
String url = "http://www.tutorialspoint.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(url));
PendingIntent pending = PendingIntent.getActivity(context, 0,
intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.activity_main);
views.setOnClickPendingIntent(R.id.button1, pending);
appWidgetManager.updateAppWidget(currentWidgetId,views);
Toast.makeText(context, "widget added", Toast.LENGTH_SHORT).show();
}
}
}
796
Android
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/website"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="18dp"
android:text="@string/app_name" />
</RelativeLayout>
797
Android
<string name="app_name">Widget</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="website">TutorialsPoint.com</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.widget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
798
Android
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="MainActivity" >
<intent-filter>
<action
android:name="android.appwidget.action.APPWIDGET_UPDATE"
/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/mywidget" />
</receiver>
</application>
</manifest>
Let's try to run your Widget application. We assume, you have connected your
actual Android Mobile device with your computer. To run the app from Eclipse,
open one of your project's activity files and click Run
icon from the toolbar.
Before starting your application, Eclipse will display following window to select
an option where you want to run your Android application.
799
Android
Select your mobile device as an option and then check your mobile device which
will display your default screen:
Go to your widget section and add your created widget to the dsktop or
homescreen. It would look something like this:
800
Android
Now just tap on the widget button that appears, to launch the browser. But
before that please make sure that you are connected to the internet. After
pressing the button, the following screen would appear:
801
Android
Note. By just changing the url in the java file, your widget will open your desired
website in the browser.
802
Android
XML stands for Extensible Markup Language. XML is a very popular format and
commonly used for sharing data on the internet. This chapter explains how to
parse the XML file and extract necessary information from it.
Android provides three types of XML parsers which are DOM, SAX and
XMLPullParser. Among all of them android recommend XMLPullParser because
it is efficient and easy to use. So we are going to use XMLPullParser for parsing
XML.
The first step is to identify the fields in the XML data in which you are interested
in. For example, in the XML given below we are interested in getting
temperature only.
<?xml version="1.0"?>
<current>
<city id="2643743" name="London">
<coord lon="-0.12574" lat="51.50853"/>
<country>GB</country>
<sun rise="2013-10-08T06:13:56" set="2013-10-08T17:21:45"/>
</city>
<temperature value="289.54" min="289.15" max="290.15" unit="kelvin"/>
<humidity value="77" unit="%"/>
<pressure value="1025" unit="hPa"/>
</country>
XML - Elements
An xml file consist of many components. Here is the table defining the
components of an XML file and their description.
Sr.No
Prolog
An XML file starts with a prolog. The first line that contains the
information about a file is prolog
Events
803
Android
An XML file has many events. Event could be like this. Document
starts, Document ends, Tag starts, Tag ends and Text etc.
3
Text
Apart from tags and events, and xml file also contains simple text.
Such as GB is a text in the country tag.
Attributes
Attributes are the additional properties of a tag such as value etc.
XML - Parsing
In the next step, we will create XMLPullParser object, but in order to create that
we will first create XmlPullParserFactory object and then call its newPullParser()
method to create XMLPullParser. Its syntax is given below:
private XmlPullParserFactory xmlFactoryObject =
XmlPullParserFactory.newInstance();
private XmlPullParser myparser = xmlFactoryObject.newPullParser();
The next step involves specifying the file for XmlPullParser that contains XML. It
could be a file or could be a Stream. In our case it is a stream. Its syntax is
given below:
myparser.setInput(stream, null);
The last step is to parse the XML. An XML file consist of events, Name, Text,
AttributesValue etc. So XMLPullParser has a seperate function for parsing each of
the component of XML file. Its syntax is given below:
int event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT)
{
String name=myParser.getName();
switch (event){
case XmlPullParser.START_TAG:
break;
case XmlPullParser.END_TAG:
if(name.equals("temperature")){
temperature = myParser.getAttributeValue(null,"value");
804
Android
}
break;
}
event = myParser.next();
}
The method getEventType returns the type of event that happens. e.g:
Document start, tag start etc. The method getName returns the name of the
tag and since we are only interested in temperature, so we just check in
conditional statement that if we get a temperature tag, we call the
method getAttributeValue to return us the value of temperature tag.
Apart from these methods, there are other methods provided by this class for
better parsing XML files. These methods are listed below:
Sr.No
getAttributeCount()
This method just returns the number of attributes of the current start
tag.
getAttributeName(int index)
This method returns the name of the attribute specified by the index
value.
getColumnNumber()
This method returns the current column number, starting from 0.
getDepth()
This method returns the current depth of the element.
getLineNumber()
Returns the current line number, starting from 1.
getNamespace()
This method returns the namespace URI of the current element.
getPrefix()
805
Android
getName()
This method returns the name of the tag.
getText()
This method returns the text for that particular element.
10
isWhitespace()
This method checks whether the current TEXT event contains only
whitespace characters.
Example
Here is an example demonstrating the use of XMLPullParser class. It creates a
basic Weather application that allows you to parse XML from google weather api
and shows the result.
To experiment with this example, you can run this on an actual device or in an
emulator.
Steps
Description
You will use Eclipse IDE to create an Android application and name it
as XMLParser under a package com.example.xmlparser. While creating
this project, make sure you Target SDK and Compile With at the latest
version of Android SDK to use higher levels of APIs.
Modify the
components.
res/layout/activity_main
to
add
respective
XML
806
Android
Run the application and choose a running android device and install
the application on it and verify the results.
Following
is
the
content
of
the
modified
src/com.example.xmlparser/MainActivity.java.
main
activity
file
package com.example.xmlparser;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
Android
setContentView(R.layout.activity_main);
location = (EditText)findViewById(R.id.editText1);
country = (EditText)findViewById(R.id.editText2);
temperature = (EditText)findViewById(R.id.editText3);
humidity = (EditText)findViewById(R.id.editText4);
pressure = (EditText)findViewById(R.id.editText5);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
// if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
808
Android
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
Android
this.urlString = url;
}
public String getCountry(){
return country;
}
public String getTemperature(){
return temperature;
}
public String getHumidity(){
return humidity;
}
public String getPressure(){
return pressure;
}
case XmlPullParser.END_TAG:
if(name.equals("country")){
country = text;
}
else if(name.equals("humidity")){
810
Android
humidity = myParser.getAttributeValue(null,"value");
}
else if(name.equals("pressure")){
pressure = myParser.getAttributeValue(null,"value");
}
else if(name.equals("temperature")){
temperature =
myParser.getAttributeValue(null,"value");
}
else{
}
break;
}
event = myParser.next();
}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void fetchXML(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
811
Android
conn.connect();
InputStream stream = conn.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES
, false);
myparser.setInput(stream, null);
parseXMLAndStoreIt(myparser);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
Following is the modified content of the xml res/layout/activity_main.xml.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
812
Android
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="@string/location"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:ems="10" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="68dp"
android:text="@string/country"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
813
Android
android:layout_marginTop="19dp"
android:text="@string/temperature"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="32dp"
android:text="@string/humidity"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="21dp"
android:text="@string/pressure"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:ems="10" >
<requestFocus />
</EditText>
814
Android
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/editText2"
android:ems="10" />
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView5"
android:layout_alignLeft="@+id/editText1"
android:ems="10" />
<EditText
android:id="@+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_alignRight="@+id/editText4"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText1"
android:onClick="open"
815
Android
android:text="@string/weather" />
</RelativeLayout>
Following is the content of the res/values/string.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">XMLParser</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="location">Location</string>
<string name="country">Country:</string>
<string name="temperature">Temperature:</string>
<string name="humidity">Humidity:</string>
<string name="pressure">Pressure:</string>
<string name="weather">Weather</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xmlparser"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
816
Android
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.xmlparser.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
817
Android
Now you need to enter any location in the location field. For example, we have
entered London. Press the weather button, when you enter the location. The
following screen would appear in you AVD:
818
Android
Now when you press the weather button, the application will contact the Google
Weather API and will request for your necessary XML location file and will parse
it. In case of London following file would be returned:
819
Android
820