Build Menu Bar Apps

How to create ActionBar/Toolbar and Menu in Android Android22.07.2016

  1. Toolbar
  2. Build Menu Bar Apps Store

There are two kind of bar with control items in activity

Insert menu bar in your power apps screen. Select the menu bar. Using the drop-down option, select the custom property that you just created. In my case, it will be screen1.

  • ActionBar is action control bar or navigation bar you usually see at the top of an app, it usually has an app logo icon on the left, name of the current screen next to the logo, and other menu list on the right. To use actionbar, you don’t have to define an actionbar in the layout file which is handy but it deosn’t give you much flexibilities when you want to do some customizations in the actionbar, such as adding background image, animations, place the actionbar on the bottom instead on the top.
  • Toolbar does evertying you can do with ActionBar, and gives you the freedom to do customiztions that you can’t do easily with ActionBar.

You can use either old ActionBar (without any declarations in XML) or define android.support.v7.widget.Toolbar in activity layout and customize it look and events (more coding in this way).

Build an App for your Pub or Bar in minutes. Select your template. The Pub/Bar template is pre-loaded with all the features you’ll need for your new app. Brand your Pub’s app. Just provide your Website and Facebook Page, and we’ll do the legwork for you pulling in info and branding. The menu bar is available in Mac apps built with Mac Catalyst. Where you override this method determines the menu system that the builder updates. To add and remove items from the menu bar using the main menu system, override build Menu(with:) in your app delegate. To build a context menu using the context system, override this method in your. If you build a PowerApp for mobile devices you are faced with limited space to display information. If you add a menu you will be loose additional space. This post shows how you can build a menu structure that uses a minimum of space. The minimum of space is used if the menu is not visible and uses a mimimum of space if it is visible. Electron - Menus. The desktop apps come with two types of menus – the application menu (on the top bar) and a context menu (right-click menu). We will learn how to create both of these in this chapter. We will be using two modules – the Menu and the MenuItem modules. Note that the Menu and the MenuItem modules are only available in the main.

ActionBar example

Let's look at ActionBar and it's parameters. Insert following lines in onCreate method and you'll get ActionBar with icon

Toolbar example

ToolBar was introduced in Android Lollipop, API 21 release and is the successor of the ActionBar. It's a ViewGroup that can be placed anywhere in your XML layouts. ToolBar's appearance and behavior can be more easily customized than the ActionBar.

There are two ways to use Toolbar

  1. Use a Toolbar as an ActionBar when you want to use the existing ActionBar facilities (such as menu inflation and selection, ActionBarDrawerToggle, and so on) but want to have more control over its appearance.
  2. Use a standalone Toolbar when you want to use the pattern in your app for situations that an ActionBar would not support; for example, showing multiple toolbars on the screen, spanning only part of the width, and so on.

So, you should replace ActionBar with ToolBar if you want the flexibilities that comes with the Toolbar (adding background image, animations, place the actionbar on the bottom instead on the top and so on). Note that the ActionBar continues to work and if all you need is a static bar at the top that can host icons and a back button, then you can safely continue to use ActionBar.

To use Toolbar as an ActionBar, first ensure the AppCompat-v7 support library is added to your application build.gradle (Module:app) file

Second, let's disable the theme-provided ActionBar. The easiest way is to have your theme extend from Theme.AppCompat.NoActionBar (or the light variant) within the res/styles.xml file

Now lets talk about the color scheme for our project, as you can see from the image below, there are attributes which you can set to get a basic color scheme of your App done, right now we are just dealing we App bar so we would talk about colorPrimary and colorPrimaryDark. colorPrimary as the name says is the primary color of your App and the App bar while with the colorPrimaryDark you can set the color of the status bar to a certain color.

Next, you need to add a Toolbar to your Activity layout file. Below I place the Toolbar at the top of a LinearLayout like the standard ActionBar

Next, in your Activity or Fragment, set the Toolbar to act as the ActionBar by calling the setSupportActionBar(Toolbar) method

Adding items to ActionBar or Toolbar

Menus are typically created in resource files using XML but they are stored in the res/menu directory though they can also be created in code. To create a menu resource, use the <menu> element.

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

The most common <item> attributes are the following

  • id, this is the standard resource identifier;
  • title, this indicates the text to display;
  • icon, this is a draw-able resource;
  • showAsAction, this controls how the menu item is shown;
  • enabled, this is enabled by default.

The showAsAction attribute controls how the menu item is shown. The options include the following:

  • ifRoom, this menu item should be included in the Action Bar if there's enough space;
  • withText, this indicates that both the title and the icon should be shown;
  • never, this indicates that the menu item should never be included in the Action Bar;
  • always, this indicates that the menu item should be always included in the Action Bar.

First, we will add a string resource to the strings.xml file for the menu title. Start by opening the /res/values/strings.xml file and add the following <string> element to the <resources> element <string name='menu_email'>EMail</string>

Create a new file in the res/menu directory (right click on res - New - Android resource directory) and call it menu_main.xml.

Open the menu_main.xml file and add the following XML to define the menu

With the menu now created, we just have to override the onCreateOptionsMenu() method in our activity to inflate the menu

Add the following method to the application to see a Toast when the EMail menu is selected

If you want to show icon in overflow menu use following snippet

Creating sub menus

Sub menus are created and accessed in almost exactly the same manner as other menu elements and can be placed in any of the provided menus, although they cannot be placed within other sub menus. To define a sub menu, include a <menu> element within an <item> element.

Here is the XML form this recipe with two sub menu items added

Grouping menu items

Build menu bar apps

Another menu feature that Android supports is grouping menu items. Android provides several methods for groups, including the following

Build menu bar apps storeBuild menu bar apps list
  • setGroupVisible() show or hide all items;
  • setGroupEnabled() enable or disable all items;
  • setGroupCheckable() set the checkable behavior.

Android will keep all grouped items with showAsAction='ifRoom' together. This means all items in the group with showAsAction='ifRoom' will be in the Action Bar or all items will be in the overflow.

To create a group, add the <item> menu elements to a <group> element. Here is an example using the menu XML from this recipe with two additional items in a group

Custom title

A Toolbar is just a decorated ViewGroup and as a result, the title contained within can be completely customized by embedding a view within the Toolbar such as:

This means that you can style the TextView like any other. You can access the TextView inside your activity with:

Note that you must hide the default title using setDisplayShowTitleEnabled. This results in:

How to change color of menu in Toolbar

How to add back button to Toolbar

Android context menu example

First, define main activity layout

Second, describe context menu items

Third, define events and action in MainActivity class

onCreateContextMenu called when a context menu for the view is about to be shown. Unlike onCreateOptionsMenu, this will be called every time the context menu is about to be shown and should be populated for the view (or item inside the view for AdapterView subclasses, this can be found in the menuInfo).

onContextItemSelected hook is called whenever an item in a context menu is selected. The default implementation simply returns false to have the normal processing happen. You can use this method for any items for which you would like to do processing without those other facilities.

Toolbar example in Kotlin

Following is MainActivity.kt file.

Following is activity_main.xml file.

Following is toolbar_menu.xml file.

Result

Popup menu with icons example in Kotlin

Following is MainActivity.kt file.

Following is activity_main.xml file.

Following is popup_menu.xml file.

Toolbat with title and icons

Toolbar

File MainActivity.kt

File activity_main.xml

File toolbar.xml

Build Menu Bar Apps Store

Useful links