Programming Android UI J. Serrat Software Design December 2017 - - PowerPoint PPT Presentation

programming android ui
SMART_READER_LITE
LIVE PREVIEW

Programming Android UI J. Serrat Software Design December 2017 - - PowerPoint PPT Presentation

Programming Android UI J. Serrat Software Design December 2017 Preliminaries : Goals Introduce basic programming Android concepts Examine code for some simple examples Limited to those relevant for the project at hand Provide


slide-1
SLIDE 1

Programming Android UI

  • J. Serrat

Software Design December 2017

slide-2
SLIDE 2

Preliminaries : Goals

  • Introduce basic programming Android concepts
  • Examine code for some simple examples
  • Limited to those relevant for the project at hand
  • Provide references to materials for self study
  • Understand provided project implementation
slide-3
SLIDE 3

Preliminaries: why Android

  • Many students own an Android phone
  • Free development environment, Android Studio
  • Well documented
  • Good chance to learn UI design
  • Challenging design
  • T

ake away course project in your pocket

  • Add Android to your CV
  • Starting point to learn more
slide-4
SLIDE 4

Preliminaries: why Android

  • Drawbacks: learning curve
  • Many things left out
slide-5
SLIDE 5

Preliminaries: how to Android

T ry to solve small relevant problems in separate projects :

  • create an app bar with tabs / actions and
  • verflow action
  • create bottom bar
  • customize ListView to show project/task name,

date and time, intervals

  • make a contextual app bar
  • report and new project/task forms
  • ...
slide-6
SLIDE 6

Contents

  • 1. References
  • 2. Platforms and APIs
  • 3. Building blocks
  • 4. Structure of an Android project
  • 5. Activity life cycle
  • 6. Views, Layouts
  • 7. Intents, Broadcast receivers, Adapters
  • 8. Services
  • 9. TimeTracker architecture
slide-7
SLIDE 7

References

Beginning Android 4 application development. Wei-Meng Lee. Wiley, 2012. Electronic version at UAB library. Head first Android development. Dawn Griffiths, David Griffiths. O'Reilly, 2015.

slide-8
SLIDE 8

References

http://developer.android.com/training/index.html Always up to date How-to style, check the entry “Best practices for UI” → App bar

slide-9
SLIDE 9

Building blocks

Main logical components of Android applications :

  • Activity : UI component typically corresponding to
  • ne screen. They contain views = UI controls like

buttons, labels, editable text ... and layouts = view groups (composite) May react to user input and events (intents) An application typically consists of several screens, each screen is implemented by one activity. Moving to the next screen means starting a new activity.

slide-10
SLIDE 10

Building blocks

  • Service : application part that runs in background

without the user’s direct interaction, similar to a Unix daemon. For example, a music player.

  • Content provider : generic interface to manage

(access, change) and share (like “contacts”) application data. Can be stored as SQLite databases.

Activity Activity Application Activity Activity Application Activity Activity Content Provider Content Provider Service Service Application Data file Data file SQLite XML file XML file Remote Store Content Resolver Content Resolver Content Resolver Content Resolver Content Resolver Content Resolver

slide-11
SLIDE 11

Building blocks

  • Intent : “messages” sent by an activity or service

in order to

 launch an activity = show a new screen  broadcast (announce) that a certain event has

  • ccurred so that it can be handled

Fundamental to decouple cooperating application components.

  • Post 3.0 APIs include some more components:

fragments, tasks...

slide-12
SLIDE 12

Building blocks

Structure of an Android project: create and run a “Hello world” application

Do not close the emulator! It takes a lot to start. Each time you build the project, the new version is uploaded and execution starts automatically.

slide-13
SLIDE 13

Android Studio

  • Install Android Studio (3.0.1 in Dec. 2017)
  • Add some virtual devices, e.g. Galaxy Nexus + API24

Nougat, installing dependencies

  • Follow tutorial Training → Getting started → “Building

your first App”

slide-14
SLIDE 14

Android platforms and APIs

Compatibility with existing devices based

  • n Playstore downloads

+ Oreo ...

slide-15
SLIDE 15

Android platforms and APIs

slide-16
SLIDE 16

Android Studio

slide-17
SLIDE 17

Android Studio

Emulator may take a lot to launch, run slowly or even crash → disable cameras, multicore CPUs, set graphics to software emulation If absolutely needed, change to API19 KitKat or API22 Lollipop

slide-18
SLIDE 18

Android Studio

slide-19
SLIDE 19

Android Studio

slide-20
SLIDE 20

Structure of an Android App

MainActivity.java Automatically generated code

slide-21
SLIDE 21

“Inflates” the UI from the activity_main.xml file specifying it Autogenerated class R

Structure of an Android App

slide-22
SLIDE 22

Structure of an Android App

activity_main.xml

slide-23
SLIDE 23

Structure of an Android App

