+ All Categories
Home > Technology > Introduction to Android Development with Java

Introduction to Android Development with Java

Date post: 03-Jul-2015
Category:
Upload: jim-mckeeth
View: 2,001 times
Download: 2 times
Share this document with a friend
Description:
An introduction to the Android platform and how to develop for it with Java and the provided tools. Also covers best practices and some resources to aid in the development process.
62
INTRODUCTION TO ANDROID DEVELOPMENT Jim McKeeth @JimMcKeeth [email protected] 1 Tuesday, August 20, 13
Transcript
Page 1: Introduction to Android Development with Java

INTRODUCTION TO ANDROID DEVELOPMENT

Jim McKeeth@JimMcKeeth

[email protected]

1

Tuesday, August 20, 13

Page 2: Introduction to Android Development with Java

AGENDA

• Hello Android

• Overview of Platform

• Platform Tools

• UI - XML

• Views & Widgets

• Architecture

• Manifest & Permission

• Activities & Intents

• Debugging

• Resources

2

Tuesday, August 20, 13

Page 3: Introduction to Android Development with Java

ABOUT ME• Live here in Boise - [email protected]

• Lead Developer Evangelist for Embarcadero Technologies

• www.embarcadero.com - Delphi for Android coming soon

• Google Developer Group - www.gdgb.org

• Boise Software Developers Group - www.bsdg.org

• Boise Code Camp - www.BoiseCodeCamp.com

Tuesday, August 20, 13

Page 4: Introduction to Android Development with Java

DEMO: HELLO ANDROID

Tuesday, August 20, 13

Page 5: Introduction to Android Development with Java

GOOD PLACES TO KNOW

• developer.android.com - Official developer site

• developers.google.com/android/ - Google on Android

• developers.google.com/events/io/ - Google I/O Replays

• stackoverflow.com - Great Q&A community

• android.stackexchange.com - End user Q&A

• vogella.com/android.html - Detailed tutorials5

Tuesday, August 20, 13

Page 6: Introduction to Android Development with Java

ANDROID

6

Tuesday, August 20, 13

Page 7: Introduction to Android Development with Java

ANDROID STACK

• Tweaked Linux 2.6

• Tweaked Java (Dalvik)

• Android OS

• Android Apps

• (Mostly open source)

7

Tuesday, August 20, 13

Page 8: Introduction to Android Development with Java

VERSION HISTORY

• November 5th, 2007 is Android’s Birthday

• Each version has a version number, name and API level

• Version number is most specific

• Name is group of versions and API levels

• Latest: Android 4.3 Jelly Bean (API 18)

http://en.wikipedia.org/wiki/Android_version_history#Version_history_by_API_levelTuesday, August 20, 13

Page 9: Introduction to Android Development with Java

VERSION POPULARITY

As of August 2013http://sn.im/platform-versions9

40% = Jelly Bean63% >= ICS96% >= Gingerbread

40%

23%

33%

Tuesday, August 20, 13

Page 10: Introduction to Android Development with Java

Tuesday, August 20, 13

Page 12: Introduction to Android Development with Java

FRAMEWORK• Dalvik VM

• WebKit Browser

• 2d Graphics Library, 3D w/OpenGL

• SQLite

• MPEG4, MP3, PNG, JPEG, etc

• GSM Telephony, Bluetooth, WiFi

• Camera, GPS, Compass, Accelerometer12

Tuesday, August 20, 13

Page 13: Introduction to Android Development with Java

CORE APPLICATIONS

• Phone

• eMail and SMS clients

• Calendar

• Contacts

• Web Browser

• Geographic Support via Google Maps13

Tuesday, August 20, 13

Page 14: Introduction to Android Development with Java

ANDROID DEVELOPMENT

Tuesday, August 20, 13

Page 15: Introduction to Android Development with Java

ANDROID SDK

• http://developer.android.com/sdk

• 3 Options

• ADT Bundle (in Eclipse)

• “Existing IDE” download

• Android Studio preview (in IntelliJ)

15

Tuesday, August 20, 13

Page 16: Introduction to Android Development with Java

RELATED TOOLS

• Managers

• Android SDK Manager

• AVD Manager (Android Virtual Device)

• Command-Line Tools

• adb (Android Debug Bridge - Your best friend!)

• Others

