Friday, September 30, 2011

Menus Interface in Android exam

n this final installation of the Android Views series of articles, we shall continue our exploration of another category of views - Menu views, and some additional cool views. The views discussed include:
  • Context Menu
  • Options Menu
  • AnalogClock
  • DigitalClock
  • WebView
Note: for all the examples in this article, you shall use the project created in the previous article.

Menus

Menus are useful for displaying additional options that are not directly visible on the main UI of an application. There are two main types of menus in Android:
  • Context Menu – displays information related to a particular view on an activity. In Android, to activate a context menu you press and hold on to it.
  • Options Menu – displays information related to the current activity. In Android, you activate the options menu by pressing the MENU key.
Figure 1 shows an example of an options menu in the Browser application. The option menu is displayed whenever the user presses the MENU button. The menu items displayed is dependent on the current activity that is running.

Figure 1 - The options menu in the Browser application
Figure 2 shows a context menu that is displayed when the user presses and holds on a hyperlink displayed on the page. The menu items displayed are dependent on the component or view currently selected. To activate the context menu, the user selects an item on the screen and either presses and holds it or simply presses the center button on the directional keypad.

Figure 2 - The context menu in the Browser application
To see how to implement menus in Android, add a new file to the res/layout folder and name it as menu.xml. Populate it with the following element:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 
    <Button android:id="@+id/btn1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text = "Hello, world!"
        />            
 
</LinearLayout>
Then, add a new class to the src/net.learn2develop.AndroidViews folder and name it as MenuExample.java. You will leave this file alone for now. Modify the AndroidManifest.xml file to register the new activity:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="net.learn2develop.AndroidViews"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ViewsActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <activity android:name=".MenuExample"
                  android:label="@string/app_name" />
 
    </application>
</manifest>