slide-24
SLIDE 24

Structure of an Android App

The interface design is represented in XML, decoupling design from code (opposite to “programmatic UI”). The call in Mainactivity.java setContentView(R.layout.activity_main) “inflates” the UI. Layouts are special (group) view that contain

  • ther views / group views in specific spatial

arrangements : LinearLayout, Gridlayout, RelativeLayout, ConstraintLayout... TextView is a non-editable text label.

slide-25
SLIDE 25

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textViewTitol" android:text="TubeQuoter V0.10" /> <TableLayout android:id="@+id/tableLayout1" android:layout_marginLeft="20dp" > <TableRow android:id="@+id/tableRow1"> <TextView android:id="@+id/textViewLabelLongitud" android:text="Longitud" /> <EditText android:id="@+id/editT extLongitud" android:inputT ype="number" > <requestFocus /> </EditT ext> <TextView android:id="@+id/textViewLabelUnitatsLongitud" android:text="mm" /> </TableRow> : : </TableLayout> <Button android:id="@+id/butocalcul" android:text="Calcula" /> </LinearLayout>

layout/main.xml

slide-26
SLIDE 26

Structure of an Android App

slide-27
SLIDE 27

Structure of an Android App

res/values/strings.xml

slide-28
SLIDE 28

Place to define UI constant strings, values, arrays

  • f integers and strings, colors, size of things

(dimensions)...

Structure of an Android App

slide-29
SLIDE 29

Structure of an Android App

slide-30
SLIDE 30

AndroidManifest.xml

Structure of an Android App

This activity may be the application entry point.

slide-31
SLIDE 31
  • includes xml nodes for each of the application

components : Activities, Services, Content Providers and Broadcast Receivers

  • using intent filters to specify how they interact

with each other:

 which activities can launch another activity or

service

 which broadcast intents an activity listens to,

in order to handle them with a receiver ...

  • offers attributes to specify application metadata

(like its icon or theme)

Structure of an Android App

AndroidManifest.xml

slide-32
SLIDE 32

Activity Life Cycle

  • Many Android devices have limited memory,

CPU power, and other resources.

  • The OS assures the most important processes

get the resources they need.

  • In addition, the OS takes

responsiveness very seriously: if the application does not answer user input (key press...) in < 5 seconds, the ANR dialog appears.

slide-33
SLIDE 33

Activity Life Cycle

  • Each application runs in its own

process, which has a main thread, within which activities, services... run

  • The OS ranks processes and

kills those with lowest priority, if some application needs unavailable resources.

  • If a process is killed “in the middle”, somehow

data can not be lost.

slide-34
SLIDE 34

Activity Life Cycle

Android in practice. Collins, Galpin, Käpler. Manning, 2012.

slide-35
SLIDE 35

Activity Life Cycle

States of an activity and methods invoked when changing state

Hello Android. Ed Burnette. The Pragmatic Programmer, 2010

Activity is active = visible in foreground interacting with user Activity is visible in background Not visible. Will remain in memory. Need to save data, such as a database record being edited.

slide-36
SLIDE 36

States of an activity and methods invoked when changing state Changing orientation landscape ←→ portrait calls onDestroy() +

  • nCreate()
slide-37
SLIDE 37

Views, Layouts

Control: extension of class View that implements some simple functionality, like a button. ViewGroup : extensions of the View class that can contain multiple child Views (compound controls). Layout managers, such as LinearLayout. Activities represent the screen being displayed to the user. You assign a View or layout to an Activity:

main.xml HelloWordActivity.java

slide-38
SLIDE 38

Views, Layouts

Common controls : T extView, EditT ext (many types), Button, ListView, ExpandableList, Spinner, Checkbox, ProgressBar, SeekBar, RadioGroup, RatingBar, Time and Date picker …

slide-39
SLIDE 39

Views, Layouts

Layouts control the position of child controls on a screen. Can be nested, creating arbitrarily complex interfaces. Common layouts:

  • LinearLayout adds each child View in a straight

line, either vertically or horizontally

  • RelativeLayout define the positions of child

Views relative to each other or screen boundaries

  • ConstraintLayout like relative layout but more

flexible, avoids nesting layouts in complex

  • designs. Check tutorial!
slide-40
SLIDE 40

Views, Layouts

1 2 3 4 1 2 3 4

3 LinearLayout

1 2

slide-41
SLIDE 41

Views, Layouts

RelativeLayout

slide-42
SLIDE 42

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <T extView android:id="@+id/textViewTitol" android:text="T ubeQuoter V0.10" /> <TableLayout android:id="@+id/tableLayout1" android:layout_marginLeft="20dp" > <T ableRow android:id="@+id/tableRow1"> <T extView android:id="@+id/textViewLabelLongitud" android:text="Longitud" /> <EditT ext android:id="@+id/editT extLongitud" android:inputT ype="number" > <requestFocus /> </EditT ext> <T extView android:id="@+id/textViewLabelUnitatsLongitud" android:text="mm" /> </T ableRow> : : </TableLayout> <Button android:id="@+id/butocalcul" android:text="Calcula" /> </LinearLayout>

slide-43
SLIDE 43

T

  • DoList example :

How to react to user input ? How to bind data to the UI (lists) ?

Views, Layouts

slide-44
SLIDE 44

Views, Layouts

slide-45
SLIDE 45

Views, Layouts

From now on, changes on ArrayList todoItems are shown in the screen when adapter notifies it.

slide-46
SLIDE 46

Views, Layouts

Java anonymous class Override onKey of class onKeyListener Which listeners has an EditT ext ?

slide-47
SLIDE 47

Views, Layouts

Java anonymous class Override onKey of class onKeyListener Which listeners has an EditT ext ?

slide-48
SLIDE 48

Intents

Intents is a fundamental concept in Android development : “the glue that binds applications' components”. Message-passing mechanism to

  • explicitly or implicitly start an Activity or a

Service

  • broadcast that an event has occurred,

application or system-wide to handle user action or process a piece of data

slide-49
SLIDE 49

Intents

slide-50
SLIDE 50

Intents

  • rigin

context activity to start

slide-51
SLIDE 51

Intents

Need to declare all activities in AndroidManifest.xml

slide-52
SLIDE 52

Broadcast Receivers

Intents can also be used to broadcast messages to anonymous components within one same application. The sender can associate data to those intents. A broadcast receiver (maybe within other app. component):

  • listens for selected types of broadcast intents
  • responds to them = processes associated data

'anonymous' means components do not need to know each other.

slide-53
SLIDE 53

Broadcast Receivers

On button click a broadcast intent of type “NEW_LIFE” is sent, along with three data fields. A broadcast receiver object has subscribed to this type of messages in the AndroidManifest.xml. The receiver does not belong to an Activity or Service in this case. Response is printing a message.

NEW_LIFE String name double longitude double latitude

slide-54
SLIDE 54

Broadcast Receivers

Broadcast intent type pairs of (key=string, value)

slide-55
SLIDE 55

Broadcast Receivers

data field names Broadcast intent type

slide-56
SLIDE 56

Broadcast Receivers

The broadcast receiver will always be active (listening), even when MyActivity has been killed or not started Broadcast intent type

slide-57
SLIDE 57

Broadcast Receivers

Alternatively, register the receiver when MyActivity is in foreground and unregister when not. T ypically when the receiver updates am UI element.

slide-58
SLIDE 58

Activity Life Cycle

States of an activity and methods invoked when changing state

Hello Android. Ed Burnette. The Pragmatic Programmer, 2010

Activity is active = visible in foreground interacting with user Activity is visible in background Not visible. Will remain in memory. Need to save data, such as a database record being edited.

slide-59
SLIDE 59

TimeTracker architecture

LlistaActivitatsActivity.java LlistaIntervalsActivity.java ListView controls

slide-60
SLIDE 60

TimeTracker architecture

slide-61
SLIDE 61

TimeTracker architecture

slide-62
SLIDE 62

TimeTracker architecture

slide-63
SLIDE 63

TimeTracker architecture

slide-64
SLIDE 64

TimeTracker architecture

slide-65
SLIDE 65

TimeTracker architecture

slide-66
SLIDE 66

TimeTracker architecture

Contains the actual activities and intervals tree Analogous to TimerT ask or Timer, which are not usable in Android. See code comments and references there. Harder to destroy by Android OS than activities

slide-67
SLIDE 67

TimeTracker architecture

Show a part of the tree, the childs of some node . P T P P T P T P I I I

root

different fields

slide-68
SLIDE 68

TimeTracker architecture

DONAM_FILLS PUJA_NIVELL BAIXA_NIVELL DONAM_FILLS ENGEGA_CRONOMETRE PARA_CRONOMETRE PUJA_NIVELL

slide-69
SLIDE 69

TimeTracker architecture

TE_FILLS + array of project and task data : dates and duration TE_FILLS + array of interval data : name, dates and duration

slide-70
SLIDE 70

TimeTracker architecture

Intent data to activities is a serialized array list of these

  • bjects.

This avoids serializing the whole or a subtree of activities and intervals, slow if the tree is large! (need to do it every 1, 2 secs.) Creates a random synthetic but data-consistent large tree (durations and dates)

slide-71
SLIDE 71

Homework

  • Get some recommended book AND read

developer.android.com/training main topics

  • Install Android Studio
  • Create a Hello world project, with string and

icon resources, try nested layouts and ConstraintLayout

  • Import our Android TT project into Android

Studio, replace our first milestone classes by yours and edit code to integrate it and make it work

  • Read comments, identify activities, service...