Android apps have access to add activity-specific actions to the ActionBar
at the top of the device screen, just below the status bar. We will define two actions for the POIListActivity
class; New
, to create a new POI, and Refresh
, to refresh the cache of POIs from the device's local storage.
The Activity
class provides the following virtual methods that can be overridden to add actions:
Virtual Method |
Description |
---|---|
|
It allows for the creation of the actions either through API calls or through inflating an XML definition. |
|
Actions can be defined in a menu XML file that resides in the Resources/menu
folder, or it can be created programmatically using API calls. We will define the New
and Refresh
actions in an XML file named POIListViewMenu.xml
.
To create POIListViewMenu.xml
, perform the following steps:
Resources
folder in POIApp
, right-click on it and click on Add | New Folder.menu
.menu
folder, right-click on it and click on Add | New File.POIListViewMenu.xml
for the name and click on New.You now need to fill in the definitions for the two actions we identified. Unfortunately, Xamarin Studio does not contain a template for menu XML files, so you have to hunt the format down from Android documentation or online examples. The following code contains definitions for actionNew
and actionRefresh
:
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/actionNew" android:icon="@drawable/ic_new" android:title="New" android:showAsAction="ifRoom" /> <item android:id="@+id/actionRefresh" android:icon="@drawable/ic_refresh" android:title="Refresh" android:showAsAction="ifRoom" /> </menu>
Note that from the menu definition, we have referenced two new drawables; ic_new
and ic_refresh
. We need to add these images to the project the same way that we did for the ic_app
icon in Chapter 3, Creating the Points of Interest App. The images can be found in the drawable
folder present in the assets location.
The OnCreateOptionsMenu()
method is called to give an opportunity to the Activity
parameter to define actions for the ActionBar
. The Activity
class provides a MenuInflater
method, which reads the XML definition file and places the action defined on the ActionBar
. The following code shows the implementation from the code bundle:
public override bool OnCreateOptionsMenu(IMenu menu) { MenuInflater.Inflate(Resource.Menu.poiListViewMenu, menu); return base.OnCreateOptionsMenu(menu); }
The OnOptionsItemSelected()
method is called whenever an action in the ActionBar
is clicked and an instance of IMenuItem
is passed in. The IMenuItem.ItemId
instance corresponds to the ID specified in the item definition and can be used to determine which action was clicked on. The following code shows the implementation of OnOptionsItemSelected()
from the code bundle:
public override bool OnOptionsItemSelected (IMenuItem item) { switch (item.ItemId) { case Resource.Id.actionNew: // place holder for creating new poi return true; case Resource.Id.actionRefresh: POIData.Service.RefreshCache (); _adapter.NotifyDataSetChanged (); return true; default : return base.OnOptionsItemSelected(item); } }
Note that we have simply created a placeholder for actionNew
and placed two method calls for actionRefresh
.
The POIJsonService.RefreshCache()
method is called to refresh the cache with the *.json
files stored locally on the device.
The _adapter.NotifyDataSetChanged
method is called so that poiListView
will be refreshed based on the updated cache of POIs.
18.227.111.192