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.

 

opt_menu.xml

 

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

<menu xmlns:android="http://schemas.android.com/apk/res/android">

 

<item

android:id="@+id/info"

android:alphabeticShortcut="a"

android:title="Info"/>

<item

android:id="@+id/pref"

android:alphabeticShortcut="b"

android:title="Preferences"/>

<item

android:id="@+id/exit"

android:alphabeticShortcut="b"

android:title="Exit"/>

 

</menu>

 

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

super.onCreateOptionsMenu(menu);

MenuInflater menuinflater = getMenuInflater();

menuinflater.inflate(R.menu.opt_menu, 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.

 

 

info.xml

 

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

 

<TextView

android:id="@+id/Text"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

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

/>

</LinearLayout>

 

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

 

 

Info.java

 

package com.example.helloandroid;

 

import android.app.Activity;

import android.os.Bundle;

 

public class Info extends Activity{

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.info);

}

 

}

 

 

  • 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.

 

 

 

<activity

android:name="com.example.helloandroid.Info"

android:label="@string/app_name"

android:theme="@android:style/Theme.Dialog">

<intent-filter>

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

 

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

</intent-filter>

</activity>

 

  • 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