Skip to content

Creating Your First Action Bar

jamesmontemagno edited this page Jul 14, 2012 · 1 revision

Creating an action bar is easy. First you should create a new Activity, new Android Layout, and a New Menu.xml.

Android Layout Setup

Wherever you wish to place your action bar (usually the top of a LinearLayout simply add:

<monodroid.actionbarsample.ActionBar android:id="@+id/actionbar" style="@style/ActionBar"/>

You can replace "monodroid.actionbarsample" with your namespace. This will add a default action bar to the top of your layout and will be ready to use.

Menu.xml Setup

For an easy example let's simply add 1 menu Item here with an id of menu_search:

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

<item android:id="@+id/menu_search" android:icon="@drawable/ic_menu_search" android:title="@string/menu_string_search"/>

</menu>

Activity Setup

After creating an activity you will want to changed the base class. You can create as many ActionBar Base classes as you need. We have provided a standard Activity and Also an ListActivity. Take a look at these for examples. The base classes provide advanced functionality such as inflating menus, custom onstart/onstop (flurry integration), and handling overflow in the action bar.

In this case we will derive from ActionBarActivity

public class NewActivity : ActionBarActivity

Now we are ready to setup and user our action bar.

Adding Action Bar Items

In your OnCreate method after you SetContentView you will want to add the 3 following lines:

ActionBar = FindViewById<ActionBar>(Resource.Id.actionbar); This finds your action bar in the layout

ActionBar.CurrentActivity = this; Sets the current context for the action bar to perform action

ActionBar.SetHomeLogo(Resource.Drawable.ic_launcher); Adds your logo to the action bar (this is optional), you can specify any drawable here. Additionally if you want this to be an "Up" action you can also call AddHomeAction which is a helper method in the ActionBarAcitivy Base class that will add a new intent and set the logo to be an Up button.

Finally we are able to add an action bar action. Since we want to re-use our Menu.xml so it is compatible with older devices we will want to add our MenuItemActionBarAction:

'var searchMenuItemAction = new MenuItemActionBarAction(this, this, Resource.Id.menu_search, Resource.Drawable.ic_action_search_dark, Resource.String.menu_string_search);' We pass in the same exact id from the menu.xml and also the string here will be what is popped up if the user long presses the button.

Next we can set the Action Type (by default it is "IfRoom") which describes how/when it should be put in the action bar. We currently support the following:

  • IfRoom - Dynamically calculates if the action can fit based on screen res
  • Always - Force it in the action bar no matter what
  • Never - Always put it in overflow (3.0+) or leave it in the old school menu bar.

In this case we will force it:

searchMenuItemAction.ActionType = ActionType.Always;

Lastly simply add the button to the action bar:

ActionBar.AddAction(searchMenuItemAction);

This will add 1 button to the action bar with the search icon.

Handle Action Bar Button Events

Since we used the MenuItemActionBarAction when this button is pressed it will fire the OnOptionsItemSelected with the specified menuId. To handle this we simple add our standard switch case and fine our Resource.Id.menu_search:

public override bool OnOptionsItemSelected(IMenuItem item){

switch (item.ItemId) { case Resource.Id.menu_search: OnSearchRequested(); return true; }

return base.OnOptionsItemSelected(item); }

And there you have it your first action bar!

Clone this wiki locally