82
Ben Bishop [email protected] EPER DIVE INTO XAMARIN.AN

Deep Dive Xamarin.Android

Embed Size (px)

DESCRIPTION

My presentation from St. Louis Days of .NET that covers the 20% of concepts that helps you get through the 80% of your Android app development with Xamarin.Android.

Citation preview

Page 1: Deep Dive Xamarin.Android

Ben [email protected]

A DEEPER DIVE INTO XAMARIN.ANDROID

Page 2: Deep Dive Xamarin.Android
Page 3: Deep Dive Xamarin.Android
Page 4: Deep Dive Xamarin.Android

for code/slides...@benjamminstl

@rendrstl

Page 5: Deep Dive Xamarin.Android

Who Am i?• My name is Ben Bishop (Twitter:

@benjamminstl)

• I grew up in a small rural town of Indiana

• Went to Purdue University

• Now lives in Saint Louis

• Been independent for 4+ years

• Worked at an agency for 5 years, 1 year at TMG

Page 6: Deep Dive Xamarin.Android

my new companyhttp://rendr.io

Page 7: Deep Dive Xamarin.Android

WHAT IS A XAMARIN?

“The best damn mobile team,building the best damn mobile platform.”

Page 8: Deep Dive Xamarin.Android

What is Mono?• Mono 1.0 was released on June 30, 2004

• “The Mono runtime contains a code execution engine that translates ECMA CIL byte codes into native code and supports a number of processors: ARM, MIPS SPARC, PowerPC, S390 , x86, x86-64 and IA-64 for 64-bit modes.”

• Also has support for LLVM

• Runs on Linux, OSX, Windows, PS3, XBox 360, Wii

Page 9: Deep Dive Xamarin.Android

How does Mono Work?“Xamarin compiles your app to a native binary, not cross-compiled, and not interpreted.”

Page 10: Deep Dive Xamarin.Android

ART (Android RunTime) is an experimental runtime that could replace Dalvik. ART uses

Ahead of Time compilation instead of JIT (like iOS)

Page 11: Deep Dive Xamarin.Android

Code StructureBy only having to write core code once, more time

can be spent tuning the UI per platform.

Page 12: Deep Dive Xamarin.Android

must have componentsComponents that keep you from having to write

platform specific code.

Page 13: Deep Dive Xamarin.Android

xamarin.mobileA component that helps abstract your code for

location, photos, and contacts across all platforms.

Page 14: Deep Dive Xamarin.Android

Xamarin.SocialA component that helps abstract your code for

App.Net, Twitter, Facebook, and Flickr

Page 15: Deep Dive Xamarin.Android

xamarin.authMakes OAuth suck less.

Page 16: Deep Dive Xamarin.Android

DataRestSharp, SQLite.NET, and JSON.net help you

store and retrieve data for your app.

Page 17: Deep Dive Xamarin.Android

Android componentsGoogle Play Services allows your app to interface with the PlayStore. Android support helps make

your Android app backwards compatible.

Page 18: Deep Dive Xamarin.Android

Frameworks

https://github.com/MvvmCross/MvvmCross

https://github.com/benbishop/MonkeyArms

Page 19: Deep Dive Xamarin.Android

anatomy of an Android app

Page 20: Deep Dive Xamarin.Android

Android Manifest.xml

Page 21: Deep Dive Xamarin.Android

Edit App Name & Package, Icon, Version #, Min and Target Android SDKs, Permissions

Page 22: Deep Dive Xamarin.Android

specify supported screens

http://developer.android.com/guide/topics/manifest/supports-screens-element.html

Page 23: Deep Dive Xamarin.Android

Pro tip: need for speed

You can specify hardware acceleration for the app or for specific activities. http://developer.android.com/guide/topics/graphics/hardware-accel.html

Page 24: Deep Dive Xamarin.Android

More activity options

http://developer.android.com/guide/topics/manifest/activity-element.html

Page 25: Deep Dive Xamarin.Android

Activities

Page 26: Deep Dive Xamarin.Android

Activity Lifecycle

Page 27: Deep Dive Xamarin.Android

Activity Lifecycle

Page 28: Deep Dive Xamarin.Android

Activity Lifecycle

Page 29: Deep Dive Xamarin.Android

lifecycle in use

Page 30: Deep Dive Xamarin.Android

another example

OnResume and OnPause are good placesto do assignment and release.

Page 31: Deep Dive Xamarin.Android

declaring activity behavior

In this case, InitialActivity is our splash/loading screen.

Page 32: Deep Dive Xamarin.Android

layouts

Page 33: Deep Dive Xamarin.Android

Anatomy of a layout

