23
Sample Copy. Not For Distribution.

Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

  • Upload
    vutuyen

  • View
    219

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Sample Copy. Not For Distribution.

Page 2: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

i

Android App Development

A Complete Tutorial For Beginners

Sample Copy. Not For Distribution.

Page 3: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

ii

Publishing-in-support-of,

EDUCREATION PUBLISHING

RZ 94, Sector - 6, Dwarka, New Delhi - 110075

Shubham Vihar, Mangla, Bilaspur, Chhattisgarh - 495001

Website: www.educreation.in

________________________________________________________________

© Copyright, Author

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted, in any form by any means, electronic, mechanical, magnetic, optical, chemical, manual, photocopying, recording or otherwise, without the prior written consent of its writer.

ISBN: 978-1-61813-396-0

Price: ` 454.00

The opinions/ contents expressed in this book are solely of the author and do not represent the opinions/ standings/ thoughts of Educreation. The book is released by using the services of self-publishing house.

Printed in India

Sample Copy. Not For Distribution.

Page 4: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

iii

Android App Development A Complete Tutorial For Begginners

By

Padmini

EDUCREATION PUBLISHING (Since 2011)

www.educreation.in

Sample Copy. Not For Distribution.

Page 5: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

iv

Dedication

This is for you, Mumma. Thanks for always being there for me. Today I

am capable of writing this book just because of your support, the

blessings that you showered on me, the struggles that you faced for me,

or the sacrifices you did for my comfort. Thank you for being my

mother. And in my every birth I want to be your lovable daughter. Love

you Mumma, and I know that no one could ever love me like u do.

Sample Copy. Not For Distribution.

Page 6: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

v

Acknowledgement I would like to express my special thanks of gratitude to my mentor

Mr. Vishwajeet Mishra, as this book would have never been completed

without his guidance.

I would also like to thank my family and friends who supported me a lot

in the completion of this book.

Sample Copy. Not For Distribution.

Page 7: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

vi

Content List

Content Page no.

Unit 1 ix

Introducing Android 1

What is Android 1

Features of Android 1

History of Android 2

Android Versions 2

Android Architecture 3

Core Building Blocks of Android 5

Android Emulator 7

How to make First App 10

Internal Details of MyFirstApp Example 13

What is DVM 19

Displaying Toast 23

Working with Buttons 27

Handling Events in Android 32

GUI Components in Android 44

ToggleButton 44

ImageButton 49

RadioButton 53

TextView 58

EditText 62

AutoCompleteTextView 67

CheckBox 70

Exercise 1

78

Sample Copy. Not For Distribution.

Page 8: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

vii

Unit 2 91

Android Activity Lifecycle 92

Logcat 94

Android Intent 102

Types of Android Intent 106

Implicit Intent 107

Explicit Intent 109

Send And Receive Data 116

Returning Data From an Intent 119

Intent Filters 123

Exercise2 131

Exercise3 134

Layouts in Android 138

LinearLayout 138

RelativeLayout 141

TableLayout 143

AbsoluteLayout 145

Dialog Boxes in Android 146

Alert Dialog 146

DatePicker 148

TimePicker 151

ListView 154

SpinnerView 158

Unit 3 162

Notification in Android 164

Menus in Android 171

Option Menu 171

Context Menu 175

BroadcastReceiver 178

Service 186

Sample Copy. Not For Distribution.

Page 9: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

viii

Notification with Alarm 194

Working With the Database 200

Define the Schema 201

Create the Database 201

Execute the Queries 203

Exercise 4 217

Fragments in Android 222

What is a Fragment 222

Why do we need Fragments 223

How to make Fragments 223

Using XML 224

Using Java 227

Camera 234

Project 239

Sample Copy. Not For Distribution.

Page 10: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

ix

Unit 1 Introducing Android

What is Android

Features of Android

History of Android

Android Versions

Android Architecture

Core Building Blocks of Android

Android Emulator

How to make First App

Internal Details of MyFirstApp Example

What is DVM

Displaying Toast

Working with Buttons

Handling Events in Android

GUI Components in Android

ToggleButton

ImageButton

RadioButton

TextView

EditText

AutoCompleteTextView

CheckBox

Exercise1

Sample Copy. Not For Distribution.

Page 11: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

x

Sample Copy. Not For Distribution.

Page 12: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Android App Development

1

INTRODUCING ANDROID

__________________________________________________________

WHAT IS ANDROID?

Android is an open source software package and linux based operating

system for mobile devices such as tablets, computers and smartphones.

It is developed by Google and later by the OHA (Open Handset

Alliance). Mainly Java language is used to write the android code, even

though other languages can also be used.

The objective of android project is to create a successful real-world

product that enhances the mobile experience for end users.

FEATURES OF ANDROID

After learning what is android, now let's go through the features of

android. The important features of android are :

1) It is open-source.

2) It supports Web Browser based on open-source Blink (Previously

Webkit) layout engine, coupled with Chrome’s V8 JavaScript engine.

3) It provides messaging service in forms of SMS and MMS, including

threaded text Messaging and Android Cloud To Device

Messaging(C2DM).

4) It supports multi-touch screens.

5) It supports multi-tasking applications.

1

Sample Copy. Not For Distribution.

Page 13: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Padmini

2

6) It also provides many other interesting features like weather details,

live RSS (Really Simple Syndication) feeds etc.

HISTORY OF ANDROID

The history and versions of android are quite interesting to know. The

code names of android ranges from A to M currently, such as Aestro,

Blender, Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb,

Ice Cream Sandwitch, Jelly Bean, KitKat, Lollipop and

Marshmallow. Let's go through the android history in a sequence.

1) Initially, Andy Rubin founded Android Incorporation in Palo Alto,

California, United States in October, 2003.

2) Google acquired android Incorporation, on 17th August 2005. Since

then, it is in the subsidiary of Google Incorporation.

3) The key persons of Android Incorporation are Andy Rubin, Rich

Miner, Chris White and Nick Sears.

4) Originally planned for camera but shifted to smart phones later only

because of low market for camera .

5) Android is the nick name of Andy Rubin given by coworkers because

of his passion for robots.

6) Google announced the development of android OS in 2007.

7) HTC launched the first android mobile in 2008.

ANDROID VERSIONS

Let's go through the android versions, codenames , initial release date

and API Level.

Version Code name Initial release date API Level

1.0 23 Sep’2008 1

1.1 9 Feb’2009 2

1.5 Cupcake 27 April’2009 3

1.6 Donut 15 Sep’ 2009 4

Sample Copy. Not For Distribution.

Page 14: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Android App Development

3

2.0 - 2.1 Éclair 26 Oct’2009 5 - 7

2.2 - 2.2.3 Froyo 20 May’2010 8

2.3 – 2.3.7 Gingerbread 6 Dec’2010 9 and 10

3.0 - 3.2.6 Honeycomb 22 Feb’ 2011 11 and 13

4.0 – 4.0.4 Ice Cream

Sandwitch

18 Oct’ 2011 14 and 15

4.1 – 4.3.1 Jelly Bean 9 July’ 2012 16 - 18

4.4 – 4.4.4

,4.4W -

4.4W2

KitKat 31 Oct’ 2013 19 - 20

5.0 – 5.1.1 Lollipop 12 Nov’ 2014 21 - 22

6.0 – 6.0.1 Marshmallow 5 Oct’ 2015 23

ANDROID ARCHITECURE

Android architecture is categorized into five parts:

1. linux kernel

2. native libraries (middleware),

3. Android Runtime

4. Application Framework

5. Applications

Sample Copy. Not For Distribution.

Page 15: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Padmini

4

Let's see the android architecture.

1) Linux kernel

It is the heart of android architecture that exists at the root of android

architecture and contains all the low-level device drivers for the various

hardware components of an Android device. Linux kernel is also

responsible for device drivers, power management, memory

management, device management and resource access.

2) Native Libraries

Native libraries such as WebKit, OpenGL, FreeType, SQLite, Media, C

runtime library (libc) etc, are on the top of linux kernel.

The WebKit is responsible for browser support, SQLite is for database,

FreeType for font support, Media for playing and recording audio and

video formats.

3) Android Runtime

Sample Copy. Not For Distribution.

Page 16: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Android App Development

5

There are core libraries and DVM (Dalvik Virtual Machine) in Android

Runtime, which is responsible to run android application. DVM is just

like JVM but it is optimized for mobile devices. It consumes less

memory and provides fast performance.

4) Android Framework

There is android framework on the top of Native libraries and android

runtime. Android framework constitutes of Android API's such as UI

(User Interface), telephony, resources, locations, Content Providers

(data) and package managers. It provides a lot of interfaces and classes

for android application development.

5) Applications

There are applications on the top of android framework. All applications

like home, contact, settings, games, browsers are using android

framework that uses android runtime and libraries. Android runtime and

native libraries use linux kernal.

CORE BUILDING BLOCKS OF ANDROID

An android component is just a piece of code that has a well defined life

cycle e.g. Activity, Receiver, Service etc.

Sample Copy. Not For Distribution.

Page 17: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Padmini

6

The fundamental components or core building blocks of android are

activities, views, intents, services, content providers, fragments and

AndroidManifest.xml.

Activity

An activity is like a Frame in AWT that represents a single screen with

a user interface. It is the subclass of ContextThemeWrapper class.

View

A view is just the UI element such as button, label, text field etc.

Anything that you see is a view.

Intent

Intent is responsible to invoke components. It is mainly used to:

Launch an activity

Display a web page

Display a list of contacts

Broadcast a message

Start the service

Dial a phone call etc.

Service

An application component that perform long-running operations in the

background and does not provide a user interface is known as Service in

android.

There are two types of services in android, local and remote. Local

service can be accessed from within the application whereas remote

service is accessed remotely from other applications running on the same

device.

__________________________________________________________

Content Provider

Content Providers is used to manage access to a central repository of

data.

Fragment

Fragments are like sub-activity. It has its own layout and behavior An

activity can display one or more fragments on the screen at the same

time.

Sample Copy. Not For Distribution.

Page 18: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Android App Development

7

AndroidManifest.xml

It contains essential information about activities, content providers,

permissions etc. It is like the web.xml file in Java EE.

Android Virtual Device (AVD)

It is used to test the android application without using mobile or tablet

etc. It can be created in different configurations to emulate different

types of real devices.

ANDROID EMULATOR

Android Emulator is responsible for running, debugging and testing

the android application. If you don't have mobile or tablet, it can be the

best way to run, debug and test your application.

It uses an open source processor emulator technology called QEMU.

The emulator tool helps to start the emulator from the command line.

You need to write:

emulator -avd <AVD NAME>

In the case of Eclipse IDE, you can create AVD by Window menu >

AVD Manager > as shown in the figure below:

Sample Copy. Not For Distribution.

Page 19: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Padmini

8

After clicking on the “Android Virtual Device Manager” a dialog box

will appear . Click New . After clicking on New another dialog box will

appear as shown in the figure below.

Install Android

Android supports c#, c++, java, etc. language for developing android

applications. Officially, Java is the supported language for android. All

the examples of this book is developed using Java language and Eclipse

IDE.

Here, we are going to discuss the required softwares to develop android

applications using Eclipse IDE.

We can install android in two ways.

1. By ADT Bundle

2. By Setup Eclipse Manually

1) By ADT Bundle

This is the simplest technique to install required softwares for android

application. It includes:

Eclipse IDE

Android SDK

Eclipse Plugin

If you have download the ADT from the android site, you don't need to

have eclipse IDE, android SDK and eclipse Plugin because it is already

included in adt bundle.

After downloading the ADT bundle, unjar it, go to eclipse IDE and start

the eclipse by clicking on the eclipse icon. You don't need to do any extra

steps here.

How to setup Android for Eclipse IDE

Now, you will learn what are the softwares that are required for running

an android application on eclipse IDE. Here, you will learn how to install

the android SDK and ADT plugin for Eclipse IDE. Let's see the list of

softwares that are required to setup android for eclipse IDE manually.

Installing the JDK

Download and install the Eclipse to develop android application

Download and Install the android SDK

Download the ADT plugin for eclipse

Configure the ADT plugin

Create the AVD

Create the myFirstApp android application

Sample Copy. Not For Distribution.

Page 20: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Android App Development

9

1) Install the Java Development Kit (JDK)

If you are developing the android application with Java language, JDK

must be installed for creating android application.

2) Download and install the Eclipse IDE

To develop the android application using eclipse IDE, you need to install

the Eclipse. Eclipse classic version is recommended but we will be using

the Eclipse IDE for JavaEE Developers.

3) Download and install the android SDK

First , download the android SDK. In this example we have installed the

android SDK for windows (.exe version).

Now double click on the exe file, it will be installed.

__________________________________________________________

4) Download the ADT plugin for eclipse

In the eclipse IDE, ADT (Android Development Tools) is required for

developing the android application. It is the plugin for Eclipse IDE that is

designed to provide the integrated environment.

To download the ADT, you need to follow these steps:

Start the eclipse IDE, then click Help>Install new software...

In the work with box, write :-

https://dl-ssl.google.com/android/eclipse/

select the checkbox next to Developer Tools and click on next

You will see, a list of tools to be downloaded here, click next

click finish

After completing the installation, restart the eclipse IDE

__________________________________________________________

5) Configuring the ADT plugin

Once the installation is completed, tell the eclipse IDE for your android

SDK location. To do so:

Click the Window menu > preferences

Select the android from the left panel. Here you may see a dialog box

asking if you want to send the statistics to the google. Click proceed.

Click on the browse button and then locate your SDK directory e.g.

my SDK location is C:\Program Files\Android\android-sdk .

Click on the apply button then OK.

6) Create an Android Virtual Device (AVD)

Sample Copy. Not For Distribution.

Page 21: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Padmini

10

To run the android application in the Android Emulator, you need to

create an AVD. For creating the AVD:

Click on the Window menu > AVD Manager

Click on the new button, to create the AVD

Now a dialog will appear, write the AVD name e.g. firstAVD. Then

choose the target android version e.g. android4.2.2.

click the ok

7) create and run the simple android example

HOW TO MAKE MYFIRSTAPP

Now , you will learn how to create the simple android application. We

will create the simple example of android using the Eclipse IDE. For

creating the simple example:

Create the new android project

Run the android application

__________________________________________________________

Sample Copy. Not For Distribution.

Page 22: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Android App Development

11

Get Complete book At Educreation Store

www.educreation.in

Sample Copy. Not For Distribution.

Page 23: Sample Copy. Not For Distribution. - Book Publishing House · A Complete Tutorial For Beginners Sample Copy. Not For Distribution. ii Publishing-in-support-of, ... BroadcastReceiver

Sample Copy. Not For Distribution.