Ysera
Assistant Engineer
Assistant Engineer
  • UID634
  • Fans0
  • Follows0
  • Posts44
Reads:2004Replies:0

Study notes of Android: Android ActionBar

Created#
More Posted time:Nov 2, 2016 13:16 PM
After Android 3.0, Google made a series of changes to the UInavigation design. One of the new features is the introduced ActionBar which replaces the title bar that exists in versions before Android 3.0. The ActionBar offers richer navigation effects.
The ActionBar can display the application icons and titles of activities, as well as the action items.
Major usage of ActionBar:
1. To set the icon and title of the activity.
2. To navigate between different pages with tags or drop-down lists in concert with Fragment.
3. To add the Action View to provide quick access for users.
 
Enable ActionBar:
(1). ActionBar is enabled by default in versions later than Android 3.0. We may find that a newly created activity is inherited from ActionBarActivity by default. If you want to disable ActionBar, you can set the theme to Xxx.NoActionBar as below:
<activity android:theme="@android:style/Theme.Holo.NoActionBar">
 
(2). We can also call show() or hide() methods in the code to display or hide the ActionBar.
 
.When we are using the actionBar.hide() method, the system will by default allocate the space used by ActionBar to the page, and the current displayed page will be re-rendered. Similarly, calling the actionBar.show(); method will also trigger the re-rendering of the page. If one of our programs needs to display or hide ActionBar frequently, the effect won’t be satisfactory. Google offers an attribute to make ActionBar float over the page. You can also set the background of the ActionBar to transparent to enjoy a better experience and effect.
 
(3). Display the option menu on the ActionBar
This is to solve the problem that many mobile phones do not provide the Menu buttons.
MenuItem provides the setShowAsAction(int actionEnum) method to display the menu items on the ActionBar as action items.
The optional parameter values for actionEnum are as follows:
SHOW_AS_ACTION_ALWAYS: Always display the MenuItem on the ActionBar.
SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW: Collapse the Action View to a general menu item.
SHOW_AS_ACTION_IF_ROOM: Display the MenuItem when there is enough space on the ActionBar.
SHOW_AS_ACTION_NEVER: Do not display on the ActionBar.
SHOW_AS_ACTION_WITH_TEXT: Only display the text of the MenuItem on the ActionBar.
 
Of course, we can also modify the XML file of the menu item to display the menu item on the ActionBar. You only need to modify the android:showAsAction attribute of the <item.../>.
 
When there is not enough space on the ActionBar on top of the phone screen, Android implements different solutions for different devices. On the phones with the MENU buttons, the menu items failing to be displayed on the ActionBar will be displayed on the menu called out by the MENU buttons. On the phones without the MENU buttons, a collapsed icon will be displayed at the end of the ActionBar and the rest of the menu items will be displayed upon a click on the icon.
 
(4). Transform application icons on the ActionBar into clickable icons
To achieve this, you can call the following methods:
setDisplayHomeAsUpEnabled(boolean showHomeAsUp): to set whether the application icons are clickable and add a left arrow on the icon, usually used for returning to the activity of an upper level.
setDisplayOptions(int options): to control the display options of the ActionBar.
setDisplayShowHomeEnable(boolean showHome): to set whether to display the application icons.
setHomeButtonEnabled(boolean enabled): to set whether to transform the application icons to clickable buttons.
 
When you click this icon, the system will call the onOptionsItemSelected() method with android.R.id.home ID of the activity.
To respond to the click event, we also need to add corresponding processing actions in the onOptionsItemSelected method of the option menu.
 
(5). Display Action View on the ActionBar
We can add general components on the ActionBar through the following two methods:
1. When defining the Action Item, use the android:actionViewClass attribute to specify the implementation class of the Action View.
2. When defining the Action Item, use the android:actionLayout attribute to specify the implementation class of the Action View.
 
(6). Implement tab navigation using the ActionBar
We can display multiple tabs on the ActionBar and a click on the tab will take you to the specified tab page.
Steps for implementing the tab navigation are as follows:
1. Call the setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) method to set the tab navigation mode for the ActionBar. This method can also be used to set the ActionBar.NAVIGATION_MODE_LIST parameter which is used to implement the drop-down list.
2. Call the addTab() method of the ActionBar to add multiple tabs for the ActionBar and add event listening for every tab.
 
To better demonstrate the tab navigation effect, we usually use ActionBar and Fragment in combination.
Fragment enables the creation of multiple pages in an activity. The specific usage will be detailed later.
 
(7). Apart from tab navigation, we can also use ActionBar to implement drop-down navigation. The steps are as follows:
1. Set the drop-down list mode for the ActionBar using the setNavigationMode(ActionBar.NAVIGATION_MODE_LIST) method.
2. Call the setListNavigationCallbacks(SpinnerAdapter adapter,ActionBar.OnNavigationListener callback) method to add multiple list items and event listeners for the list items for the drop-down list.
Guest