Page 34: Deep Dive Xamarin.Android
Page 35: Deep Dive Xamarin.Android

includes

Page 36: Deep Dive Xamarin.Android

layouts for orientations

By creating a directory “layout-land,” Activities will use this as content when orientation changes.http://docs.xamarin.com/guides/android/application_fundamentals/handling_rotation/

Page 37: Deep Dive Xamarin.Android

layouts for screen sizes

Relying on naming conventions you can specify layouts to be used for screen widths. “layout-large” is for Android

apps below API-13, “layout-swXXXdp” is for 13 and above.

Page 38: Deep Dive Xamarin.Android

How layouts are connected to your code

Page 39: Deep Dive Xamarin.Android

Strings.xml

Page 40: Deep Dive Xamarin.Android

How to connect strings.xml to your code

You can link via layout xml or use “GetString” from your Activity.

Page 41: Deep Dive Xamarin.Android

string array

This can be used as a data provider for a spinner.

Page 42: Deep Dive Xamarin.Android

localization

More folder name magic!

Page 43: Deep Dive Xamarin.Android

More value resource typesBoolXML resource that carries a boolean value.

ColorXML resource that carries a color value.

DimensionXML resource that carries a dimension value. IntegerXML resource that carries an integer value.

Integer ArrayXML resource that provides an array of integers.

Typed ArrayXML resource that provides a TypedArray (which you can use for an array of drawables).

Page 44: Deep Dive Xamarin.Android

Style.xml

Page 45: Deep Dive Xamarin.Android

declare reusable styles

Page 46: Deep Dive Xamarin.Android

Units of measurepxPixels - corresponds to actual pixels on the screen.

inInches - based on the physical size of the screen.

mmMillimeters - based on the physical size of the screen.

ptPoints - 1/72 of an inch based on the physical size of the screen.

dpDensity-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

spScale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

Page 47: Deep Dive Xamarin.Android

Connect style to view

Page 48: Deep Dive Xamarin.Android

Drawables

Page 49: Deep Dive Xamarin.Android

Using a PNG

PNGS FOR STATES

Page 50: Deep Dive Xamarin.Android

vector drawable

Page 51: Deep Dive Xamarin.Android

alternative drawables

Using folder naming conventions will tellAndroid what version of the drawable to use

Page 52: Deep Dive Xamarin.Android

MainApplication

Page 53: Deep Dive Xamarin.Android

BOOTSTRAP/INIT

Class will be instantiated on app startup.

Page 54: Deep Dive Xamarin.Android

sending intents

Page 55: Deep Dive Xamarin.Android

receiving intents

Page 56: Deep Dive Xamarin.Android

simple listview

Page 57: Deep Dive Xamarin.Android

Create and assign adapter

Page 58: Deep Dive Xamarin.Android
Page 59: Deep Dive Xamarin.Android

Grouped ListView

Page 60: Deep Dive Xamarin.Android

Setup list of data.

Page 61: Deep Dive Xamarin.Android

Assign an adapter

Page 62: Deep Dive Xamarin.Android

In our Adapter, we collapse unused TextViews

Page 63: Deep Dive Xamarin.Android

FragmentsAndroid 3.0+

Page 64: Deep Dive Xamarin.Android

Set layout of Activity

Page 65: Deep Dive Xamarin.Android

init tab fragments

Page 66: Deep Dive Xamarin.Android

the fragment

Page 67: Deep Dive Xamarin.Android

fragment lifecycle

Page 68: Deep Dive Xamarin.Android

Fragments are important

Fragments promote code reuse.

Page 69: Deep Dive Xamarin.Android

custom views

Page 70: Deep Dive Xamarin.Android

Month Layout

Page 71: Deep Dive Xamarin.Android

Week Layout

Page 72: Deep Dive Xamarin.Android

notated cell

Page 73: Deep Dive Xamarin.Android

OnDraw

Page 74: Deep Dive Xamarin.Android

UpdateSwatches

Page 75: Deep Dive Xamarin.Android

Calendar month activity

Page 76: Deep Dive Xamarin.Android

Update calendar

Page 77: Deep Dive Xamarin.Android

Update week labels

Page 78: Deep Dive Xamarin.Android

when month events set

Page 79: Deep Dive Xamarin.Android

Updating cell swatches

Page 80: Deep Dive Xamarin.Android

parting thoughts

Page 81: Deep Dive Xamarin.Android

experiences- The Android SDK is substantially more robust (complicated?) than iOS

- Device fragmentation is challenging (TestCloud)

- Emulators suck. Use a device instead.

- Not as prescriptive in UX/Design

- The more innovative (but not as polished) platform.

Page 82: Deep Dive Xamarin.Android

thank you!