Video Games list lab 6 At the end of this lab you will be expected to know: What Views, View...

9
Video Games list lab 6 At the end of this lab you will be expected to know: What Views, View Groups, Layouts, and Widgets are and how they relate to each other. How to declare layouts dynamically at runtime. How to reference resources in code and from other resource layout files. How to use Events and Event Listeners. CS440 1

Transcript of Video Games list lab 6 At the end of this lab you will be expected to know: What Views, View...

Page 1: Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.

CS440

1

Video Games list lab 6

At the end of this lab you will be expected to know: What Views, View Groups, Layouts, and

Widgets are and how they relate to each other.

How to declare layouts dynamically at runtime.

How to reference resources in code and from other resource layout files.

How to use Events and Event Listeners.

Page 2: Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.

CS440

2

Part 3: Game List

Modify the view: Goto main.xml Delete the current view Add a text editor with ID GameText, a

button with ID btnAdd and a listview with ID GameListView

Add these widgets as properties in the GameListActivity class

Find the objects: edit text, button and list view using findViewById (look at the android API for more information)

Set key listener for the edit text and click listener for the button

Page 3: Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.

CS440

3

Part 3: Game List

Create GameList property as an ArrayList of Strings

Create an Array Adapter aa property(see android API)

Create addGame(String GameTitle) method in the GameListActivity class Add the game title to the list Notify the adapter that the data set has

changed (see android API) Clear the text for the Edit Text widget

Page 4: Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.

CS440

4

Part 4: Add event listeners

The GameListActivity class needs to implement two listeners: OnClickListener, OnKeyListener

Page 5: Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.

CS440

5

Part 4: Add event listeners

Setup the onClickListener for the "Add Game" Button. When the user hits the "Add Game" Button

a new Game object should be initialized with the text from the EditText field and added to your game list. If the EditText is empty, then the Button should do nothing.

public void onClick(View v): Check if the button is pressed Add game title to the list

Page 6: Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.

CS440

6

Part 4: Add event listeners

Setup the onKey listener public boolean onKey(View v, int

KeyCode, KeyEvent event) : Check if the key is pressed (ACTION_DOWN,

KEYCODE_DPAD CENTER) Add game title to the list

Page 7: Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.

CS440

7

Add event listeners with anonymous class (a different approach) If you are unfamiliar with anonymous

inner classes that's alright. Essentially, its an inline way of creating a one-time-use class that implements some interface. You declare the class and instantiate it in one motion. You can read more about them from Sun's Java Tutorials on Anonymous Inner Classes.

Copy the following code:GameButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) {

//Implement code to add a new game here... } });

Page 8: Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.

CS440

8

Part 4: Add event listeners

Fill in the onClick method in the anonymous inner class that you created to add a new game to the list. Retrieve the text entered by the user into

the EditText by calling getText().toString() (Check for empty strings here).

Clear the EditText. Call your addGame method Call initAddGameListeners from onCreate. Run your application to ensure that the

"Add Game" Button functions properly.

Page 9: Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.

CS440

9

References

Professional Android 2 Application Development (Wrox Programmer to Programmer) by Reto Meier