Reader small image

You're reading from  Android 9 Development Cookbook - Third Edition

Product typeBook
Published inOct 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788991216
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
Rick Boyer
Rick Boyer
author image
Rick Boyer

Rick Boyer has been programming professionally for over 20 years. He has written apps on Windows, created websites, and coded for various mobile devices, including Windows CE, Windows Phone, and Android. Almost eight years ago, he took the plunge and started his own software consulting business, NightSky Development, focusing exclusively on Android development.
Read more about Rick Boyer

Right arrow

Introduction


The Android OS is an ever-changing environment. The earliest Android devices (prior to Android 3.0), were required to have a hardware menu button. Though a hardware button is no longer required, menus are no less important. In fact, the Menu API has expanded to now support three different types of menus:

  • Options menu and action bar: This is the standard menu, which is used for global options of your application. Use this for additional features such as search, settings, and so on.
  • ContextualMode (Contextual Action Mode): This is generally activated by a long press. (Think of this as similar to a right-click on the desktop.) This is used to take an action on the pressed item, such as replying to an email or deleting a file.
  • Pop-up menu: This provides a pop-up selection (like a spinner) for an additional action. The menu options are not meant to affect the item pressed; instead, use Contextual Mode as described previously. An example would be hitting the share button and getting...

Creating an options menu


Before we actually create and display a menu, let's look at a menu to see the end result. The following is a screenshot showing the menu section of the Chrome app:

The most obvious feature to note is that the menu will look different based on the screen size. By default, menu items will be added to the overflow menu—that's the menu you see when you press the three dots at the far right edge.

Menus are typically created in resource files using XML (like many other Android resources) stored in the res/menu directory, though they can also be created in code. To create a menu resource, use the <menu> element as shown:

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

The <item> element defines each individual menu item and is enclosed in the <menu> element. A basic menu item looks as follows:

<item  
    android:id="@+id/settings" 
    android:title="@string/settings" /> 

The most common <item> attributes...

Modifying menus and menu items during runtime


Though it's been stated many times it's considered the best programming practice to create UIs in XML rather than in Java, there are still times when using code is the better option. This is especially true if you wanted a menu item to be visible (or enabled) based on some external criteria. Menus can also be included in resource folders, but there are times when you need code to perform the logic of which resource to use. One example might be if you wanted to offer an upload menu item only if the user is logged in to your app.

In this recipe, we will create and modify the menu only through code.

Getting ready

Create a new project in Android Studio and call it RuntimeMenu using the default Phone & Tablet option. Select the Empty Activity option when prompted to add an activity. Since we will create and modify the menu completely in code, we will not need to create ares/menu directory.

How to do it...

To start, we will add string resources for...

Enabling Contextual Action Mode for a view


A context menu provides additional options related to a specific view—the same concept as a right-click on the desktop. Android currently supports two different approaches: the floating context menu and Contextual Mode. Contextual Action Mode was introduced in Android 3.0. The older floating context menu could lead to confusion since there was no indication of the currently selected item and it didn't support actions on multiple items—such as selecting multiple emails to delete in one action.

Creating a floating context menu

If you need to use the old-style context menu, for example to support pre-Android 3.0 devices, it's very similar to the Option Menu API, you just different method names. To create the menu, use onCreateContextMenu() instead of onCreateOptionsMenu(). To handle the menu item selection, use onContextItemSelected() instead of onOptionsItemSelected(). Finally, call registerForContextMenu() to let the system know you want context menu...

Using Contextual Batch Mode with RecyclerView


As discussed in the previous recipe, Contextual Mode supports two forms of use: single View mode (as demonstrated) and multiple selection (or batch) mode. Batch mode is where Contextual Mode outperforms the old-style context menu as multiple selections were not supported.

If you've ever used an email app such as Gmail or a file browser, you've probably seen Contextual Mode when selecting multiple items. Here is a screenshot from Solid Explorer, which shows an excellent implementation of Material Theme and Contextual Mode:

When we introduced RecyclerView in Chapter 2, Layouts, we discussed how many features from the old ListView were not already included in the new RecyclerView. Multiple item selection is one of the most missed features. In this recipe, we will demonstrate multiple item selection with the RecyclerView using Action Mode. 

Getting ready

We will use the RecyclerView example created in Chapter 2, Layouts as the base for this recipe. If...

Creating a pop-up menu


A pop-up menu is attached to a view similar to the drop-down menu on a spinner. The idea of a pop-up menu is to provide additional options to complete an action. A common example might be a Reply button in an email app. When pressed, several reply options are shown, such as: Reply, Reply All, and Forward.

Here is an example of the pop-up menu from the following recipe:

Android will show the menu options below the anchor view if there is room; otherwise, the menu will be shown above the view.

Note

A pop-up menu is not meant to affect the view itself. That is the purpose of a Context Menu. Instead refer to the Floating Menu/Context Mode described in the Enabling Contextual Action Mode for a view recipe.

In this recipe, we will create the pop-up menu shown previously, using ImageButton as the anchor view.

Getting ready

Create a new project in Android Studio and call it PopupMenu. Use the default Phone & Tablet option and select Empty Activity on the Add an Activity to Mobile...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android 9 Development Cookbook - Third Edition
Published in: Oct 2018Publisher: PacktISBN-13: 9781788991216
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Rick Boyer

Rick Boyer has been programming professionally for over 20 years. He has written apps on Windows, created websites, and coded for various mobile devices, including Windows CE, Windows Phone, and Android. Almost eight years ago, he took the plunge and started his own software consulting business, NightSky Development, focusing exclusively on Android development.
Read more about Rick Boyer