Unit-3 Session 3
Unit-3 Session 3
Development
CREATING NEW VIEWS
overriding the onDraw method, and implement constructors that call a new init
method stub.
2. Create a new colors.xml resource in the res/values folder. Create new color
3. Create a new dimens.xml resource file and add a new value for the paper’s
margin width.
appearance. Create new private instance variables to store the Paint objects
you’ll use to draw the paper background and margin. Also create variables for
In order for your new View to be interactive, it will need to respond to user
events like key presses, screen touches, and button clicks.
Android exposes several virtual event handlers, listed here, that let you
react to user input:
onKeyDown Called when any device key is pressed; includes the D-pad,
keyboard, hang-up, call, back, and camera buttons
onKeyUp Called when a user releases a pressed key
<com.paad.compass.CompassView
android:id=”@+id/compassView”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
/>
inflate the layout and get a reference to the CompassView, as usual, using the
following code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CompassView cv = (CompassView)this.findViewById(R.id.compassView);
cv.setBearing(45);
}
You can also add your new view to a layout in code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CompassView cv = new CompassView(this);
setContentView(cv);
cv.setBearing(45);
}
INTRODUCING ADAPTERS
Adapters are used to bind data to View Groups that extend the
AdapterView (such as listView or gallery) class.
Adapters are responsible for creating child Views that represent
the underlying data within the bound parent View.
Introducing Some Native Adapters
Android supplies a set of Adapters that can pump data from common data
sources (including arrays and Cursors) into the native controls that extend
Adapter View.
ArrayAdapter — The Array Adapter uses generics to bind an Adapter View
to an array of objects of the specified class. By default, the Array Adapter uses
the toString value of each object in the array to create and populate Text
Views.
SimpleCursorAdapter — The Simple Cursor Adapter enables you to bind the
Views within a layout to specific columns contained within a Cursor.
Customizing the Array Adapter
Array Adapter uses the toString values of each item within an object array to
populate a Text View within the layout.
Array Adapter to populate the layout used for each View to represent the
underlying array data.
The getView method is used to construct, inflate, and populate the View that will
be added to the parent Adapter View class (e.g., List View), which is being bound
to the underlying array using this Adapter.
The getView method receives parameters that describe the position of the item
to be displayed, the View being updated (or null), and the View Group into which
this new View will be placed.
A call to getItem will return the value stored at the specified index in the
underlying array.
Return the newly created and populated (or updated) View instance as a result
from this method.
Using Adapters to Bind Data to a View
second (equally sized) array that contains resource IDs to specify which
Views within the layout should be used to display the contents of the
corresponding columns.