RSS

Tag Archives: tutorials

Creating a menu

A menu is the component that pops up when you press the menu button. The design of the menu is held as an xml and then implemented in java. So to do it, I created a separate folder known as menu that will contain the menu files ie nice_menu.xml. Eclipse automatically detects that you are creating a menu and when you create a new xml it will open a file that looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title="About"
android:id="@+id/about"
/>
<item
android:title="Preference"
android:id="@+id/preferences"
/>
<item
android:title="Exit"
android:id="@+id/exit"
/>

</menu>

<item /> adds a label like the preferences or exit. The id is so that you can reference in java and add methods to detect when that particular option has been selected. There are more stuff here, Its a great read like you could add icons, I’ll do that in the next blog post along with the method that detects which item has been selected. For now the menu does nothing when clicked.

Going to the java side of this. We need to inflate the menu (show the menu on screen). You have to implement the onCreateOptionsMenu(Menu).

public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		MenuInflater showMenu = getMenuInflater();
		showMenu.inflate(R.menu.nice_menu, menu);

		return true;
	}

To view the complete source code for the camera app with menu included: Click here

Advertisements
 
2 Comments

Posted by on July 1, 2012 in Android, Code

 

Tags: ,