Unit - 3 Providing Users with Options and Context Menus


Lesson Plan: Unit - 03
Subject: P15A2AAD - Android Application Development
Topic of Study: Providing Users with Options and Context Menus
Grade/Level: Master of Computer Applications
Objective: To understand Options and Context Menus with properties and events
Time Allotment: 55 Minutes
  • Providing Users with Options menus.
    • You need to be aware of two special application menus for use within your Android applications: the options menu and the context menu.
    • The Android SDK provides a method for users to bring up a menu by pressing the menu key from within the application called option menu.
    • The OptionsMenu control can contain icons, submenus, and keyboard shortcuts.
    • What is MenuInflator?
      • It is used to instantiate menu XML files into Menu objects.
      • Methods:
      • In Java Code:
    • How to create Options menu.
  1. Create menu directory under res folder.
  2. Right click on menu directory. select NEW > Menu resource file.
  3. Write a menu name (menu.xml).
  4. We have two options. 1. Text 2. Design. 1. Text mode is used to write a tags. 2. Design mode is used to design menus.
  5. Here, we used design mode. 5.1 add a menu item from pallets. 5.2 change its id and title properties. 5.3 Repeat 5.1 & 5.2 as per need.
  6. Move to MainActivity.java file - Create OnCreateOptionMenu method.
  7. Create OnOptionsItemSelected method.
  • Using Context menus.
    • The ContextMenu is a subtype of Menu that you can configure to display when a long press is performed on a View.
    • ContextMenu provides for contextual menus to display to the user for performing additional actions on selected items.
    • ContextMenu objects are slightly more complex than OptionsMenu objects.
    • You need to implement the onCreateContextMenu() method of your Activity for one to display.
    • However, before that is called, you must call the registerForContextMenu() method and pass in the View for which you want to have a context menu.
    • Step 01: Design a menu (timer.xml)
    • Step 02: Design in XML.

    • Step 03:Write a code in JAVA file.

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form