Programming for Mobile II

Spinner (Dropdown)

  • A Spinner is the Android term for a dropdown interface, like select in HTML
  • The Android documentation recomends if you have more choices than can fit on a screen horizonitally, to use a Spinner rather than a CheckBox
  • The entries for a Spinner are defined either programatically or through a seperate XML file with the choices
    <Spinner
    android:entries="@array/ChoicesFromFile" />
    

Images

  • To display an image, the XML is almost idenctical to an ImageButton
  • The element is named ImageView
  • Once again, the location of the image it defined by the android:src attribute
<ImageView
        android:src="@drawable/ImageFile" />

The Static Class R

  • As part of the building process, a static class known as R is dynamically created
  • This class holds constants that refer to all the resources specified in the res directory
  • For example, to get the numerical id associated with the id string "myId"
    int numericalId = R.id.myId;
    

The Activity Class

  • As mentioned last class, each screen in an application is known as an activity
  • All activities have their own class, which inherits from Activity or one of Activity's subclasses
  • There are several methods that should be overridden, the most important being onCreate
    • This is the first method called when the Activity starts up
    • This method is responsible for specifiying the layout file and setting up any event listeners usually.

Acessing Elements

  • Another method inherited from the Activity class is findViewById
    • This method takes an integer id and returns the View object coressponding to the UI element
    • Use the R class to get the interger Id
  • This method returns a View object, but the element we are getting is usually a subclass of View
    • The return value should be casted to the appropriate subclass

Input Events

  • Android provides two ways to attach event handlers
    • The attachment method dictates how the event handler is implemented
  • The event handler is always passed an object of the View class, which is the base class for all UI elements
    • This object represents the specific UI element that the event was triggered from
  • Common events for input are onClick, onLongClick, onKey, and onFocusChanged

Attaching Events in the XML

  • The simplest way to attach event handlers is through use on an XML attribute
  • To attach the handler for the onClick event to a button, use the following:
    <Button
                  android:text="New Button"
                  android:id="@+id/button"
                  android:onClick="aMethod"/>
    
  • The value of android:onClick is the name of a method located in the activity class
    • The method must be public and return void
    • The method must take one parameter, a View object

XML Events Practice

  • Populate a TextView with which button is being held in.

Attaching Events Programatically

  • Attaching events in XML is convient, but quickly becomes hard to manage
  • It is also necesarry sometimes to change the event handlers dynamically
  • Android offers methods to attach events programatically
    Button myButton = (Button) getViewById('myButtonId');
    myButton.setOnClickListener(new MyListenerObject());
    

Listener Classes

  • The MyListenerObject in the previous code is a class created by the programmer
  • The class must implement the approriate Listener interface.
    • For an onClick event, the class will implement OnClickListener
    • Each interface only has one method to override, which is named after the event, ie onCLick
      class MyListenerObject implements OnClickListener{
            public void onClick(View v) {
                \\Respond to event
            }
      }
      

Dynamic Event Attachment Practice

  • Populate a TextView with which button is being held in.

Events on the Activity Itself

  • Sometimes it is useful to list to events on the activity
  • These can serve as default events
    • ie onTouchEvent will handle all clicks,etc not handled by the UI elements themselves
  • Some events are to enable your program to be a "good citizen" in the OS enviroment
    • The onLowMemory event is triggered when the OS is low on memory and all applications should evaluate their memory and free any unneeded memory.
  • All events on the Activity are methods implemented directly in the activity class

Sensors

  • The hardware on Android devices is usually equipped with various sensors that can also cause events
  • These event must be attached programatically
  • The events are registed with a SensorManager object by passing the listener object and the sensor to listen for events on
    • SensorManager is provided by the OS and retrieved using
      mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
      

SensorEventListener

  • Just like for input events, the sensor events are handled by classes that implement SensorEventListener
  • This provides two methods that need to be overridden
    • onSensorChanged(SensorEvent event) which is called when the sensor value changes
    • onAccuracyChanged(SensorEvent event, int accuracy) which is called when the accuracy of the sensor changes for some reason

Attaching the Sensor Event Listener

  • To attach the listener, an object representing the sensor must be retrieved from the sensor manager object

    temp = mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE);
    
  • The listener is then attached using registerListener and passing the listener, the sensor, and the delay between sensor events

mySensorManager.registerListener(new SensorEventListener(), temperature, SensorManager.SENSOR_DELAY_NORMAL);