Creating menus in android applications

Creating menu content

This specific menu is displayed in an assembly

Where are the menus?

For each menu, it is necessary to define the items that are in it. It is defined in XML format in a file located in a specific folder named “menu” (located in the “res” folder). We generate this file by right-clicking on the “menu” folder in the project section and selecting the New / “Menu resource file” option.

Item attributes of the menu element

Menu items are defined through the element <item/> which can have many attributes defined, the most commonly used attributes are the following:

  • id (android:id=”@+id/delete”)
  • title (android:title=”@string/delete”)
  • icon (android:icon=”@drawable/ic_delete”)
  • enabled (android:enabled=”false”)
  • checkable (android:checkable=”true”)
  • showAsAction (These attributes are related to the display of “menu options” elements within the action bar )
    • ifRoom (app:showAsAction=”ifRoom”)
    • withText (app:showAsAction=”withText”)
    • never (app:showAsAction=”never”)
    • always (app:showAsAction=”always”)
Example

Groups

It is possible to gather similar menu items into groups:

Submenu

Creating a submenu is simple, a new “menu” element should be embedded within the “item” element:

NOTE:
With elements where the attribute android:checkable=”true” is used, after clicking on such a menu member, you need to set the state of the check box, because the checkBox (or radio button) does not automatically change its state. After the click, we need to ask what the state of the item was before this click (whether it was checked) using isChecked().
If it was previously checked, now after the click it is no longer and we have to set the state setChecked(false) and vice versa if it was not checked item.isChecked()=false now after the click it is and we have to set item.setChecked(true);

Floating context menu

Creating menu content

First it is necessary to define the content of the menu in XML format.

Example (neki_menu.xml)

In this example, the first element of the menu is the so-called checkbox.

Integrating the menu into the activity

After defining the content, it is necessary to enable that menu to be available in the activity.

Example

Registering a button that opens a menu

As part of the activity, it is necessary to register a button (in the onCreate() method) that will be connected to that menu:

And then attach a clickListener to it that willon click to call the openContextMenu(view):

method

Defining what happens when a menu member is clicked

In the onContextItemSelected() method, we define what will happen when one of the menu members is clicked. To select a menu member, the “id” item defined in XML (neki_menu.xml) is used:

Options menu

options menu

For the options menu, it is not necessary to define a button that will respond to a click and open this menu, because android itself inserts this button into the action bar and assigns it an icon with three vertical dots.

It should be emphasized that here we have the opportunity to define which menu item will be immediately displayed in the actionBar next to the options icon (three vertical dots). This is provided with the app:showAsAction="ifRoom" attribute. The options menu is displayed exactly where the button is clicked. If a member is displayed immediately in the action bar, then it is not in the menu itself.

Creating menu content

In addition to these specifics, as with all other menus, it is necessary to define the content of the menu in XML format.


open options menu

Integrating the menu into the activity

In a similar way to the Context Float menu, the menu is integrated here:

Defining what happens when a menu member is clicked

In the onOptionsItemSelected method, we define what will happen after clicking on one of the menu members:

PopUp menu

This menu is specific because it is linked to the place where it was clicked. The menu is displayed where there is space either below the clicked place, or above the clicked place. The PopUp menu is hidden from the screen when the user clicks on a menu item or somewhere outside the menu.

popup

Defining what happens when a menu member is clicked

In order for a class (eg an activity) to be able to perform actions after a click, it is necessary for the class to implement the PopupMenu.OnMenuItemClickListener interface.
After implementing the interface, it is necessary to implement (Override) and its abstract method onMenuItemClick(), through which we are enabled to define for each member of the menu which action will be taken after clicking on the member.

Integrating the menu into the activity

In order to display the PopUp menu, we need to define the onClick method (eg shoPopup()) on the trigger (eg some button) which looks like this:

Contextual Action Mode menu

This specific menu is displayed as part of the actionBar, more precisely through the actionBar. In addition to the menu items shown with icons at the right end, there is also a menu title in the middle, and an arrow to close the menu on the left.


pre

Creating menu content

As with any menu, you first need to create the menu content in xml format (New “Menu resource file”) in the “menu” folder

Example (context_action_menu.xml)

Defining the ActionModeCallback method

First of all, within the activity, it is necessary to define a variable of type ActionMode (note, choose version v.7), because it is used within the callback method:

And then the ActionModeCallback instance where all the work related to the Contextual Action Mode menu is done. It has 4 methods, in the first onCreateActionMode() the previously defined menu content is connected to the Action Mode menu. In the onActionItemClicked() method, actions are defined after clicking on one of the members. And what happens when the menu is closed is defined in the onDestroyActionMode() method.

Registering a button that opens a menu

As part of the activity, it is necessary to define a clickListener on a certain button (in the onCreate() method) with which we will enable the previously defined mActionModeCallback() callback method to be called on a click:

So the whole listener code would look like this: