Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener...
Transcript of Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener...
![Page 1: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/1.jpg)
Programming Android UI
J. SerratSoftware DesignDecember 2017
![Page 2: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/2.jpg)
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
![Page 3: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/3.jpg)
Preliminaries: why Android
● Many students own an Android phone
● Free development environment, Android Studio
● Well documented
● Good chance to learn UI design
● Challenging design
● Take away course project in your pocket
● Add Android to your CV
● Starting point to learn more
![Page 4: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/4.jpg)
Preliminaries: why Android
● Drawbacks: learning curve
● Many things left out
![Page 5: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/5.jpg)
Preliminaries: how to Android
Try to solve small relevant problems in separate projects :
● create an app bar with tabs / actions and overflow 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
● ...
![Page 6: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/6.jpg)
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
![Page 7: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/7.jpg)
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.
![Page 8: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/8.jpg)
Referenceshttp://developer.android.com/training/index.html
Always up to dateHow-to style, check the entry “Best practices for UI” → App bar
![Page 9: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/9.jpg)
Building blocks
Main logical components of Android applications :
● Activity : UI component typically corresponding to one 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.
![Page 10: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/10.jpg)
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.
ActivityActivity
ApplicationActivityActivity
Application
ActivityActivity
Content ProviderContent Provider
ServiceService
Application
Data file
Data file
SQLite XML file
XML file
Remote Store
Content ResolverContent Resolver Content ResolverContent Resolver
Content ResolverContent Resolver
![Page 11: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/11.jpg)
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 occurred so that it can be handled
Fundamental to decouple cooperating application components.
● Post 3.0 APIs include some more components: fragments, tasks...
![Page 12: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/12.jpg)
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.
![Page 13: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/13.jpg)
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”
![Page 14: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/14.jpg)
Android platforms and APIs
Compatibility with existing devices based on Playstore downloads
+ Oreo ...
![Page 15: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/15.jpg)
Android platforms and APIs
![Page 16: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/16.jpg)
Android Studio
![Page 17: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/17.jpg)
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
![Page 18: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/18.jpg)
Android Studio
![Page 19: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/19.jpg)
Android Studio
![Page 20: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/20.jpg)
Structure of an Android App
MainActivity.java
Automatically generated code
![Page 21: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/21.jpg)
“Inflates” the UI from the activity_main.xml file specifying it
Autogenerated class R
Structure of an Android App
![Page 22: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/22.jpg)
Structure of an Android App
activity_main.xml
![Page 23: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/23.jpg)
Structure of an Android App
![Page 24: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/24.jpg)
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 other views / group views in specific spatial arrangements : LinearLayout, Gridlayout, RelativeLayout, ConstraintLayout...
TextView is a non-editable text label.
![Page 25: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/25.jpg)
<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/editTextLongitud" android:inputType="number" > <requestFocus /> </EditText>
<TextView android:id="@+id/textViewLabelUnitatsLongitud" android:text="mm" /> </TableRow> : : </TableLayout> <Button android:id="@+id/butocalcul" android:text="Calcula" />
</LinearLayout>
layout/main.xml
![Page 26: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/26.jpg)
Structure of an Android App
![Page 27: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/27.jpg)
Structure of an Android App
res/values/strings.xml
![Page 28: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/28.jpg)
Place to define UI constant strings, values, arrays of integers and strings, colors, size of things (dimensions)...
Structure of an Android App
![Page 29: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/29.jpg)
Structure of an Android App
![Page 30: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/30.jpg)
AndroidManifest.xmlStructure of an Android App
This activity may be the application entry point.
![Page 31: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/31.jpg)
● 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 AppAndroidManifest.xml
![Page 32: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/32.jpg)
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.
![Page 33: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/33.jpg)
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.
![Page 34: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/34.jpg)
Activity Life Cycle
Android in practice. Collins, Galpin, Käpler. Manning, 2012.
![Page 35: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/35.jpg)
Activity Life CycleStates 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 backgroundNot visible. Will remain
in memory. Need to save data, such as a database record being edited.
![Page 36: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/36.jpg)
States of an activity and methods invoked when changing state
Changing orientation landscape ←→ portrait calls onDestroy() + onCreate()
![Page 37: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/37.jpg)
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
![Page 38: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/38.jpg)
Views, Layouts
Common controls : TextView, EditText (many types), Button, ListView, ExpandableList, Spinner, Checkbox, ProgressBar, SeekBar, RadioGroup, RatingBar, Time and Date picker …
![Page 39: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/39.jpg)
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!
![Page 40: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/40.jpg)
Views, Layouts
1 2 3 4
1
2
3
4
3 LinearLayout
1
2
![Page 41: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/41.jpg)
Views, Layouts
RelativeLayout
![Page 42: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/42.jpg)
<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/editTextLongitud" android:inputType="number" > <requestFocus /> </EditText>
<TextView android:id="@+id/textViewLabelUnitatsLongitud" android:text="mm" /> </TableRow> : : </TableLayout> <Button android:id="@+id/butocalcul" android:text="Calcula" />
</LinearLayout>
![Page 43: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/43.jpg)
ToDoList example :
How to react to user input ?
How to bind data to the UI (lists) ?
Views, Layouts
![Page 44: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/44.jpg)
Views, Layouts
![Page 45: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/45.jpg)
Views, Layouts
From now on, changes on ArrayList todoItems are shown in the screen when adapter notifies it.
![Page 46: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/46.jpg)
Views, Layouts
Java anonymous class
Override onKey of class onKeyListener
Which listeners has an EditText ?
![Page 47: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/47.jpg)
Views, LayoutsJava anonymous class
Override onKey of class onKeyListener
Which listeners has an EditText ?
![Page 48: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/48.jpg)
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
![Page 49: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/49.jpg)
Intents
![Page 50: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/50.jpg)
Intents
origin context
activity to start
![Page 51: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/51.jpg)
Intents
Need to declare all activities in AndroidManifest.xml
![Page 52: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/52.jpg)
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.
![Page 53: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/53.jpg)
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_LIFEString namedouble longitudedouble latitude
![Page 54: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/54.jpg)
Broadcast Receivers
Broadcast intent type
pairs of (key=string, value)
![Page 55: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/55.jpg)
Broadcast Receivers
data field names
Broadcast intent type
![Page 56: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/56.jpg)
Broadcast Receivers
The broadcast receiver will always be active (listening), even when MyActivity has been killed or not started
Broadcast intent type
![Page 57: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/57.jpg)
Broadcast Receivers
Alternatively, register the receiver when MyActivity is in foreground and unregister when not.
Typically when the receiver updates am UI element.
![Page 58: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/58.jpg)
Activity Life CycleStates 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 backgroundNot visible. Will remain
in memory. Need to save data, such as a database record being edited.
![Page 59: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/59.jpg)
TimeTracker architecture
LlistaActivitatsActivity.java
LlistaIntervalsActivity.java
ListView controls
![Page 60: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/60.jpg)
TimeTracker architecture
![Page 61: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/61.jpg)
TimeTracker architecture
![Page 62: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/62.jpg)
TimeTracker architecture
![Page 63: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/63.jpg)
TimeTracker architecture
![Page 64: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/64.jpg)
TimeTracker architecture
![Page 65: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/65.jpg)
TimeTracker architecture
![Page 66: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/66.jpg)
TimeTracker architecture
Contains the actual activities and intervals tree
Analogous to TimerTask or Timer, which are not usable in Android. See code comments and references there.
Harder to destroy by Android OS than activities
![Page 67: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/67.jpg)
TimeTracker architectureShow a part of the tree, the childs of some node
.
P TP P T
P TP I I I
root
different fields
![Page 68: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/68.jpg)
TimeTracker architecture
DONAM_FILLSPUJA_NIVELL
BAIXA_NIVELLDONAM_FILLSENGEGA_CRONOMETREPARA_CRONOMETREPUJA_NIVELL
![Page 69: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/69.jpg)
TimeTracker architecture
TE_FILLS + array of project and task data : dates and duration
TE_FILLS + array of interval data : name, dates and duration
![Page 70: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/70.jpg)
TimeTracker architecture
Intent data to activities is a serialized array list of these objects.
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)
![Page 71: Programming Android UI - cvc.uab.es · Java anonymous class Override onKey of class onKeyListener Which listeners has an EditText ? Views, Layouts Java anonymous class Override onKey](https://reader031.fdocuments.us/reader031/viewer/2022041223/5e0da5e8ec11772f43221928/html5/thumbnails/71.jpg)
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...