DEPARTMENT OF COMPUTER GRAPHICS AND INTERACTION

Development Android

Jan Balata

1 Topics n n Architecture n Development n Accessibility

Development Android 2 n New UI design n New runtime n Enhanced notifications n Increased efficiency

Development Android 3 Material Design n Material is the metaphor – The material is grounded in tactile reality – Inspired by the study of paper and ink – The flexibility of the material creates new affordances that supersede those in the physical world n Bold, graphic, intentional – Deliberate color choices, edge-to-edge imagery, large-scale typography, intentional white space n Motion provides meaning – Respects and reinforces the user as the prime mover – Objects are presented to the user without breaking the continuity of experience – They transform and reorganize

Development Android 4 What is material? n 3D space n All objects have x, y, and z dimensions n Z-axis is perpendicularly aligned to the plane of the display n Every sheet of material occupies a single position along the z- axis and has a standard 1dp thickness

Development Android 5 Material properties

Material casts shadows. Material can change shape.

Z-axis motion is typically a result of user interaction with material.

If you remove a portion of material from a sheet of material, the sheet of material will become a whole sheet again. Development Android 6 Elevation n All material objects have a resting elevation, whether the object is a small component or a sheet that spans the entire display.

n Responsive elevation and dynamic elevation offsets

Development Android 7 Animation n Accelerate objects swiftly and decelerate them slowly to avoid abrupt changes in velocity. – Motion with swift acceleration and gentle deceleration feels natural and delightful.

Development Android 8 Meaningful transitions n It is difficult for a user to know where to look or understand how an element got from point A to point B – Carefully choreographed motion design can effectively guide the user’s attention and focus through multiple steps of a process or procedure

Development Android 9 Color n Color is inspired by bold color statements juxtaposed with muted environments – Taking cues from contemporary architecture, road signs, pavement marking tape, and sports courts – Emphasize bold shadows and highlights – Introduce unexpected and vibrant colors

Development Layout n Designed to scale across different screen sizes n Paper craft – In material design, every drawn by an application resides on a sheet of paper – Paper has a flat background color and can be sized to serve a variety of purposes – Typical layout is composed of multiple sheets of paper

Development Layout structure n Status bar n App bar n Toolbars n Side nav – Left nav for navigation or identity (Navigation drawer) – Right nav secondary to the main content n Content n Android navigation bar

Development Buttons n Choose a button style depends on the primacy of the button, the number of containers on screen, and the overall layout of the screen

Floating action button Raised button Flat button

Development Android 13 Gestures

Touch Double touch Drag, Swipe, or Fling Long-press

Long-press drag Double-touch drag Pinch open Pinch closed

Two-finger touch Two-finger swipe, drag, fling Two-finger long-press Two-finger long-press drag

Two-finger double touch Rotate

Development Android 14 Navigation I. n Up vs. Back – The Up button is used to navigate within an app based on the hierarchical relationships between screens – The system Back button is used to navigate, in reverse chronological order, through the history of screens the user has recently worked with

Development Android 15 Navigation II. n Up vs. Back – The Up button is used to navigate within an app based on the hierarchical relationships between screens – The system Back button is used to navigate, in reverse chronological order, through the history of screens the user has recently worked with

Development Android 16 Devices and Display I. n Millions of phones, tablets, and other devices – Wide variety of screen sizes and form factors n Use RelativeLayout – Allows to specify layout in terms of the spacial relationships between components not only linear n Use density-independent pixels (dp) – Base layout dimension unit – Defining dimensions with pixels is a problem because different screens have different pixel densities n Use scale-independent pixels (sp) – Base text size unit – It is scaled by the user's preferred text size

Development Android 17 Devices and Display II. n Each device is falling into size bucket and density bucket – ldpi (low) ~120dpi – mdpi (medium) ~160dpi – hdpi (high) ~240dpi – xhdpi (extra-high) ~320dpi – xxhdpi (extra-extra-high) ~480dpi – xxxhdpi (extra-extra-extra-high) ~640dpi n Design layouts for different size buckets n Provide bitmap images for different density buckets

