Introduction to mobile programming with Androids.

91
Intro to programming with Androids @MaksimGolivkin Android dev @Uber

description

Delivered in Kaunas Technical University as part of NFQ academy (http://www.nfq.lt/atviros-paskaitos/)

Transcript of Introduction to mobile programming with Androids.

Page 1: Introduction to mobile programming with Androids.

Intro to programming with Androids

@MaksimGolivkin

Android dev @Uber

Page 2: Introduction to mobile programming with Androids.

Plan

•  Brave mobile world

• Daily life-cycle

• Different screens

• Hybrid applications

Page 3: Introduction to mobile programming with Androids.

Why “mobile” development?

Page 4: Introduction to mobile programming with Androids.
Page 5: Introduction to mobile programming with Androids.
Page 6: Introduction to mobile programming with Androids.

In developing world

Page 7: Introduction to mobile programming with Androids.

And in the most posh economies.

Page 8: Introduction to mobile programming with Androids.

Smartphones, tablets and more

Page 9: Introduction to mobile programming with Androids.

Only an Internet device?

Page 10: Introduction to mobile programming with Androids.

What smart devices are made of?

Page 11: Introduction to mobile programming with Androids.

Memory Ports Positioning Radios Sensors Build-­‐in   Audio   Cellular   Mobile     Audio  

SD  card   USB   GPS   Wi-­‐Fi   Photo/video  

HDMI   Wi-­‐Fi   Bluetooth   Light  

A-­‐GPS   NFC   AcceleraBon  

MagneBc  

Gyroscope  

Proximity  

… Flash, Stylo, Second Screen

Page 12: Introduction to mobile programming with Androids.

Dongle empowering 3 billion $ business

Page 13: Introduction to mobile programming with Androids.

Why Android?

Page 14: Introduction to mobile programming with Androids.

Android creates a sweet choice.

Page 15: Introduction to mobile programming with Androids.

Open mobile computing platform

Page 16: Introduction to mobile programming with Androids.

64% in smartphones

40% in tablets  

of device sales in 2012 Q3

Android accounts for

Page 17: Introduction to mobile programming with Androids.

GooGPS show-case

Page 18: Introduction to mobile programming with Androids.

Two big butts

• App profitability 1/6 of iOS

•  Fragmentation

Page 19: Introduction to mobile programming with Androids.

Summary

• Dawn of connected devices era.

• HW knowledge creates opportunities.

• Android is leading it.

Page 20: Introduction to mobile programming with Androids.

Android OS

Page 21: Introduction to mobile programming with Androids.

Designed for fast switching between apps

Page 22: Introduction to mobile programming with Androids.

Applications “talk” between each other

Page 23: Introduction to mobile programming with Androids.

There is always a Back button

Page 24: Introduction to mobile programming with Androids.

Programming Android

Page 25: Introduction to mobile programming with Androids.

APIs

•  SDK – Java applications

• NDK – mostly games

• Hybrid – everybody should

Page 26: Introduction to mobile programming with Androids.

Tools

•  Eclipse

•  Eclipse ADT plugin

• Android SDK

• USB drivers

Page 27: Introduction to mobile programming with Androids.

Learn Android •  d.android.com •  stackoverflow.com

•  AppDemo sample application •  youtube for Google I/O

•  Android OS source code •  grepcode.com Books!

Page 28: Introduction to mobile programming with Androids.

Script vs. Application

Page 29: Introduction to mobile programming with Androids.

uber.com Init

Process

Output Display

Die PHP script life-cycle

Page 30: Introduction to mobile programming with Androids.

Interact

Init

Process

Die

Display

State

Application life-cycle

Page 31: Introduction to mobile programming with Androids.

Screen ~= Activity

Page 32: Introduction to mobile programming with Androids.

Created

Resumed

Stopped

Launch

Press Home

Interact

Foreground In Memory

Page 33: Introduction to mobile programming with Androids.

Created

Started

Resumed

Stopped

Launch

Paused

Interact

Open other

Activity

Visible Foreground In Memory

Page 34: Introduction to mobile programming with Androids.

Started

Resumed

Stopped

Re-Launch

Press Home

Paused

Interact

Visible Foreground In Memory

Page 35: Introduction to mobile programming with Androids.

Activities Stack (briefly)

Page 36: Introduction to mobile programming with Androids.

1st Screen

Resumed 1.

Page 37: Introduction to mobile programming with Androids.

2nd Screen

Stopped

Resumed

1.

2.

Page 38: Introduction to mobile programming with Androids.

3rd Screen

Stopped

Resumed

Stopped

1.

2.

3.

Page 39: Introduction to mobile programming with Androids.

Closes an application

Delegates responsibility

Page 40: Introduction to mobile programming with Androids.

Pressing Back

Stopped

Resumed

Stopped

1.

2.

3.

Page 41: Introduction to mobile programming with Androids.

Destroying last activity

Stopped

Resumed

1.

2.

Page 42: Introduction to mobile programming with Androids.

Pressing Home

Stopped

Resumed

Stopped

1.

2.

3.

Page 43: Introduction to mobile programming with Androids.

Stops everything

Stopped

Stopped

1.

2.

Stopped 3.

Page 44: Introduction to mobile programming with Androids.

Returned to the app

Stopped

Resumed

Stopped

1.

2.

3.

Page 45: Introduction to mobile programming with Androids.

Running In The Background?

… not all of them

Page 46: Introduction to mobile programming with Androids.

Maintaining state

Page 47: Introduction to mobile programming with Androids.

Activity/app life-time

• Parameters

+ Saved Instance State

•  Singleton

Singleton is king. Mind the GC!

Page 48: Introduction to mobile programming with Androids.

Persistence

•  Shared Preferences

•  Files

•  Server

•  SQLite

Do you really need it?

Page 49: Introduction to mobile programming with Androids.

Service

Page 50: Introduction to mobile programming with Androids.

Use cases

•  Long actions in between activities

• Notifications, when app is dead

•  Intensive calculations

Consider simply using Threads. Twice!

Page 51: Introduction to mobile programming with Androids.

Fragments

Page 52: Introduction to mobile programming with Androids.
Page 53: Introduction to mobile programming with Androids.

Fragments enable multi-pane layouts

Page 54: Introduction to mobile programming with Androids.

Resources

Page 55: Introduction to mobile programming with Androids.

Life of an image

Page 56: Introduction to mobile programming with Androids.
Page 57: Introduction to mobile programming with Androids.

Resources r = getResources(); Drawable d =

r.getDrawable(R.drawable.ic_american_express); ImageView icon =

(ImageView) findViewById(R.layout.card_logo); icon.setDrawable(d);

Using resources

Page 58: Introduction to mobile programming with Androids.

Drawable vs. View

Page 59: Introduction to mobile programming with Androids.

Screen ~= Activity Everything else ~= Views

Page 60: Introduction to mobile programming with Androids.

Summary (Android OS) • Android is popular but poor, yet

•  Learn life-cycle by heart

• Assess feasibility of Persistence and

Services. Twice.

Page 61: Introduction to mobile programming with Androids.

Many screens

Page 62: Introduction to mobile programming with Androids.
Page 63: Introduction to mobile programming with Androids.

Different resolutions

320x480 px 1280x720 px

Page 64: Introduction to mobile programming with Androids.

42 dp

Same physical size

Page 65: Introduction to mobile programming with Androids.

screen_ density = pixel _width2 + pixel _height2

diagonal _ in_ inches

Screen density

Page 66: Introduction to mobile programming with Androids.

Many resolutions 320x426 legacy phones 240x320 legacy phones 320x533 320x576

legacy phones

320x480 phones 320x533 320x568

phones

320x480 360x640 400x640

new phones

640x1067 640x1138

tweener tablets

480x800 480x854

600x1024

tweener tablets

1024x768 1280x768 1280x800

tablets

...  

Page 67: Introduction to mobile programming with Androids.

Landscape is yet another resolution

Page 68: Introduction to mobile programming with Androids.

Patterns

Page 69: Introduction to mobile programming with Androids.

Stretching

Page 70: Introduction to mobile programming with Androids.

Adding margins

Page 71: Introduction to mobile programming with Androids.

Multi-pane

Page 72: Introduction to mobile programming with Androids.

Switch points

Page 73: Introduction to mobile programming with Androids.

Switch points

small size portrait small size landscape normal size portrait

normal size landscape large size portrait

Home base 240dp 320dp 360dp 400dp 426dp 480dp 533dp 568dp 578dp 640dp

resized elements

margins added

switch point (another layout)

Page 74: Introduction to mobile programming with Androids.

Expensive way

small size portrait small size landscape normal size portrait

normal size landscape large size portrait

Home base 240dp 320dp 360dp 400dp 426dp 480dp 533dp 568dp 578dp 640dp

Boom!

resized elements

margins added

switch point (another layout)

Page 75: Introduction to mobile programming with Androids.

Prepairing graphics

Page 76: Introduction to mobile programming with Androids.

Density buckets

ldpi (low) 100 ~ 140 dp

mdpi (medium) 140 ~ 200 dp

hdpi (high) 200 ~ 280 dp

xhdpi (extra high) 280 ~ 340 dp

Page 77: Introduction to mobile programming with Androids.

1 dp = ? px

ldpi 0.75

mdpi 1

hdpi 1.5

xhdpi 2

Page 78: Introduction to mobile programming with Androids.

Nine-patch

Resizable area

Content area

Page 79: Introduction to mobile programming with Androids.

Nine patch •  Buttons

•  Backgrounds

Page 80: Introduction to mobile programming with Androids.

Summary (Many screens) • Needs investment, but little surprise.

• One layout for a start.

• Resolution ignorance is ugly,

but not ineffective.

Page 81: Introduction to mobile programming with Androids.

Hybrid apps

Page 82: Introduction to mobile programming with Androids.

“Our biggest mistake was betting too much on HTML5”, - Mark Zuckerberg

Page 83: Introduction to mobile programming with Androids.

HTML5 reality in 2012

Page 84: Introduction to mobile programming with Androids.

Hybrid champion: LinkedIn

Native

Native

HTML/CSS  

Page 85: Introduction to mobile programming with Androids.

Hybrid architecture

Native

JavaScript API

HTML/CSS/JavaScript

Page 86: Introduction to mobile programming with Androids.

WebView webView = (WebView) findViewByid(R.id.webview);

webView.addJavascriptInterface(obj, "Android"); final String html = AssetUtil.readAssetsFile(

context, filename); webView.loadDataWithBaseURL(

"file://", html, "text/html","utf-8", null);

Native side

Page 87: Introduction to mobile programming with Androids.

function onClick() {

Android. jsOnNextArticle(this.id); }

JavaScript side

Page 88: Introduction to mobile programming with Androids.

public void showArticle(long id, String content) { webView.loadUrl("javascript: jsShowArticle(" + id

+ ", \" + Uri.encode(content) + "\");") } .. public void jsOnNextArticle(long articleId) {

… }

“API” glue

Page 89: Introduction to mobile programming with Androids.

public void showArticle(long id, String content) { webView.loadUrl("javascript: jsShowArticle(" + id

+ ", \" + Uri.encode(content) + "\");") } .. public void jsOnNextArticle(long articleId) {

… }

“API” glue

Page 90: Introduction to mobile programming with Androids.

Summary (Hybrid apps)

• Content centered apps

•  FAQ, User License, …

•  1-1.5x more effort than native

• Pays of when targeting >= 3 platforms

Page 91: Introduction to mobile programming with Androids.

Read ON -  The real problem with Android fragmentation

-  Where does Android fragmentation hide?

-  The technical adventure building a hybrid app.

-  Fast track to Android design.

Interested in Android? @MaksimGolivkin

Care to give feedback? [email protected]