What's new

how to create menus in android phone


New Techie
HOW TO CREATE MENUS IN ANDROID PHONES.  We can do it in: 1. Java code or  2.Using XML Method. Before we proceed further, let?s do it in the Java code, and the next in XML. through this way, we will see the differences and we can adopt the better way according to our programming styles. By Using the Menus in Java Codes Programming part, We need to add the following codes below in bold to our Activity, to create the Menu:  "public boolean onCreateOptionsMenu(Menu menu){ menu.add(0,EDIT_ CONTACT,0,?Edit Contact?); menu.add(0,DELETE_ CONTACT,0,?Delete Contact?); menu.add(0,EXIT,0,?Exit?); return true}. In the method add(),  we have four group argument id:  1.Group Arguement Id: With this parameter, we can associate this item with a group of others items.  2.Item Arguement Id: the ID unique item identifier.  3.Order Arguement Id: This number allows us to order the elements in the menu.  4.Title Arguement: This is the string that appears in the Menu Screen.  "public boolean onOptionsItemSelected (MenuItem item){ switch (item.getItemId()){ case EDIT_CONTACT: /* Actions in case that Edid Contacts is pressed */ return true case DELETE_CONTACT : /* Actions in case that Delete Contact is pressed */ return true }??return false }" . With the "onOptionsItemSelected method", we can control the actions of every item of the menu. In addition, we can put an Image or picture to each item in the Menu, to do this, we just need to add the following method cold in bold below to the menu.add() line:  "menu.add(0,EDIT_CONTACT, 0, ?Edit?). setIcon(R.drawable.edit_ icon)" . The Menu icons are from a drawable resources. If our Android device has physical keyboard, we would like to add shortcuts to our menu. We can add an Alphabetic shorcut (with set Alphabetic Shorcut), or a numeric shorcut (with set Numeric Shortcut). Also, we can add both with ?setShortcut? methods.  Lets see an example:  "menu.add(0,EDIT_ CONTACT,0,?edit?). setAlphabeticShortcut(?e?)"; When ?e? is pressed, the EDIT_ CONTACT option will be displayed. If you find the First Method a bit Technical, You can Try using the following codes:  "< ?xml version="1.0" encoding="utf-8"?>@Override public boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_ menu, menu); return true; }" . With the above android programming codes, you can program any sort of android menus and widget that has to do with arguements.  Alternative Rerources: Better still for Private Learning, You Can Get The Pdf Guide From Our Product Store at http://www.otechguide.com/store/products/android-programming-guide/ From Learning Android Application Programming: A Hands-On Guide to Building Android Applications From an Android developer.

Related Topics