Development Android 18 Devices and Display III. n Without support for different densities

n With support for different densities

Development Android 19 48dp Rhythm n Touchable UI components are generally laid out along 48dp units. – 48dp translate to a physical size of about 9mm (with some variability). • In the range of recommended target sizes (7-10 mm) for touchscreen objects – Users will be able to reliably and accurately target them with their fingers • Your targets will never be smaller than the minimum recommended target size of 7mm regardless of what screen they are displayed on • You strike a good compromise between overall information density on the one hand, and targetability of UI elements on the other

n Mind the gaps – Spacing between each UI element is 8dp

Development Android 20 Pure Android n Don't mimic UI elements from other platforms n Don't carry over platform-specific icons n Don't use bottom tab bars n Don't hardcode links to other apps n Don't use labeled back buttons on action bars n Don't use right-pointing carets on line items

Development Android 21 History n Developed by Android, Inc., bought by in 2005, unveiled in 2007 along with founding of Open Handset Alliance – first smartphone HTC Dream, (October 22, 2008) n Free under Apache License n Java libraries based on Apache Harmony

Development Android 22 ART

Development Android 23 Android Runtime n Core Java Libraries – APIs • Utilities, • File Access, • Network, • Graphics, ... n Dalvik Virtual Machine n Android Runtime (ART) – Ahead-of-time (AOT) compilation performed upon installation – From java bytecode for backward compatibility (.dex) – No compiling on start but more storage for compiled code – Improved performance of garbage collection, debugging, profiling

Development Android 24 Libraries & Application Framework

n Application Framework – Enable and simplify reuse of components • View System – buttons, text boxes, web browser, ... • Resource Manager – non-code resources: audio, images, ... • Notification Manager – alerts in status bar, … n Libraries (C++) – Available through Application Frameworks • Media Libraries, Audio Manager, 3D Libraries, SQLite, …

Development Android 25 Development n IDE – Graphical UI Builders, – Develop on Hardware Devices, – Develop on Virtual Devices, – Powerful Debugging, ... n Language – Apps usually developed in the Java – Parts of app using native-code languages such as C and C++ • Generally does not result in a noticeable performance improvement, but it always increases your app complexity • Signal processing, physics simulation, games ... – Cross-platform tools • Xamarin, PhoneGap, MonoGame, …

Development Android 26 Installation n JDK 6 n – http://developer.android.com/sdk/installing/studio.html – Based on IntelliJ IDEA – All the Android SDK Tools to design, test, debug, and profile your app – Windows, Mac, Linux

Development Android 27 Distribution Versions n Relative number of active devices running a given version of the Android platform

Version Codename API Distribution

2.2 Froyo 8 0.4%

2.3.3 - Gingerbread 10 7.4% 2.3.7

4.0.3 - Ice Cream 15 6.4% 4.0.4 Sandwich

4.1.x 16 18.4%

4.2.x Jelly Bean 17 19.8%

4.3 18 6.3%

4.4 KitKat 19 39.7%

5.0 Lollipop 21 1.6%

February 2, 2015

Development Android 28 Distribution Screens n Data about the relative number of active devices that have a particular screen configuration – Defined by a combination of screen size and density

February 2, 2015

Development Android 29 Application Fundamentals n Multi-user Linux system – Each application is a different user n Each process has its own virtual machine (ATR) – Application's code runs in isolation from other applications n Every application runs in its own Linux process – Android starts the process when any of the application's components need to be executed – Then shuts down the process • When it's no longer needed or • When the system must recover memory for other applications

Development Android 30 Application Components n Activities – A single screen with a user interface – Each one is independent of the others • Different application can start any one of these activities n Services – Component that runs in the background to perform long-running operations or to perform work for remote processes – Does not provide a user interface n Content Providers – Manages a shared set of application data – Can store the data in the file system, an SQLite database, on the web, or any other persistent storage location application can access n Broadcast Receivers – Component that responds to system-wide broadcast announcements – Can create a status bar notification to alert the user when a broadcast event occurs – A "gateway" to other components and is intended to do a very minimal amount of work

