Software Development Exam  >  Software Development Notes  >  How to Implement Options Menu in Android

How to Implement Options Menu in Android - Software Development PDF Download

How to Implement Options Menu in Android?

Last Updated: 09 Feb, 2023

In Android, there are three types of menus available to define a set of options and actions in our Android applications. The menus in Android applications are as follows:

Menus
  • Android Options Menu
  • Android Context Menu
  • Android Popup Menu

Android Options Menu

  • Android Options Menu
  • Android Context Menu
  • Android Popup Menu

Android Option Menus are the primary menus in Android. They can be utilized for settings, searching, deleting items, etc. The appearance of this item as an action item in the app bar is determined by the 'Show Action' attribute. The values that can be assigned to the 'showAsAction' attribute are:

Android Option MenusShow ActionshowAsAction attribute
  • always: This ensures that the menu always appears in the action bar.

Syntax:

Example:

Example:
  • XML
<item android:id="@id/message" android:icon="@android:drawable/ic_menu_send" app:showAsAction="always" android:title="message" />
How to Implement Options Menu in Android - Software Development

never: This indicates that the menu will never be displayed and will be accessible through the overflow menu.

Options Menu in Android

The Options Menu in Android provides a way for users to access key functionalities and settings within an app. Let's delve into how it can be implemented effectively.

Example:

Below is the complete code for implementing the Options Menu in Android:

  • Java
  • Kotlin

Implementation Steps

  • Define the menu items in an XML file using the menu tag.
  • Specify the namespace declarations for Android attributes like xmlns:android, xmlns:app, and xmlns:tools.
  • Assign unique IDs, icons, titles, and showAsAction attributes to each menu item.
  • Ensure proper linkage to the main activity using the tools:context attribute.

XML Structure

The XML structure for defining the Options Menu in Android involves specifying various attributes for each menu item:

Item IDIconTitleShowAsAction
@id/message@android:drawable/ic_menu_sendmessagealways
@id/picture@android:drawable/ic_menu_gallerypicturealways|withText
@id/mode@android:drawable/ic_menu_callmodealways
@id/about@android:drawable/ic_dialog_infocalculatornever|withText
@id/exitexitnever

By following these steps, you can create a functional and user-friendly Options Menu in your Android application.

Understanding the MainActivity Class in Android Development

  • Import Statements:

    The MainActivity class in Android development requires various import statements to access necessary functionalities:

    • androidx.appcompat.app.AppCompatActivity: This statement imports the AppCompatActivity class from the AndroidX library, allowing the MainActivity to inherit its features.
    • android.os.Bundle: It imports the Bundle class, which is used for passing data between activities.
    • android.view.Menu and android.view.MenuItem: These are essential for creating the app's menu.
    • android.widget.Toast: This allows the application to display brief messages to the user.
    • static android.widget.Toast.LENGTH_LONG: Sets the duration for which a toast message will be displayed.
  • MainActivity Class Structure:

    The MainActivity class extends AppCompatActivity and contains various methods:

    • onCreate(Bundle savedInstanceState): This method is called when the activity is created. It initializes the activity's layout using setContentView().
    • onCreateOptionsMenu(Menu menu): This method is used to inflate the menu resource defined in XML.
    • onOptionsItemSelected(MenuItem item): This method is invoked when a menu item is selected. It handles different menu item clicks using a switch-case structure.
  • Handling Menu Item Selections:

    Within the onOptionsItemSelected() method, specific actions are taken based on the selected menu item:

    • R.id.message: Displays a toast message indicating a share icon.
    • R.id.picture: Shows an image icon and starts a new activity.
    • R.id.mode: Indicates a call icon is displayed.
    • R.id.about: Shows a message related to the calculator menu.
    • R.id.exit: Closes the current activity.

Android Development Basics

  • android.os.Bundle: A class in Android that is used for passing data between activities.
  • android.view.Menu: Represents the options menu of an activity.
  • android.view.MenuItem: Represents a single item in a menu.
  • android.widget.Toast: Provides simple feedback about an operation in a small popup.
  • androidx.appcompat.app.AppCompatActivity: Base class for activities that use the support library action bar features.

MainActivity Class Overview

  • MainActivity : AppCompatActivity(): The main activity class that extends AppCompatActivity.
  • onCreate(savedInstanceState: Bundle?): Method called when the activity is starting.
  • setContentView(R.layout.main): Sets the activity content from a layout resource.

Menu Handling in MainActivity

  • onCreateOptionsMenu(menu: Menu): Called to create the options menu.
  • onOptionsItemSelected(item: MenuItem): Called when a menu item is selected.

Menu Item Actions

  • R.id.message: Shows a message with a share icon.
  • R.id.picture: Shows a message with an image icon and starts an activity.
  • R.id.mode: Shows a message with a call icon.
  • R.id.about: Shows a message related to the calculator menu.
  • R.id.exit: Finishes the activity.

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule. What We Offer:

What We Offer:

  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000 Successful Geeks

Please Login to comment...

LoginLike

Our Offerings:

Our comprehensive course provides in-depth learning opportunities for all participants, ensuring a thorough understanding of the subject matter.

  • Comprehensive Course: Our program covers a wide range of topics, equipping students with a holistic view of the subject.
  • Expert Guidance for Efficient Learning: Experienced instructors provide valuable insights and support to enhance the learning process.
  • Hands-on Experience with Real-world Projects: Students engage in practical exercises to apply theoretical knowledge in real-world scenarios.
  • Proven Track Record with 100,000 Successful Geeks: Our track record speaks volumes about the success of our past participants, with over 100,000 individuals achieving their goals.

Engagement Options:

Participants can actively engage with our platform through various means to enhance their learning experience.

  • Login: Access your account to explore additional features and engage with the community.
  • Like: Show appreciation for content and interact with other participants by liking their contributions.
The document How to Implement Options Menu in Android - Software Development is a part of Software Development category.
All you need of Software Development at this link: Software Development
Download as PDF

Top Courses for Software Development

Related Searches

pdf

,

Extra Questions

,

How to Implement Options Menu in Android - Software Development

,

study material

,

shortcuts and tricks

,

Semester Notes

,

Summary

,

Free

,

past year papers

,

Exam

,

Previous Year Questions with Solutions

,

Objective type Questions

,

practice quizzes

,

MCQs

,

How to Implement Options Menu in Android - Software Development

,

Sample Paper

,

Important questions

,

mock tests for examination

,

ppt

,

video lectures

,

Viva Questions

,

How to Implement Options Menu in Android - Software Development

;