83
ANDROID Beginners Workshop Nitin R Patel Purvik N Rana

Android Workshop_1

Embed Size (px)

Citation preview

ANDROID Beginners Workshop

Nitin R Patel

Purvik N Rana

Outline

Era of Mobile OS

Android • What it is..!

• Software stack

• Components

• App fundamentals

Development Environments • Tools/Setups

Application Structure

Layouts and View

Activity

Fragments

Services

ERA OF MOBILE OS

Symbian

• low-end phones

Android

• Sep 20th, 2008 (Astro)

Apple iOS

• June 29th, 2007

BlackBerry

• property of Research in Motion (RIM) found in1999

Windows

• Started by Nokia to take his reputation back to forth

BADA

• owns by Samsung

ANDROID OS

What it is..!!

Free – open source OS (platform for app creation)

Linux kernel based OS

Developed by Google and later the Open Handset Alliance (OHA)

Allow to write/manage code in the Java

Core and third party app are equal

Easy and fast app development

Era of Android starts since 5 November 2007

OHA

Open Handset Alliance

Group of technology and mobile companies

First free provided mobile platform is – Android

Providing richer experience (with open standards) main Goal

Handset manufactures are free to pay licencing fees to load

Android

Version History

Version Codename API Distribution

4.0.3 -

4.0.4

Ice Cream

Sandwich 15 3.7%

4.2.x Jelly Bean

17 15.2%

4.3 18 4.5%

4.4 KitKat 19 39.2%

5.0 Lollipop

21 15.9%

5.1 22 5.1%

Native Android Applications

Home Screen

Music Player, Picture Gallery

SMS, E-mail

Camera Apps

Full PIM (Calendar, Contacts)

Web Browsers

Play Store

Sensors

Google Maps

Google Talk

Google Mail Client

YouTube

Data Stores

Much more . . .

Android Software Stack

• Contain all the Low level drivers for various hardware components of Android

• Relaying on Linux Kernel for core system services like • Memory and process management

• Network stack

• Driver model

• Security

• Provide Abstraction between hardware and rest of the software stack

Linux Kernel

• Set of Core libraries – enable developers to write Android Apps using Java Programming

• DVM – Dalvik Virtual Machine

Android Runtime

Dalvik Virtual Machine

Provides environment to let us execute Android application

Each Android application runs in its own process

• Have it’s own instance of the Dalvik VM

Dalvik has been written such that a device can run multiple VMs

efficiently.

Register-based virtual machine

• Executing the Dalvik Executable (.dex) format

• .dex format is optimized for minimal memory footprint.

Compilation

• Relying on the Linux Kernel for: Threading & other Low-level

memory management

Exposed to developers through the Android application framework

Including a set of C/C++ libraries used by components of the Android system

Code for main features

• SQLite library- data storage

• WebKit library – functions for web browsing and much more..

Libraries

Expose various capabilities of Android – developers use them in their Apps

Enabling and simplifying the reuse of components

• Developers have full access to the same framework APIs used by the core applications

• Users are allowed to replace components

Application Framework

Features

Apps that download and install from Android Market

Android provides a set of core applications:

• Email Client

• SMS Program

• Calendar

• Maps

• Browser

• Contacts etc…

Applications

Android Components

Activities

• Single UI that handle the user interaction, app contains one or more activities

Services

• handle background processing for applications, doesn‟t have UI

Broadcast Receivers

• handle communication between Android OS and applications

Content Providers

• handle data and database management issues

Views

• UI elements like Buttons, Textviews and many more…

Android Application Fundamental

Individual app lives in its own world

• Each application has its own process

• Identify with unique Linux user ID

• File alteration only permitted – ID and apps (through permission)

Applications in between can – process elements of each other

DEVELOPMENT ENVIRONMENT Eclipse – Android Studio

System requirements

OS

• Xp (32) , Vista (32 - 64), 7 (32 - 64)

• Mac OS (10.5.8 or later)

• Linux

Eclipse IDE (3.6.2 or later)

Android SDK

ADT Plug In

JDK 6 (or later)

Continue . .

Remember to get download of right version of java

Unpack ZIP of SDK at appropriate location

Install additional version of Android as well as other packages

Configure Android Development Environment on Eclipse

• Add New Software to Eclipse

• ADT Plug In configuration

ADT integration in Eclipse

SDK Manager

Android Virtual Device

DDMS Perspective

Debug..

Android Studio

Studio interface

First Hello World Example (Demo)

ANDROID APPLICATION STRUCTURE

Codes & Resources resides in Different Folders

Can be added as needed

• src : all source codes

• gen : java files generated by ADT

• assets : store raw assests files

• bin : output directory

• res : resources of our application

• res/drawable : image/image-descriptor files

• res/layouts : layouts of application

• res/menus : application menus

• res/values : other resources of application like • Strings, Styles, Colors and so on… (xml based)

Studio file structure

Benefits

• Good to keep code away from all other things

• Easy to maintain – update – manage

• Support different devices and localization

• Resource selection dynamically at runtime

System resources defined under System.R

Strings.xml

Colors.xml

Efficient to group all colors in one separate file like strings the

same way…

Androidmenifest.xml

Application descriptor file

Properties like..

• Activities

• Content Providers

• Services

• Intent Receivers

• Permissions

• Version Number

• And much more . . .

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.svit.helloworld"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk android:minSdkVersion="19"

android:targetSdkVersion="21" />

<application android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name=".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>

Different language and hardware

Dynamic resource collection mechanism

• Done with dynamic directory structure

• Specific language, location, hardware

• Alternatives described with – mark

Like.

• res/layout/main.xml

• res/layout-land/main.xml

• res/values-fi/strings.xml

• res/values-en/strings/xml

Screen orientation support

