CS378 - Mobile Computing
description
Transcript of CS378 - Mobile Computing
![Page 1: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/1.jpg)
CS378 - Mobile Computing
More UI
![Page 2: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/2.jpg)
UI Review• Containers–more formally ViewGroups– store widgets and other containers–examples: LinearLayout, RelativeLayout,
ListView, GridView, TableLayout, GridLayout• Widgets–buttons, text views (labels), edit texts,
spinners, radio buttons, seek bars
![Page 3: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/3.jpg)
Concrete Example• Tip Calculator• What kind of layout
to use?• Widgets:– TextView– EditText– SeekBar
![Page 4: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/4.jpg)
TextViews
![Page 5: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/5.jpg)
EditText
All but top EditText areuneditable
Alternative?TextViews?
![Page 6: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/6.jpg)
Uneditable EditText
Or could have used TextView.
![Page 7: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/7.jpg)
SeekBar
![Page 8: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/8.jpg)
Layout• TableLayout
row 0row 1
row 2
row 3row 4
row 5
![Page 9: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/9.jpg)
Layout• Table Layouts
made up of Table Rows
• Elements in Row appear in order declared in xml file
![Page 10: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/10.jpg)
Layout Attributes
• android:background–#RGB, #ARGB, #RRGGBB, #AARRGGBB– can place colors in res/values/colors.xml
![Page 12: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/12.jpg)
StretchColumns
• columns 0 indexed• columns 1, 2, 3 can stretch to fill layout width• column 0 wide as widest element, plus any padding
for that element– try changing String in column 0
• columns also shrinkable– shrink column(s) so table fits into parent object
![Page 13: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/13.jpg)
Initial UI• Done via some Drag
and Drop, Outline view, and editing XML
• Demo outline view–properties
![Page 14: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/14.jpg)
Changes to UI• all TextViews' textColor set to
black #000000• change column for %DD labels
• use center gravity for components
![Page 15: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/15.jpg)
Changes to UI• change bill total and
seekbar to span more columns
• gravity and padding for text in column 0
![Page 16: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/16.jpg)
Changes to UI
• align text vertically with seekBar
• set seekBar progress to 18
![Page 17: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/17.jpg)
Changes to UI• Prevent Editing in
EditText– focusable and cursor
visible properties to false– inputType set to none
• Set text in EditText to 0.00
• Change weights to 1 to spread out
![Page 18: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/18.jpg)
onCreate - tip and total amounts• onCreate instance variables assigned to
components found via ids• edit text for tip and total amounts
![Page 19: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/19.jpg)
Functionality - Total EditText
• Another anonymous inner class• implement onTextChanged to converts to double
and call update methods• register with EditText for total in onCreate()!
![Page 20: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/20.jpg)
![Page 21: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/21.jpg)
Functionality Responding to SeekBar
• customSeekBarListener instance variable• type OnSeekBarChangeListener
![Page 22: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/22.jpg)
Create an Anonymous Inner Class• Class notified when seek bar changed and
program updates custom tip and total amount• must register with the seekBar instance
variable in onCreate.
![Page 23: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/23.jpg)
Changing Tip and Totals • update standard tip and total amounts
when bill amount changes
![Page 24: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/24.jpg)
Update Custom Tip and Total
![Page 25: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/25.jpg)
Functionality - Saving State• onSaveInstance– save BillTotal and CustomPercent to the
Bundle– check for these in onCreate
![Page 26: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/26.jpg)
Orientation Change• The application has a single layout• Same layout used for vertical and
horizontal orientations• By default an orientation change will
destroy and recreate an application• before onDestroy called system will call
onSaveInstanceState
![Page 27: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/27.jpg)
onSaveInstanceState• called by system before destroying activity– activity below top of activity stack and
memory being reclaimed–orientation or configuration change
• passed a Bundle object• write necessary data to Bundle object to
restore state on restart• Bundle passed to onCreate when restarted
![Page 28: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/28.jpg)
onSaveInstanceState• in the tip calculator:• save value in Bill Total EditText and value
of SeekBar• All other values are calculated from these
![Page 29: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/29.jpg)
onCreate - check savedInstanceState
![Page 30: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/30.jpg)
onCreate check• In, onCreate method check the Bundle
value passed in to determine if creating Activity from scratch or a saved instance state
![Page 31: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/31.jpg)
Constraining Input• EditText from tip
calculator• input was numberDecimal• Use InputFilter to
constrain input• Several built in filters such
as AllCaps and LengthFilter
![Page 32: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/32.jpg)
Custom Input Filter• InputFilter Interface has one method:public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {• replace dstart to dend in dest with new
text from start to end of source• dest is current, source is new
• http://developer.android.com/reference/android/text/InputFilter.html
![Page 33: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/33.jpg)
![Page 34: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/34.jpg)
DIALOGS
![Page 35: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/35.jpg)
Dialogs - Old Way• Dialogs from tutorials were cut and paste• Implementing Dialogs demonstrates
evolution of Android SDK• legacy approach has Activity manage its
own Dialogs• created, initialized, updated, and
destroyed using Activity class call back methods
![Page 36: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/36.jpg)
Dialogs - New Way• Android evolving from smartphone OS
to smart device OS• API level 11 (Android 3.0, the tablet release)
introduced Fragments• A fragment represents a behavior or a portion
of a UI in an Activity– like a sub activity
• multiple fragments combined in multi-pane UI• reuse fragments in multiple activities
![Page 37: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/37.jpg)
Fragments
![Page 38: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/38.jpg)
Dialogs as Fragments• Dialogs are special type of Fragment• managed by the FragmentManager class• still part of an activity, but lifecycle not
managed by the Activity– life cycle issues of Dialogs as Fragments will
be more difficult to deal with–must save state and restore instance
![Page 39: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/39.jpg)
39
Types of Dialogs• Used to organize information and react
to user events without creating a whole new activity
• Old Dialogs:–Dialog, AlertDialog, DatePickerDialog,
TimePickerDialog, ProgressDialog• New Dialogs:–DialogFragment
![Page 40: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/40.jpg)
40
Sample Dialogs
![Page 41: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/41.jpg)
Legacy Approach• Dialog defined in Activity it is used• Activity maintains a pool of Dialogs• showDialog() method displays Dialog• dismissDialog() method used to stop
showing a Dialog– in tutorial, when we have difficulty
• removeDialog removes from pool
![Page 42: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/42.jpg)
Legacy Approach - Steps• Define unique indentifier for the Dialog
in Activity (constants)
• implement onCreateDialog method, returns Dialog of appropriate type
![Page 43: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/43.jpg)
onCreateDialog
![Page 44: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/44.jpg)
Dialog Steps - Legacy Approach• implement onPrepareDialog() if necessary– if necessary to update dialog each time it is
displayed– for example, a time picker, update with the
current time • launch dialog with showDialog()– in tutorials done when a menu or action bar
menu item selected– could launch Dialogs for other reasons
![Page 45: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/45.jpg)
Alert Dialogs• Most common type• Title, Content Area, Action buttons (up to 3)• Content area could be message, list,
seekbar, etc.• set positive,
set negative, set neutral
![Page 46: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/46.jpg)
46
Custom Dialogs• AlertDialog very flexible, but you can
create CustomDialogs• Create a layout
![Page 47: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/47.jpg)
47
Custom Dialogs• from onCreateDialog
![Page 48: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/48.jpg)
48
Custom Dialog• Simple dialogs are dismissed with the
back button
dialog title
![Page 49: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/49.jpg)
Dialogs - Fragment Method• Decouple Dialogs from the Activity– good SE approach?– TicTacToe UI is almost 500 lines long!
• Implement a class that is a subclass of DialogFragment–DifficultyFragment– Send info to newInstance method (current
difficulty, listener for updates)–onCreateDialog now in DifficultyFragment
![Page 50: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/50.jpg)
DifficultyFragment
![Page 51: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/51.jpg)
DifficultyFragment - onCreateDialog
![Page 52: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/52.jpg)
Using DifficultyFragment• In AndroidTicTacToe create a listener to
pass to the newInstance method• create and show Dialog as part of
onOptionsItemSelected()
![Page 53: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/53.jpg)
DifficultyListener
![Page 54: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/54.jpg)
Using Fragments• Fragments added in API level 11, Android 3.0,
the tablet release• Developers behind Android think fragments
are so important that can be used in pre API 11 builds using the Android Support Library
Froyo and Gingerbread pre API 11
![Page 55: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/55.jpg)
Android Support Library (ASL)• add library to project and application• android.support.v4.app.DialogFragment– for example– instead of android.app.DialogFragment
• ASL does not supportaction bar in earlierversions of API–discover
ActionBarSherlock
![Page 56: CS378 - Mobile Computing](https://reader035.fdocuments.us/reader035/viewer/2022081604/56816108550346895dd05040/html5/thumbnails/56.jpg)
Fragment Lifecycle
• Common error:not dealing with orientation change when Dialog is open
http://developer.android.com/guide/components/fragments.html