Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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. CS440 1

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 CS440 2

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 CS440 3

4 Part 4: Add event listeners  The GameListActivity class needs to implement two listeners:  OnClickListener,  OnKeyListener CS440 4

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 CS440 5

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 CS440 6

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: CS440 7 GameButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { //Implement code to add a new game here... } });

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. CS440 8

9 References  Professional Android 2 Application Development (Wrox Programmer to Programmer) by Reto Meier CS440 9


Download ppt "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."

Similar presentations


Ads by Google