Friday, June 21, 2013

Android 1: Lesson 9

Android 1: Lesson 9: List Menu


INTRODUCTION
In this lesson you will be creating a Menu that will list all of your activities in your app and allows you to pick which one to start
LESSON OBJECTIVES
By the end of this lesson, you will be able to:
1. Create a List Menu
2. To expand your app with more Activities
3. Know more about the Manifest
LEARNING SEQUENCE



Required Reading
Read the following:
Introduction To Development Environment
·        Eclipse
·        Android
·        Considerations for beginning programming.
Resources
View the following:
1.      Eclipse
2.     Android Development
Assignments
  1. Create a new class called ‘Menu’
  2. After Menu, type ‘extends ListActivity’, import it
  3. Below that line of text, right click and go to ‘source’ and go ‘override’ and you’ll see ‘ListActivity’ which was imported. Select ‘onCreate(Bundle)’ and hit ok.
  4. go below the ‘}’ and do the same as above, but select ‘onListItemClick’
  5. Inbetween the public class at top, and the override, Type out ‘String classes[] = { “MainActivity”, “example1”, “example2”};’ You can put as many examples as you want. This is what the Menu will actually show. The names should be the names of the classes
  6. Below the first override, under the super type ‘setListAdapter(new ArrayAdapter(Men.this, android.R.layout.));’ after the ‘layout.’ you’ll want to pick the ‘simple_list_item_1’ this will put the code in automatically for you. After that type’, classes’
  7. Ok now move below the second override. Type ‘String xxx = classes[postion];’ xxx can be anything you want. It’s just making it easier for you in the code to follow.
  8. Enter, ‘try{‘
  9. Enter ‘Class ourClass =  Class.forName(“xxx” + easy); xxx should be your package, make sure there is a period after the package and before the final “
  10. Enter ‘Intent ourIntent = new Intent(Menu.this, ourClass);’ import the intent
  11. Enter ‘startActivity(ourIntent);’
  12. After the ‘}’ right below the code you just typed, type ‘catch (ClassNotFoundException e){‘
  13. Enter ‘e.printStackTrace();’
  14. Now open up your ‘AndroidManifest.xml’, you’ll need to add the menu to it.
  15. Copy the MainActivity activity, and paste it above
  16. Change MainActivity to the name of your Menu class.
  17. Also change ‘.STARTINGPOINT’ to ‘.MENU’ save and export
  18. Congrats! you made a menu! export the app and test it!

No comments:

Post a Comment