Tuesday, August 20, 13

Page 17: Introduction to Android Development with Java

ANDROID DEVELOPMENT

JDK$ Android'SDK'

Dalvik'Virtual'Machine'

Linux&kernel&

Tuesday, August 20, 13

Page 18: Introduction to Android Development with Java

BUILD PROCESS

Android'App'Package'

APK'

DEX'Compiler'

Dalvik'byte'code'

Java'Compiler'

Java'JAR'

Tuesday, August 20, 13

Page 19: Introduction to Android Development with Java

THE APK FILE

• Zip file containing

• Manifest, etc.

• Compiled code

• Resources & Assets

Tuesday, August 20, 13

Page 20: Introduction to Android Development with Java

ACTIVITY ARCHITECTURE

Tuesday, August 20, 13

Page 21: Introduction to Android Development with Java

Applica'on*Context*

PARTS OF AN ANDROID APP

Intent%

Ac#vity(

Class% Layout' String'Resources'

Android'Manifest'

Other&Classes&

Drawables)

Services(

Broadcast)Receiver)

Content&Providers&

Ac#vity( Intent%

Tuesday, August 20, 13

Page 22: Introduction to Android Development with Java

PROJECT LAYOUT

• AndroidManifest.xml

• bin (APK file, compiled DEX files)

• gen (generated resources)

• res (resources you create)

• src (Java source files)

Tuesday, August 20, 13

Page 23: Introduction to Android Development with Java

MANIFEST

• Mandatory XML file “AndroidManifest.xml”

• Describe application components

• Default Activity

• Permissions, Attributes

• Non-SDK libraries

http://sn.im/android-manifestTuesday, August 20, 13

Page 24: Introduction to Android Development with Java

MULTIPLE ACTIVITIES

• Each Activity must be defined in manifest by class name and “action”

• Activities reside on a “task stack” - visible activity is “top of stack”

• Transition to an Activity using Intent

• Return from Activity using Activity.finish() which pops off task stack

Tuesday, August 20, 13

Page 25: Introduction to Android Development with Java

“RES” - RESOURCES

• XML Files

• drawable

• layout

• values

• etc.

• Graphics

Tuesday, August 20, 13

Page 26: Introduction to Android Development with Java

RES/LAYOUT

• Each Activity should have dedicated layout XML file

• Layout file specifies Activity appearance

• Several layout types

• Default LinearLayout provides sequential placement of widgets

Tuesday, August 20, 13

Page 27: Introduction to Android Development with Java

RES/VALUES

• strings.xml contains String constants

• Prefer strings.xml to hardcoded values (warnings)

• L10N/I18N (Localization/Internationalization)

Tuesday, August 20, 13

Page 28: Introduction to Android Development with Java

• Application/Task is a stack of Activities

• Visible activity = top of stack

• Activities maintain state, even when not TOS

• “Back” button pops stack.

• “Home” key would move stack to background

• Selecting application brings to foreground or launches new instance

APPLICATION AKA TASK

Tuesday, August 20, 13

Page 29: Introduction to Android Development with Java

ANDROID.APP.ACTIVITY

• Activity extends Context

• Context contains application environment

• “a single focused thing a user can do”

• An application consists of 1 or more Activities

• Provides a default window to draw in, might be smaller than screen or float on top of others

• UI elements are “View(s)” or widgets

Tuesday, August 20, 13

Page 30: Introduction to Android Development with Java

ANDROID.VIEW.VIEW

• Extends java.lang.Object

• Basic building block for UI components

• Rectangular area on display

• Responsible for drawing and event handling

• View is the base class for widgets

Tuesday, August 20, 13

Page 31: Introduction to Android Development with Java

Callback Methods(delegates)

Major States

Activity Lifecycle

http://sn.im/android-activityTuesday, August 20, 13

Page 32: Introduction to Android Development with Java

SAVING STATE

Tuesday, August 20, 13

Page 33: Introduction to Android Development with Java

FRAGMENTS

http://sn.im/android-fragmentsTuesday, August 20, 13

Page 34: Introduction to Android Development with Java

ANDROID.CONTENT.INTENT

• Interprocess Communication messaging

• Can launch an Activity or start a Service

• new Intent(String action, Uri uri);

• new Intent(Context packageCtx, Class class);

• Intent.putExtra()

