Pr.Pg Next Pg

Application with Options Menu tutorials


In this tutorial we are going to add Options Menu to our Hello Android Application

  • We will have Three Option items namely Info, Preferences and Exit.

  • Info will say something about the application, preferences will set the user preferences and Exit will exit the application.

  • Creating the XML file

  • We create a new folder named menu inside the res folder.

  • If the folder was already created then its well and fine.

  • We will define all our menus inside this folder.

  • We then create a new XML file for our Options Menu named opt_menu.xml.

  • Below is the XML code for this file.




<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="">

















We can also set the icon for each option using the android:icon atttribute.

  • Adding the Options Menu to the Activity

  • We now add our Options Menu to our MainActvity activity.

  • We override the method onCreateOptionsMenu()





public boolean onCreateOptionsMenu(Menu menu) {

// TODO Auto-generated method stub


MenuInflater menuinflater = getMenuInflater();

menuinflater.inflate(, menu);

return true;


  • We create a new MenuInflator object that inflate the menu.

  • We then call the inflate our options menu using the inflate() method which takes in as the parameter the integer which refers to the opt_menu we just created.

  • Finally we return a boolean value true so that the menu is displayed.

  • Action performed when the Item is clicked

  • When the Info options is clicked we will display a text giving information about the application.

  • For this purpose we start a new activity named Info.

  • We also create a new layout for the activity.

  • In the layout we will have a TextView with the text as whatever information we want to add and then we also set the theme for the activity such that it looks like a dialogue.

Below is the code for XML layout for Info named info.xml.





<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""









android:text="This application has buttons and textviews which illustrate the onCLick functionality of the button. This application also has an Option Menu which will show the information about the application, set the preferences and also help user exit the application."




Below is the code for the java file of the Info activity.


package com.example.helloandroid;



import android.os.Bundle;


public class Info extends Activity{


protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub








  • In this class we have just set the Content View to our layout in the onCreate() method of the activity.

  • Next we add an intent into our switch case to start the activity when the Info options is clicked.

  • We also have to add our activity in the AndroidManifest.xml file.









<action android:name="com.example.helloandroid.INFO" />


<category android:name="android.intent.category.DEFAULT" />




  • As you can notice we have introduced a new attribute named android:theme.

  • This attribute is used to add a theme to our activity.

  • Since we want our Info to appear as a dialog we have set the theme as shown.

  • When we run the application, and click on Info in the options menu we will see the following



Pr.Pg border                                              Next Pg