Development Android 31 Application Components Activation n Activated by an asynchronous message – intent – Activities, services, and broadcast receivers n Intent – “The messengers that request an action from other components” n For broadcast receivers intent defines the announcement being broadcast n Content provider not activated by intents – Activated when targeted by a request from a ContentResolver • Handles all direct transactions with the content provider

Development Android 32 The Manifest File n The system must know that the component exists – Reads the application's AndroidManifest.xml file – Application must declare all its components n What the manifest does? – Identify any user permissions – Declare the minimum API Level – Declare hardware and software features – API libraries the application needs (e.g. ) – And more

Development Android 33 The Manifest File Declaring components n Declaring component n Application – identifies requirements application – for activities – • name and label – for services – for receivers – for providers n uses n Component capabilities declarations to filter – applications for • In order to respond to intents from other applications particular devices

Development Android 34 The Manifest File Permissions n Using Permissions – in manifest • Wifi or GPS locations, SMS, Vibrations, Internet, Call phone, Wifi and network state, Boot receiver, Getting accounts • And More n Permissions requested by the application are granted to it by the package installer n Application was either granted a particular permission when installed or the permission was not granted – Any attempt to use the feature will fail without prompting the user

Development Android 35 Resources n Providing – You should place each type of resource in a specific subdirectory of your project's res/ directory. – Almost every application should provide alternative resources to support specific device configurations – Different screen densities and alternative string resources for different languages n Accessing – Referencing by its resource ID – Resource IDs are defined in project's R class • In source code R.string.hello • In layout from XML @string/hello – imageView.setImageResource(R.drawable.myimage);

Development Android 36 Activity Lifecycle I. n Implementing your activity lifecycle methods properly ensures your app behaves well in several ways, including that it: – Does not crash if the user receives a phone call or switches to another app while using your app – Does not consume valuable system resources when the user is not actively using it – Does not lose the user's progress if they leave your app and return to it at a later time – Does not crash or lose the user's progress when the screen rotates between landscape and portrait orientation

Development Android 37 Activity Lifecycle II. n Three static states – Resumed, Paused, Stopped

Development Android 38 Activity Lifecycle, Create & Destroy n onCreate() – Declaring the user interface (defined in an XML layout file), defining member variables, and configuring some of the UI n onDestroy() – Last chance to release system resources to prevent memory leaks – Stop background threads

Development Android 39 Activity Lifecycle, Pause & Resume n onPause() – Partially obscured (e.g. by dialog) – Stop animations – Commit unsaved changes (such as a draft email). – Release system resources, such as broadcast receivers, handles to sensors, or any resources that may affect battery life n onResume() – Every time activity comes into the foreground, including when it's created for the first time – Initialize components that you release during onPause()

Development Android 40 Activity Lifecycle, Stop & Restart n onStop() – Not visible at all – Perform larger, more CPU intensive shut-down operations, such as writing information to a database – Release almost all resources that aren't needed while the user is not using it – System might destroy the instance if it needs to recover system memory n OnStart() – Counterpart to the onStop() – System calls onStart() both when it creates your activity and when it restarts the activity from the stopped state

Development Android 41 Restart & Configuration Change

n If the orientation of the device is changed the activity restarts n For key-value pairs – Override onSaveInstanceState() method – Restore onCreate from Bundle savedInstanceState n For large data – Override onRetainNonConfigurationInstance() method to return an object carrying your data – Restore data when activity starts again with getLastNonConfigurationInstance() n You can handle orientation yourself – Define in manifest • android:configChanges="orientation|keyboardHidden" – And override onConfigurationChanged or do not if you don’t want the orientation changed – Application should always be able to shutdown and restart with its previous state intact – Don’t consider this technique an escape from retaining state during lifecycle

