An action bar is a window feature that identifies the application and user location, and provides user actions and navigation modes.
Starting with Release 3.3.0, the Titanium SDK uses the appcompat library to provide support for the action bar, including devices running Android 2.3.x and prior. If you are using a release earlier than 3.3.0, refer to the Applicaton Note below for additional information.
You can add action items to the action bar by defining an Android menu and setting the menu items to display as action items. See Menu and MenuItem for details.
In JavaScript, wait for the window or tab group's open
event before accessing
the action bar from the window or tab group's activity.
Note that setting the Window.navBarHidden property to true disables the Action Bar since it is part of the navigation title bar.
For more examples on using the Action Bar, refer to the Android Action Bar guide.
Starting with Alloy 1.5.0, you can add ActionBar attributes to the ActionBar
element.
To use the action bar, add the <ActionBar>
tag as a child of either a
a <Window>
or <TabGroup>
, then set ActionBar attributes in either the XML or TSS file.
Starting with Alloy 1.4.0, you can also add ActionBar attributes to the Menu
element.
Do not define ActionBar attributes in both the ActionBar
and Menu
elements. Only define the
attributes in one element.
To add action items to the action bar, add the <Menu>
tag as a child of either
a <Window>
or <TabGroup>
, then add <MenuItem>
tags as children of the <Menu>
tag.
Set MenuItem attributes in either the XML or TSS file.
For an example of using the Action Bar with Alloy, see "Action Bar using Alloy XML Markup" below.
Starting with Release 4.0, due to the requirement that the target SDK must be set to Android 5.0 (API level 21) or higher, the action bar icon may not display. Google is discouraging the use of icons in toolbars:
In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars
than on their application icon. The use of application icon plus title as a standard layout is
discouraged on API 21 devices and newer.
Source: Android Developer: Toolbar API reference
If you are using Release 3.2.x or earlier, this feature is only available in Android 3.0 (API level 11) and above.
To access the action bar, you must first open a heavyweight window or tab group that uses one of the action bar themes (such as the Android Holo theme).
Adds action items and sets several properties on a window's action bar in the XML and TSS files.
app/views/index.xml
:
<Alloy>
<Window title="My Test App">
<ActionBar id="actionbar" title="My XML Menu" onHomeIconItemSelected="doMenuClick" />
<Menu>
<MenuItem id="item1" title="Settings" onClick="openSettings" />
<MenuItem id="item2" title="Search" onClick="doSearch" />
</Menu>
<Label id="label">Welcome!</Label>
</Window>
</Alloy>
app/styles/index.tss
:
"MenuItem": {
showAsAction: Ti.Android.SHOW_AS_ACTION_ALWAYS
},
"#item1": {
icon: Ti.Android.R.drawable.ic_menu_preferences
},
"#item2": {
icon: Ti.Android.R.drawable.ic_menu_search
},
"#actionbar": {
icon: "/actionicon.png",
displayHomeAsUp: true,
backgroundImage: "/actionbackground.png"
}
The following example sets several properties on a window's action bar.
var win = Ti.UI.createWindow({
title: "Old Title",
navBarHidden: false
});
var actionBar;
win.addEventListener("open", function() {
if (Ti.Platform.osname === "android") {
if (! win.activity) {
Ti.API.error("Can't access action bar on a lightweight window.");
} else {
actionBar = win.activity.actionBar;
if (actionBar) {
actionBar.backgroundImage = "/bg.png";
actionBar.title = "New Title";
actionBar.onHomeIconItemSelected = function() {
Ti.API.info("Home icon clicked!");
};
}
}
}
});
win.open();
Nearly identical code can be used for a tab group, but in Release 3.0, the tab group's activity must be accessed using the getActivity method.
The name of the API that this proxy corresponds to.
The name of the API that this proxy corresponds to.
The value of this property is the fully qualified name of the API. For example, Button
returns Ti.UI.Button
.
The background image for the action bar, specified as a local file path or URL.
The background image for the action bar, specified as a local file path or URL.
Indicates if the proxy will bubble an event to its parent.
Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.
Default: true
Sets a view to be used for a custom navigation mode.
Sets a view to be used for a custom navigation mode.
Inserts a custom view for navigation in the space between the application's home button and menu actions. The custom view is trimmed to fit the space available.
Displays an "up" affordance on the "home" area of the action bar.
Displays an "up" affordance on the "home" area of the action bar.
See also: setDisplayHomeAsUpEnabled in the Android Developer Reference.
Enable or disable the "home" button in the corner of the action bar.
Enable or disable the "home" button in the corner of the action bar.
See also: setHomeButtonEnabled in the Android Developer Reference.
Sets the application icon displayed in the "home" area of the action bar, specified as a local file path or URL.
Sets the application icon displayed in the "home" area of the action bar, specified as a local file path or URL.
Starting with Release 4.0, the action bar icon may not display. Google is discouraging the use of icons in toolbars.
Prior to Release 3.3.0, this method only works on devices with Android 4.0 (API 14) and above. See also: setIcon in the Android Developer Reference.
The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.
The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.
If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks will also be called on the proxy. Proxies that require the activity lifecycle will need this property set to the appropriate containing Window or TabGroup.
Sets the application logo displayed in the "home" area of the action bar, specified as a local file path or URL.
Sets the application logo displayed in the "home" area of the action bar, specified as a local file path or URL.
Prior to Release 3.3.0, this method only works on devices with Android 4.0 (API 14) and above. See also: setLogo in the Android Developer Reference.
Callback function called when the home icon is clicked.
Callback function called when the home icon is clicked.
Adds the specified callback as an event listener for the named event.
Name of the event.
Callback function to invoke when the event is fired.
Applies the properties to the proxy.
Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.
A dictionary of properties to apply.
Fires a synthesized event to any registered listeners.
Name of the event.
A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.
Hides the action bar if it is currently showing.
See also: hide in the Android API Reference.
Removes the specified callback as an event listener for the named event.
Multiple listeners can be registered for the same event, so the
callback
parameter is used to determine which listener to remove.
When adding a listener, you must save a reference to the callback function in order to remove the listener later:
var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);
To remove the listener, pass in a reference to the callback function:
window.removeEventListener('click', listener);
Name of the event.
Callback function to remove. Must be the same function passed to addEventListener
.
Sets the value of the backgroundImage property.
New value for the property.
Sets the value of the bubbleParent property.
New value for the property.
Sets the value of the customView property.
New value for the property.
Sets the value of the displayHomeAsUp property.
New value for the property.
Shows or hides the action bar home icon
See also: setDisplayShowHomeEnabled in the Android API Reference.
Boolean to show or hide action bar home icon
Shows or hides the action bar title/subtitle
See also: setDisplayShowTitleEnabled in the Android API Reference.
Boolean to show or hide action bar title/subtitle
Sets the value of the homeButtonEnabled property.
New value for the property.
Sets the value of the lifecycleContainer property.
New value for the property.
Sets the value of the onHomeIconItemSelected property.
New value for the property.
Sets the value of the subtitle property.
New value for the property.