android | busy java developers guide to android: ui | ted neward
DESCRIPTION
TRANSCRIPT
Busy Android Developer's GuideBusy Android Developer's Guideto UI
Ted NewardNeward & Associates
http://www.tedneward.com | [email protected]
CredentialsCredentials
Who is this guy?– Architectural Consultant, Neudesic Software– Principal, Architect, Consultant and Mentor
ask me how I can help your project or your team– Microsoft MVP (F#, C#, Architect)– JSR 175, 277 Expert Group Member– Author
Professional F# 2.0 (w/Erickson, et al; Wrox, 2010)Effective Enterprise Java (Addison-Wesley, 2004)C# In a Nutshell (w/Drayton, et all; OReilly, 2003)SSCLI Essentials (w/Stutz, et al; OReilly, 2003)Server-Based Java Programming (Manning, 2000)
– Blog: http://blogs.tedneward.com– Papers: http://www.tedneward.com/writings– Twitter: @tedneward
ObjectivesObjectives
Our goal here today is...– ... to get familiar with the various Android UI elements– ... to come to understand XML layouts– ... to learn how to handle events within Android UIs
Activities and TasksActivities and Tasks
Activity– somewhat akin to a web page or screen– public class that extends android.app.Activity– activities have event methods for override
onCreate: activity "constructor"onDestroy: activity "finalizer"onStart/onResume/onPause/onStop/onRestart flowremember to always call up the inheritance chain
Activities and TasksActivities and Tasks
Tasks– Tasks are the logical thread of use through Activities
usually starting from the Home screen– Tasks form a "back stack" of Activity instances
"back" button removes the top card, destroys that activity– Tasks are distinct from one another
•a new task creates a new stack of cards•this means more than one Activity instance of the same type is possible
– Tasks cross applications and processesin this respect, Activities are like web pages
ViewablesViewables
View and ViewGroups– View is the base class for "widgets"– ViewGroup is the base for Composite-patterned containers
of other View objects (including other ViewGroup instances)
– Each Activity can hold one content Viewmost often, this is a ViewGroup, forming a hierarchy
– Custom Views and ViewGroupsyou can do them, but it's pretty rare
ViewablesViewables
Widgets (android.widget)– "Push Me": Button, CompoundButton, CheckBox,
ImageButton, RadioButton– Text: EditText, TextView, NumberPicker, TextSwitcher– Graphics: Gallery, ImageSwitcher, ImageView– Selectors: RatingBar, SeekBar, Spinner, Switch– Time: AnalogClock, DigitalClock, Chronometer,
CalendarView, DatePicker, TimePicker– Composite: ListView, SlidingDrawer, TabHost– Media: MediaController– Space (literally, just that--empty space!)– ... and a few more
ViewablesViewables
Layouts (android.widget)– LinearLayout– RelativeLayout– TableLayout– GridLayout– AbsoluteLayout (bad!)– FrameLayout (mostly useless!)
ViewablesViewables
Dialogs (android.app) are a special case of Activity– AlertDialog (OK/Cancel/Help, etc)– ProgressDialog (56 of 100...)– DatePickerDialog– TimePickerDialog
But Dialogs are not Activities– Dialogs are always created and displayed as part of (and
are "owned" by) an Activity; as such, they are intimately wrapped up in their host Activity's code
– AlertDialog is the workhorse here
CreationCreation
Two ways of creating UIs in Android– either create UI "by hand" in Java– or using XML-based layout (resource) file
•layout resources are not tied to an Activity; they are just a group of Views laid out in an XML file•layout resources can be "inflated" from XML into UI objects (via a system service)•Activity.setContentView() is a shortcut way to do this
Layout ResourcesLayout Resources
Layout resources are in res/layout directory– XML file name corresponds to R.layout.{filename}– Root element is (usually) a ViewGroup (Layout class)– android: namespace is mandatory– element attributes describe "setter" values– android:id is critical for manipulation
•"@+{prefix}/{name}" defines new R.{prefix}.{name} id•{prefix} is most often "id" but isn't required•IDs don't have to be unique across the entire tree (but usually are)
Layout ResourcesLayout Resources
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+my_team_ids/lstInvitees" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="6dip" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="bottom"> <TextView android:text="Add:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+my_team_ids/txtNewTeamMember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="(Email address)" android:maxLines="1" /> <ImageButton android:id="@+my_team_ids/btnInvite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/sym_action_chat" /> </LinearLayout></LinearLayout>
Layout ResourcesLayout Resources
Layout resources can be "tuned"– like all resources, layouts can be spceialized to particular
scenarios•screen size: "small", "normal", "large", "xlarge"•orientation: "port", "land"•pixel density: "ldpi", "mdpi", "hdpi", "xhdpi", "nodpi", "tvdpi"•platform version: "v3", "v4", "v7", ...
– this is one way to customize UI to different device profiles•the above qualifiers MUST be in that order•see ${SDK}/docs/guide/topics/resources/providing-resources.html
MenusMenus
Menus– Activities can also have menubar items associated with
thembest used for actions that aren't common or frequent
– Menus are often defined in layout (res/menu)menu: define a menu, a container for menu itemsitem: define a menuitem, with id, icon and title (text)group: convenience grouping of item elements (invisible)
MenusMenus
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_game" android:icon="@drawable/ic_new_game" android:title="@string/new_game" /> <item android:id="@+id/help" android:icon="@drawable/ic_help" android:title="@string/help" /></menu>
MenusMenus
ActionBar (3.0+)– A set of menus/menuitems appearing at the top
generally only useful for tablets
Event-handlingEvent-handling
Most Views/ViewGroups offer events for code to subscribe to– These are called "InputEvents"– Most of the time, this is the classic "Listener" idiom
View.OnClickListeners can be wired up in the XML layout– Most of the time, these are wired up in onCreate()
IntentsIntents
Moving from one Activity to another requires an Intent– Intent = Action (verb) + Context (target)– easiest Intent is the "launch the Activity" Intent
Intent next = new Intent(this, NextActivity.class);startActivity(next);
ThreadingThreading
Good Thread hygiene is critical here– This is a phone--you don't own the machine!– Android isn't quite like Java, and has a slightly different
"take" on the threads-and-UI positionno blocking behaviors (in general)NO UI modifications from non-UI threadIf the UI thread is inactive for more than 5 seconds, bye!
– Android provides Handlers, which can be sent Messages that will then be processed on the Activity's UI thread
– Java5 provides a few other constructs as well
Wrapping upWrapping up
"What do you know?"
SummarySummary
Android is a Java-based mobile device framework... but it's not Java... and it's not a web app technology... and it's definitely not Swing or SWT
ResourcesResources
Busy Coder’s Guide to AndroidMark Murphy, http://www.commonsware.com
Android websitehttp://developer.android.com
Presentations by this guy– Busy Android Dev's Guide to Persistence– Busy Android Dev's Guide to Communication– ... and more