Development Android 42 Fragment n Represents a behavior or a portion of user interface in Activity n Combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities n Support tablets and smart phones in one application with flexible UI n Introduced in Android 3.0 – With Support Library can be used from Android 1.6

Development Android 43 Fragment Lifecycle

n Creating Fragment – Similar to Activity – onCreate() • Essential components to retain when the fragment is paused or stopped and resumed – onCreateView() • Returns View of the fragment – onPause() • Commit any changed that should be persistent n Create interface for communication with Activity n Fragment-to-Fragment communication done through the associated Activity – Two Fragments should never communicate directly

Development Android 44 Saving Data n Shared Preferences – Store private primitive data in key-value pairs n Internal Storage – Store private data on the device memory n External Storage – Store public data on the shared external storage n SQLite Databases – Store structured data in a private database n Network Connection – Store data on the web with your own network server

Development Android 45 Workers n Slow, blocking operations should be done in a new Thread – Avoid slowing down the user interface • Avoid infamous "application not responding" dialog – Do not access the Android UI toolkit from outside the UI thread, instead use one of following methods • Activity.runOnUiThread(Runnable) • View.post(Runnable) • View.postDelayed(Runnable, long) n Better using AsyncTask – Performs the blocking operations • doInBackground() and you can publishProgress() – Publishes the results on the UI thread • onPreExecute(), onPostExecute(), onProgressUpdate() n Loops using Handler – schedule runnable to be executed and schedule new one inside runnable

Development Android 46 Location n Location – LocationManager • Permissions, battery drain – Challenges • Multitude of location sources • User movement • Varying accuracy – Typical flow of procedures for obtaining the user location: • Start application • Sometime later, start listening for updates from desired location providers • Maintain a "current best estimate" of location by filtering out new, but less accurate fixes • Stop listening for location updates • Take advantage of the last best location estimate n External Google Maps Library v2 – 3D rendering, vector tiles, gesture control, draw on map, indoor floor plans, location services

Development Android 47 Sensors Overview n All sensors return multi-dimensional array of values for each SensorEvent except environment sensors n Motion sensors – Accelerometer, gravity, gyroscope, linear acceleration, rotation vector, step sensor n Position sensors – Magnetic field, proximity n Environment sensors – Ambient temperature (air), temperature (device), light, pressure, relative humidity,

Development Android 48 Accessibility n Navigation – Use more than just color to – Help users to be fast and convey information efficient – Provide clues about spatial – Make touch targets at least relationships 48 x 48 pixels – Give visual alternatives to – Support mouse-free and sound standard gesture navigation n Guidance and feedback – Manage the focus of your – Make interactive controls user clear and discoverable n Readability – Provide alternative text for – Ensure your app is still images and video usable with larger font sizes – Offer guidance and help – Ensure critical text has – Give meaning to links enough contrast

Development Android 49 Text-to-Speech n Screen reader support through whole Android n Czech language installation – Google TalkBack app – SVOX Classic app for external language support – SVOX Czech app with Czech Language n Activity implements TextToSpeech.OnInitListener – Override onInit method – Instantiate with context and listener – Speak • myTTS.speak(“Hello World”, TextToSpeech.QUEUE_FLUSH, null); n Explore by Touch with TalkBack – From Android 4.2 – Navigation Bar • works correctly if it is on-screen, no solution for capacitive buttons below the screen

Development Android 50 Other solutions n Human Interface Device Protocol – One of the possibilities for visually impaired – External numeric keyboard – Supported by Android 3.1+ n Nontraditional user interfaces – Thermal output – Control Arduino over bluetooth

Development Android 51 Resources n Design Guidelines – http://www.google.com/design/ – Guidelines n Developer Android – http://developer.android.com – Documentation, Tutorials, Sample Code n Stack Overflow – http://stackoverflow.com

Development Android 52 Thank for your attention!

Jan Balata, 23.2.2015

Development Android 53