Android Bootcamp
SESSION 9 – TOUCH EVENTS
Agenda
 Event Handlers & Event Listeners
 Building Menus
 Fling Gestures
 Multi Touch events
 Animation
 Styles
2
Event Handlers and Event Listeners
 Most user interaction with an Android device is captured
by the system and sent to a ...
Intercepting a Physical Key Press 4
Source: Android Cookbook Text Book (Refer Handout)
Intercepting a Physical Key Press
 The system first sends any KeyEvent to the appropriate callback method
in the in-focus...
Intercepting a Physical Key Press
 This is done by intercepting the method for the event
KeyEvent.KEYCODE_CAMERA and retu...
Building Menus
 A developer can implement three types of menus in Android
 Options menu—The main menu for an Activity th...
Building Menus
 the onPrepareOptionsMenu() can be used if any of the menu options
need to change during run-time
 When a...
Listening for Fling Gestures
 The possible gestures in the OnGestureListener interface are
 onDown()—Notifies when a tap...
Listening for Fling Gestures
 A fling consists of two events: a touch down (the first MotionEvent)
and a release (the sec...
Using Multi touch
 A multi touch event is when more than one pointer (such as a
finger) touches the screen at the same ti...
Style Resource
 A style resource defines the format and look for a UI.
 A style can be applied to an individual View (fr...
Style Resource
 EXAMPLE:
XML file for the style (saved in res/values/):
<?xml version="1.0" encoding="utf-8"?>
<resources...
Apply a theme to an Activity or application
 To set a theme for all the activities of your application, open the AndroidM...
Android Animation
 Android provides two types of animation: frame-by-frame and Tween animation.
 Frame by frame animatio...
Android Animation
 To display the frame-by-frame animation, set the animation to a
view’s background:
 ImageView im = (I...
Android Animation
 Tween animation uses a different approach that creates an animation
by performing a series of transfor...
Upcoming SlideShare
Loading in …5
×

Android Training in Bangalore By MobiGnosis

506 views

Published on

Android Training in Bangalore Presentation - for designing and developing mobile applications using Android™ open-source platform only in 2 DAYS. For more info visit
http://www.mobignosis.com/android-development-training/

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
506
On SlideShare
0
From Embeds
0
Number of Embeds
51
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Android Training in Bangalore By MobiGnosis

  1. 1. Android Bootcamp SESSION 9 – TOUCH EVENTS
  2. 2. Agenda  Event Handlers & Event Listeners  Building Menus  Fling Gestures  Multi Touch events  Animation  Styles 2
  3. 3. Event Handlers and Event Listeners  Most user interaction with an Android device is captured by the system and sent to a corresponding callback method.  These events can be handled by extending the class and overriding the methods, called event handlers.  User interaction with View or ViewGroup objects can also support event listeners.  For example, the setOnClickListener() event listener can be registered for a button and when it is pressed, the onClick() method is called. 3
  4. 4. Intercepting a Physical Key Press 4 Source: Android Cookbook Text Book (Refer Handout)
  5. 5. Intercepting a Physical Key Press  The system first sends any KeyEvent to the appropriate callback method in the in-focus activity or view.  These callback methods are :  onKeyUp(), onKeyDown(), onKeyLongPress()—Physical key press callbacks.  onTrackballEvent(), onTouchEvent()—Trackball and touchscreen press callbacks  onFocusChanged()—Called when the view gains or loses focus  These can be overridden by the application to customize with different actions.  For example, to turn off the camera button (to avoid accidental presses), just consume the event in the onKeyDown() callback method for the Activity. 5
  6. 6. Intercepting a Physical Key Press  This is done by intercepting the method for the event KeyEvent.KEYCODE_CAMERA and returning true:  public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_CAMERA) { return true; // consume event, hence do nothing on camera button } return super.onKeyDown(keyCode, event); }  By consuming the event, it does not get passed on to other Android components.  There are a few exceptions to this:  The Power button and HOME key are intercepted by the system and do not reach the application for customization.  The BACK, MENU, HOME, and SEARCH keys should not intercept the KeyDown but instead the KeyUp. 6
  7. 7. Building Menus  A developer can implement three types of menus in Android  Options menu—The main menu for an Activity that displays when the MENU key is pressed.  Context Menu—A floating list of menu items that displays when a view is long pressed.  Submenu—A floating list of menu items that displays when a menu item is pressed.  The Options menu is created the first time the MENU key is pressed in an activity.  This launches the onCreateOptionsMenu() method that usually contains Menu methods, such as:  menu.add(GROUP_DEFAULT, MENU_ADD, 0, "Add") .setIcon(R.drawable.icon); 7
  8. 8. Building Menus  the onPrepareOptionsMenu() can be used if any of the menu options need to change during run-time  When an item from the options menu is clicked, the onOptionsItemSelected() method is called.  This passes the selected item ID, and a switch statement can be used to determine which option was selected. Utilizing Search KEY  If an activity in the in-focus application is defined to be searchable, the SEARCH key invokes it.  The menu choice simply needs a call to onSearchRequested().  The searchable activity ideally should be declared as singleTop launch mode. 8
  9. 9. Listening for Fling Gestures  The possible gestures in the OnGestureListener interface are  onDown()—Notifies when a tap down event occurs  onFling()—Notifies when a tap down, movement, and matching up event occurs  onLongPress()—Notifies when a long press occurs  onScroll()—Notifies when a scroll occurs  onShowPress()—Notifies when a tap down occurs before any movement or release  onSingleTapUp()—Notifies when a tap up event occurs  When only a subset of gestures are needed, the SimpleOnGestureListener class can be extended instead.  It returns false for any of the previous methods not explicitly implemented. 9
  10. 10. Listening for Fling Gestures  A fling consists of two events: a touch down (the first MotionEvent) and a release (the second MotionEvent).  Each motion event has a specified location on the screen given by an (x,y) coordinate pair, where x is the horizontal axis and y is the vertical axis.  The (x,y) velocity of the event is also provided. 10
  11. 11. Using Multi touch  A multi touch event is when more than one pointer (such as a finger) touches the screen at the same time.  This is identified by using a touch listener OnTouchListener, which receives multiple types of motion events:  ACTION_DOWN—A press gesture has started with a primary pointer (finger).  ACTION_POINTER_DOWN—A secondary pointer (finger) has gone down.  ACTION_MOVE—A change in press location has changed during a press gesture.  ACTION_POINTER_UP—A secondary pointer was released.  ACTION_UP—A primary pointer was released, and the press gesture has completed. 11
  12. 12. Style Resource  A style resource defines the format and look for a UI.  A style can be applied to an individual View (from within a layout file) or to an entire Activity or application (from within the manifest file).  file location : res/values/filename.xml The filename is arbitrary. The element's name will be used as the resource ID.  resource reference: In XML: @[package:]style/style_name  syntax: <?xml version="1.0" encoding="utf-8"?> <resources> <style name="style_name" parent="@[package:]style/style_to_inherit"> <item name="[package:]style_property_name" >style_value</item> </style> </resources> 12
  13. 13. Style Resource  EXAMPLE: XML file for the style (saved in res/values/): <?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomText" parent="@style/Text"> <item name="android:textSize">20sp</item> <item name="android:textColor">#008</item> </style> </resources> XML file that applies the style to a TextView (saved in res/layout/): <?xml version="1.0" encoding="utf-8"?> <EditText style="@style/CustomText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello, World!" /> 13
  14. 14. Apply a theme to an Activity or application  To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the <application> tag to include the android:theme attribute with the style name. For example:  <application android:theme="@style/CustomTheme">  If you want a theme applied to just one Activity in your application, then add the android:theme attribute to the <activity> tag instead.  <activity android:theme="@style/CustomTheme">  Just as Android provides other built-in resources, there are many pre-defined themes that you can use, to avoid writing them yourself.  For example, you can use the Dialog theme and make your Activity appear like a dialog box:  <activity android:theme="@android:style/Theme.Dialog">  if you want the background to be transparent, use the Translucent theme:  <activity android:theme="@android:style/Theme.Translucent"> 14
  15. 15. Android Animation  Android provides two types of animation: frame-by-frame and Tween animation.  Frame by frame animation shows a sequence of pictures in order.  It enables developers to define the pictures to display, and then show them like a slideshow  Frame-by-frame animation first needs an animation-list element in the layout file containing a list of item elements specifying an ordered list of the different pictures to display.  <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/anddev1" android:duration="200" /> <item android:drawable="@drawable/anddev2" android:duration="200" /> <item android:drawable="@drawable/anddev3" android:duration="200" /> </animation-list> 15
  16. 16. Android Animation  To display the frame-by-frame animation, set the animation to a view’s background:  ImageView im = (ImageView) this.findViewById(R.id.myanimated); im.setBackgroundResource(R.anim.animated); AnimationDrawable ad = (AnimationDrawable)im.getBackground(); ad.start();  After the view background is set, a drawable can be retrieved by calling getBackground() and casting it to AnimationDrawable.Then, calling the start() method starts the animation. 16
  17. 17. Android Animation  Tween animation uses a different approach that creates an animation by performing a series of transformations on a single image.  In Android, it provides access to the following classes that are the basis for all the animations:  AlphaAnimation—Controls transparency changes  RotateAnimation—Controls rotations  ScaleAnimation—Controls growing or shrinking  TranslateAnimation—Controls position changes  These four Animation classes can be used for transitions between activities, layouts, views and so on.  All these can be defined in the layout XML file as <alpha>, <rotate>, <scale>,and <translate>.  They all have to be contained within an AnimationSet <set> 17

×