• Intent.setAction()http://sn.im/android-intent

Tuesday, August 20, 13

Page 35: Introduction to Android Development with Java

INTENT (CONTINUED)

• android.intent.action.MAIN (receiver on main activity)

• many options: ACTION_VIEW, etc

• category = extra information

• MIME type

• extras (i.e. getAction(), getExtra(), etc)

Tuesday, August 20, 13

Page 36: Introduction to Android Development with Java

INTENT USAGE• Activity Switch:• new Intent(this, NextActivity.class);• Web Browser:

• Uri uri = Uri.parse(“http://foobar.com”);• new Intent(Intent.ACTION_VIEW, uri);

• Phone Dialer :• Uri uri = Uri.parse(“tel:4085551212”); • new Intent(Intent.ACTION_VIEW, uri);

http://sn.im/android-intentTuesday, August 20, 13

Page 37: Introduction to Android Development with Java

VIEWS

Tuesday, August 20, 13

Page 38: Introduction to Android Development with Java

VIEW LISTENERS

• GUI is event driven

• Event source: button press, mouse click, etc.

• Listener catches event and performs action

• Events are delivered to view w/current focus

Tuesday, August 20, 13

Page 39: Introduction to Android Development with Java

EVENT LISTENER• onClick()/View.OnClickListener()

• (boolean) onLongClick()/View.OnLongClickListener()

• onFocusChange()/View.OnFocusChangeListener()

• (boolean) onKey()/View.OnKeyListener()

• (boolean) onTouch()/View.OnTouchListener()

• onCreateContextMenu()/View.OnCreateContextMenuListener()

• return true to stop event propagation

Tuesday, August 20, 13

Page 40: Introduction to Android Development with Java

VIEW SIZE

• FILL_PARENT = parent sized (minus pad)

• Now “MATCH_PARENT” w/API Level 8

• WRAP_CONTENT = just big enough (plus pad)

Tuesday, August 20, 13

Page 41: Introduction to Android Development with Java

LAYING OUT VIEWS

• Gravity = position (i.e. left, right, center)

• Weight = extra/empty space allocation

• priority for expansion

• default weight is zero

Tuesday, August 20, 13

Page 42: Introduction to Android Development with Java

DIMENSION VALUES

• Dimension values:

• dp/dip = density independent pixels

• sp = Scale independent pixels

• px = pixels

• in/mm = inches/millimeters

• pt = point (1/72 inch)

Reco

mmen

ded

Not

Reco

mmen

ded

Tuesday, August 20, 13

Page 43: Introduction to Android Development with Java

DENSITY INDEPENDENT PIXELS

• An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, so 160dp is always one inch regardless of the screen density.

• The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion.

• You should use these units when specifying view dimensions in your layout, so the UI properly scales to render at the same actual size on different screens.

• The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

Tuesday, August 20, 13

Page 44: Introduction to Android Development with Java

SCALE-INDEPENDENT PIXELS

• This is like the dp unit, but it is also scaled by the user's font size preference.

• Recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.

Recom

mend

ed

for Fo

nts

Tuesday, August 20, 13

Page 45: Introduction to Android Development with Java

COLORS

• #RGB

• #ARGB ( Alpha / Red / Green / Blue )

• #RRGGBB (i.e. #3d1ec4)

• #AARRGGBB

Tuesday, August 20, 13

Page 46: Introduction to Android Development with Java

VIEW / VIEWGROUP

Tuesday, August 20, 13

Page 47: Introduction to Android Development with Java

LAYOUTS (VIEWGROUPS)• AbsoluteLayout -Absolute X, Y (depreciated)• FrameLayout - Pinned to top left (simplest)• LinearLayout - Horizontal or Vertical - one per row• RelativeLayout - Child specify relative widgets• SlidingDrawer - Hides until dragged out by handle• TableLayout - Rows & Columns• ScrollView - Child can be larger than window• TabWidget - Manages collection of “Tab Hosts”

Tuesday, August 20, 13

Page 48: Introduction to Android Development with Java

ORIENTATION

• landscape or portrait

• dedicated layout file

• /res/layout = default layout directory

• /res/layout-land = landscape layout directory

• /res/layout-port = portrait layout directory

Tuesday, August 20, 13

Page 49: Introduction to Android Development with Java

DEMO - LAYOUTS & VIEWS

Tuesday, August 20, 13

Page 50: Introduction to Android Development with Java

TRACE LOGGING• android.util.Log

• Log.d(“tag”, “debug”);

• Log.e(“tag”, “error”);

• Log.i(“tag”, “informational”);

• Log.v(“tag”, “verbose”);

• Log.w(“tag”, “warning”);

• View output using “adb logcat”http://sn.im/android-log

Tuesday, August 20, 13

Page 51: Introduction to Android Development with Java

ADB - ANDROID DEBUG BRIDGE

• Manages devices & emulators

• Issue debug commands

• Log output (adb logcat)

• Shell (adb shell)

• Copy files to/from emulator (adb push/pull)

http://sn.im/android_debug_bridgeTuesday, August 20, 13

Page 52: Introduction to Android Development with Java

DEMO - ADB & LOGCAT

Tuesday, August 20, 13

Page 53: Introduction to Android Development with Java

BEST PRACTICES

Tuesday, August 20, 13

Page 54: Introduction to Android Development with Java

BEST PRACTICES

• Support multiple screen sizes and densities

• Use the Action Bar pattern - Good-bye menu button

• Build Dynamic UI with Fragments

• Know when to persist and restore state

• Don’t block main thread - use AsyncTask - Test with StrictMode

http://sn.im/android-practiceshttp://sn.im/android-performance

Tuesday, August 20, 13

Page 55: Introduction to Android Development with Java

SUPPORT MULTIPLE SCREENSSIZES & DENSITIES

• Use wrap_content, fill_parent, or dp units when specifying dimensions in an XML layout file

• Do not use hard coded pixel values in your application code

• Do not use AbsoluteLayout (it's deprecated)

• Supply alternative bitmap drawables for different screen densities

• ldpi, mdpi, hdpi, xhdpihttp://sn.im/android-screens

Tuesday, August 20, 13

Page 56: Introduction to Android Development with Java

SCREEN SIZES & DENSITIES

Tuesday, August 20, 13

Page 57: Introduction to Android Development with Java

ACTION BAR

1. App Icon - optional “up” affordance

2. View control - Navigation

3. Dynamic number of “important” action buttons

4. Action overflowhttp://sn.im/android-actionbar

http://actionbarsherlock.com/

Tuesday, August 20, 13

Page 58: Introduction to Android Development with Java

ASYNC TASKprivate class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {

     protected Long doInBackground(URL... urls) {         int count = urls.length;         long totalSize = 0;         for (int i = 0; i < count; i++) {             totalSize += Downloader.downloadFile(urls[i]);             publishProgress((int) ((i / (float) count) * 100));             // Escape early if cancel() is called             if (isCancelled()) break;         }         return totalSize;     }

     protected void onProgressUpdate(Integer... progress) {         setProgressPercent(progress[0]);     }

     protected void onPostExecute(Long result) {         showDialog("Downloaded " + result + " bytes");     } } http://sn.im/android-asynctask

Tuesday, August 20, 13

Page 59: Introduction to Android Development with Java

STRICT MODE

• Place a policy on a thread - provides feedback when blocked

 public void onCreate() {

     if (DEVELOPER_MODE) {         StrictMode.enableDefaults(); // Or configure     }     super.onCreate(); }

• http://sn.im/android-strictmode

• http://sn.im/android-strictmode-blog

Tuesday, August 20, 13

Page 60: Introduction to Android Development with Java

FIN.

Tuesday, August 20, 13

Page 61: Introduction to Android Development with Java

GOOD PLACES TO KNOW

• developer.android.com - Official developer site

• developers.google.com/android/ - Google on Android

• developers.google.com/events/io/ - Google I/O Replays

• stackoverflow.com - Great Q&A community

• android.stackexchange.com - End user Q&A

• vogella.com/android.html - Detailed tutorials61

Tuesday, August 20, 13

Page 62: Introduction to Android Development with Java

ABOUT ME• Live here in Boise - [email protected]

• Lead Developer Evangelist for Embarcadero Technologies

• www.embarcadero.com - Delphi for Android coming soon

• Google Developer Group - www.gdgb.org

• Boise Software Developers Group - www.bsdg.org

• Boise Code Camp - www.BoiseCodeCamp.com

Tuesday, August 20, 13


Recommended