Portrait and Landscape mode can be done easily by resources

• Portrait – automatically

• Landscape – specified in layout-land

• Phone switches layout automatically by when orientation changes

BREAK

LAYOUTS AND VIEWS

Android Application UI is built using Views and Viewgroups

Different “Widgets” like Button, Textview, which are the UI objects are the subclass of the class “View”

“Layouts‟‟ are the subclass of Viewgroup

Different types of Layouts are available in this class

Various Layouts

• Arrange the views in Single Row or Single Column

Linear Layout

• Group views into rows and column • <TableRow> - design a Row in the Table

Table Layout

• Enables you to set child position relative to each other

Relative Layout

• A blank space on the screen which can later be filled with a single object

Frame Layout

Defining Layouts with xml

Most common way is to use XML for declaring Layout

Each element of XML is ether a View or a Viewgroup

The Name of the XML element is like the java class that represents like <TextView> element creates a Textview in the UI

Android attach the view hierarchy tree to the screen

Activity call the setContentView() method and pass the reference to the root node object

Then draw() method of the view is called to draw the widget on screen

Linear Layout Example

Relative Layout Example

Relative Layout Example

Relative Layout Example

Relative Layout Example

Common Input controls

Input controls are the interactive components of the application‟s User Interface

Android provides wide variety of controls like Buttons, Text fields, seekbars, checkboxes, spinners, pickers and so on….

Use drag and drop the controls from palette to set controls to layout or edit in XML file directory

Handling UI Events

Many ways to intercept the events from a user‟s interaction

depends on the SDK version

The approach is to capture the events from the specific view

object that the user interacts with

Use different EventListeners with code or in XML

• onClick, onLongClick, onFocusChange, …….

Handling UI Events

ACTIVITY

basic

App Component (UI) – user interact to do something

One app – multiple activities

• Main activity – launch time presented activity

• Other – specified to particular tasks

Each activity can start another activity

Subclass of Activity class

callBack methods – system calls different states of app activity

Activities are set in the stacks – root activity (begin the Task of Application)

Three main states: running, paused, stopped

• Pasued/stoppped : system can drop it from

memory

• UI, data sources and EventHandlers are

bind in onCreate()

• After onResume() : activity visible to user

• onPause() : save critical data at app data

store

android:name - specify class name of the Activity

Other attributes – specify additional information (label, icon,

theme..)

Intent filter – make your activity available to system or other

applications

INTENTS

basics

Most important and unique concepts

Like „message‟ to someone

Can be used to

• Broadcast/Transfer data between application and app components

• Start Activities and Services

• Broadcast that event/action has occurred

Intent Object like bundle of information

Contains

• Component : explicit name of component class to use for intent

• Action : action to be perform

• Data : URI of data to be acted on

• Category : string that indicate kind of a component that should handle the intent

• Type : indicate Type of intent data

• Extras : key-value pairs of information

• Flags : instruct how to launch activity and how to treat it

Intent object actions

Intent object data

ACTION_VIEW content://contacts/people/1

ACTOIN_DIAL content://contacts/people/1

ACTION_VIEW tel:123

ACTION_DIAL tel:123

ACTION_EDIT content://contacts/people/1

Intent object category

Any no of category can be included for intent to categorized

Intent object extras

Bundle of additional information

Provide extended information to the component

Intent resolution

Two groups

• Explicit : used for application-internal messages(b/w activities)

• Delivered to an instance of designated target class

• startActivity(intent)

• Starts new Activity (Data can be passed with intent)

• Parent does not notify when child activity completes

• startActivityForResult(intent, REQUEST_CODE)

• same but parent get notify when child activity completes by OnActivityCompletes()

• Implicit : used to activate components in other applications

• Resolved by intent filters by object‟s (Action – Data - Category)

• System must find best component to handle intent

Intent filter

Explicit – always deliver to target class

Activities, Services and Broadcast receivers can have one or more

intent filters

Filter describes the capability of a component

Defined in the manifest file

Passing data between activities

Intent can have addtional data extras

Key/value pair

Send data:

• intent.putExtra(“info”,value);

Get data:

• Bundle extra = getIntent().getExtras();

• int var = extra.getInt(“info”);

Receiving result from an activity

Custom Intents

• Customize name in the manifest to receive intent

Broadcast Intents

• No activity needed

• Listen broadcast intents

• Register in manifest file as Receiver

• Intent-filter used to match with listened intent actions

Custom intent

Broadcast intents

FRAGMENTS

Fragment Basics

Fragment represents a behaviour or a portion of user interface in

an Activity

To build a multi-Pane UI, multiple fragments are combined in a

single activity

Fragment must be embedded in an activity

Fragment has its own lifecycle

It supports more dynamic and flexible UI designs on large screens

Example of Typical fragment:

• For Tablet: - list in left & content on right

• For Handset: - List & content on different Activity

SERVICES

basic

Components runs in background (without UI)

not a separate process or thread

Can be stand alone process or part of an application

Started, stopped, controlled by other application cpmponents

Started services have higher priority than inactive or invisible activities

Supports

• local services (not accessible outside of application)

• remote services (accessible outside of application)

Service creation

Create class that extends Service class

Override needed methods

• onCreate() : Launched on main application thread

• onStartCommand(Intent, int, int) : Create and run new thread

• onBind() : bind Activities to Service

• onDestroy() : do all clean up (stop threads,..)

Service lifcycle

Context.startService()

• onCreate() : create a Service

• onStart() : start it

• onStartCommand()

• Context.stopService()/stopSelf() : Stops it

Client.bindService()

• Create if it is not already running

• Doesn‟t call onStartCommand()

• onBind() : client will receive Ibinder object from

Service itself

• Remain running as long as connection is established

• Allows client to call back to Service

Day 1 ends here