Android User Interface Overview
Most of the material in this sec7on comes from h8p://developer.android.com/guide/topics/ui/index.html
Android User Interface
• User interface built using views and viewgroup objects
• View – Base class for widgets – Textboxes, EditText boxes, bu8ons, …
• Viewgroup – Base class for layouts – Linear, Rela7ve, Tabular, …
• View Hierarchy – Hierarchy of views and viewgroups
viewgroup
viewgroup
view view view
view view
COMP 355 (Muppala) Android UI Overview 2
User Interface Events
• User's interac7on with views/widgets generate events, requiring you to perform ac7ons in response to the events
• To be informed of UI events, you need to do one of two things: – Define an event listener and register it with the View
• More oPen than not, this is how you'll listen for events • The View class contains a collec7on of nested interfaces named On<something>Listener, each with a callback method called On<something>(). E.g., OnClickListener(), OnKeyListener(), …
– Override an exis7ng callback method for the View • This is what you should do when you've implemented your own View class and want to listen for specific events that occur within it
COMP 355 (Muppala) Android UI Overview 3
UI Events in Android
• More than one way to intercept the events from a user’s interac7on with the applica7on – Public callback methods available to handle UI events – Called by the Android framework when the respec7ve ac7on occurs
on that object – Event listeners: collec7on of nested interfaces with callbacks – Event handlers: more suitable for custom view classes
COMP 355 (Muppala) Android UI Overview 5
Event Listeners
• Interface in the View lass that contains a single callback method
• Called by the Android framework when the View to which the listener has been registered is triggered by user interac7on with the item in the UI
• Several callback methods available: onClick(), onLongClick(), onFocusChange(), onKey(), onTouch(), onCreateContextMenu()
COMP 355 (Muppala) Android UI Overview 6
Event Listeners
• onClick() – From View.OnClickListener. This is called when the user either touches the
item (when in touch mode), or focuses upon the item with the naviga7on-‐keys or trackball and presses the suitable "enter" key or presses down on the trackball.
• onLongClick() – From View.OnLongClickListener. This is called when the user either
touches and holds the item (when in touch mode), or focuses upon the item with the naviga7on-‐keys or trackball and presses and holds the suitable "enter" key or presses and holds down on the trackball (for one second).
• onFocusChange() – From View.OnFocusChangeListener. This is called when the user navigates
onto or away from the item, using the naviga7on-‐keys or trackball.
COMP 355 (Muppala) Android UI Overview 7
Event Listeners
• onKey() – From View.OnKeyListener. This is called when the user is focused on
the item and presses or releases a key on the device.
• onTouch() – From View.OnTouchListener. This is called when the user performs an
ac7on qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item).
• onCreateContextMenu() – From View.OnCreateContextMenuListener. This is called when a
Context Menu is being built (as the result of a sustained "long click"). See the discussion on context menus in Crea7ng Menus for more informa7on.
COMP 355 (Muppala) Android UI Overview 8
Event Listeners Example
public class ExampleAc7vity extends Ac7vity implements OnClickListener { protected void onCreate(Bundle savedValues) { ... Bu8on bu8on = (Bu8on)findViewById(R.id.corky); bu8on.setOnClickListener(this); } // Implement the OnClickListener callback public void onClick(View v) { // do something when the bu8on is clicked } ... }
COMP 355 (Muppala) Android UI Overview 9
Event Listeners
• Depending on the event, some event listener methods must return a (boolean) value to indicate whether they have consumed the event and the event should not be carried further. – onLongClick()
• Return true to indicate that it has handled the event and it should stop here; return false if it hasnot handled it and/or the event should con7nue to any other on-‐click listeners.
– onKey() • Return true to indicate that it has handled the event and it should stop here; return false if it has not handled it and/or the event should con7nue to any other on-‐key listeners.
COMP 355 (Muppala) Android UI Overview 10
Event Listeners
– onTouch() • This event can have mul7ple ac7ons that follow each other. So, if you return false when the down ac7on event is received, you indicate that you have not consumed the event and are also not interested in subsequent ac7ons from this event. Thus, you will not be called for any other ac7ons within the event, such as a finger gesture, or the eventual up ac7on event.
• Android will call event handlers first and then the appropriate default handlers from the class defini7on second. As such, returning true from these event listeners will stop the propaga7on of the event to other event listeners and will also block the callback to the default event handler in the View. So be certain that you want to terminate the event when you return true.
COMP 355 (Muppala) Android UI Overview 11
Event Handlers
• Android allows users to create their own custom view subclasses if the standard widgets are not sufficient.
• For custom view components created by users, events are handled using event handlers: – onKeyDown(int, KeyEvent) -‐ Called when a new key event occurs. – onKeyUp(int, KeyEvent) -‐ Called when a key up event occurs. – onTrackballEvent(Mo7onEvent) -‐ Called when a trackball mo7on event
occurs. – onTouchEvent(Mo7onEvent) -‐ Called when a touch screen mo7on
event occurs. – onFocusChanged(boolean, int, Rect) -‐ Called when the view gains or
loses focus.
COMP 355 (Muppala) Android UI Overview 12