Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Topics Custom ArrayAdapters Creating an Event Listener

Similar presentations


Presentation on theme: "Android Topics Custom ArrayAdapters Creating an Event Listener"— Presentation transcript:

1 Android Topics Custom ArrayAdapters Creating an Event Listener
In-class App: Contact App using our own custom ArrayAdapter Lab 5 Part 4. OnClickListener Interface. Creating an Event Listener. Complete the App from Lab 5 Part 4.

2 In-class App This app will help Moquelumnan language students practice their number (1-10) skills. Each number includes an image, the written English name, the numeric value, and an audio sound that will play when the user taps directly on the list item. The activity_number layout must produce a scrollable listing without using a ScrollView. Use a ListView powered by a custom ArrayAdapter. The audio and image files can be downloaded from the website.

3 Explore OnClickListener
Question: What did you find?

4 Explore OnClickListener
Question: What did you find? Answer: OnClickListener is an interface. onClick() is the abstract method. NOTE: Implementation for abstract methods is intentionally left blank so that a developer wanting to use the interface can provide their own specific instructions for it.

5 Review of Classes and Interface
Concrete class: if you come across a concrete class in Android you can use it right away. Abstract class: This is a partially implemented class and it contains states and some methods that are fully implemented. Some methods are left abstract so that coders can define the behavior as they choose. Interface: If you come across an interface, you need to provide code for all the abstract methods. Think of these elements as a continuum where an interface is not implemented at all, an abstract class is partially implemented, and a concrete class is fully implemented.

6 Concrete Class public class TextView{ String mText; int mTextColorID;
void setText( String str){ mText = str; } void setTextColor ( int color) { mTextColorID = color; …. Concrete Class

7 Abstract Class public abstract class ViewGroup { int nChildCount;
void addView (View view){ addViewInternal (view, -1); nChildCount++; } void removeView (View view) { removeViewInternal (view); nChildCount--; void onLayout() ; …. Abstract Class

8 Interface public interface OnClickListener { void onClick (View view);
}

9 Creating an Event Listener
As a mobile developer, you will need to set up event listeners for the input events you want to handle. In a simple app, you can easily utilize the onClick property for a given view in your layout.

10 Explore onClick The onClick() method is a callback method.
onClick() is automatically invoked when a registered event is triggered. onClick() has exactly one parameter, the View object being clicked on. NOTE: Remember, for an interface, you need to provide for all the abstract methods.

11 How to set up an Event Listener?

12

13 In MainActivity.java 3. Define the event listener inline (and the custom behavior for then the event is triggered. 1. Attach the listener to a View. 2. Create new object instance of event listener. buttonView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(view.getContext(), “Hello”, Toast.LENGTH_SHORT).show(); } });

14 Numbers App: Event Listener
In the Numbers App, it is less clear which View has triggered a listener event. Research OnItemClickListener. What did you find?

15 OnItemClickListener is an interface definition for a callback to be invoked when an item in an AdapterView has been clicked. There is only one public method. This method is a callback method, onItemClick().

16 Quick Review Similarities
setOnClickListener vs. setOnItemClickListener: OnClickListener vs OnItemClickListener: onClick vs onItemClick: Both of these register a listener that will trigger an event. Both are interfaces for abstract listener events. Both are abstract methods for handling a listener event.

17 Quick Review Differences
onItemClick: A Callback method to be invoked when an item in an AdapterView has been clicked. onClick: A Callback method to be invoked when a View within an Activity layout has been clicked.

18 Tasks for implementing an Event Listener for ListView+ArrayAdapter:
Register a listener using setOnItemClickListener. Implement onItemClick() from OnItemClickListener(). Call getItem (position) to access the object clicked. onItemClick() parameters:

19 // CREATE AN ADAPTER AND LOAD IT WITH DATA OBJECTS // ATTACH THE LISTVIEW TO THE ADAPTER NumberWordAdapter itemsAdapter = new NumberWordAdapter(this, words); listView.setAdapter(itemsAdapter); //CREATE AN EVENT LISTENER TO RESPOND TO A CLICKED LISTVIEW ITEM listView.setOnItemClickListener(new AdapterView.OnItemClickListener() public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { String myWord = words.get(position).name); } }); } AdapterView where the click happened. The view within the AdapterView that was clicked. The position of the view in the adapter. The row id of the item that was clicked

20 Experiment with the Numbers App
Experiment with the Numbers App. Display a Toast containing the audio id of a clicked ListView item.

21 Review MediaPlayer Examine MediaPlayer documentation Question:
What are the different states that we can transition between as the media player plays a sound file?

22 The media player object is initially in an idle state
The media player object is initially in an idle state. It is sitting there Idle, waiting for instructions. In the Prepared state, a song has been assigned. The media player is preparing to play a certain audio file. No sounds are made. In Started state the media player is playing an audio file. Pause state will produce a pause. From the Paused state can return to Started state to resume where an audio was left off . Stop can halt the audio. Once stop the media player has stopped, it can’t be resume. It needs to enter the Prepared state.

23 Handling Touch Events on a ListView

24 Complete the Moquelumnan language app.


Download ppt "Android Topics Custom ArrayAdapters Creating an Event Listener"

Similar presentations


Ads by Google