An actionbar is a primary toolbar within the activity that may display the activity title, application-level navigation affordances, and other interactive items.
ActionBar normally appears at the top of the window of an activity in case that activity is using the System’s Holo
theme or one of its descendants. This is normally the deafult situation.
In fact ActionBars normally get associated with the Activity.
ActionBars were added in Android API level 11.
In Android API level 21(Android L)
, a new widget was introduced called Toolbar
. This may be used to represent an ActionBar.
Toolbars are more flexible and customizable than ActionBars and may be defined in the XML Layout.
You just define the Toolbar in the layout then signal to the Activity which Toolbar you want to use as the Activity’s actionbar.
However, if you use the Toolbar, you have to:
- Use of the many
.NoActionBar
themes. - Set the
windowActionBar
attribute tofalse
.
There are two main example themes that people do use:
1. Holo Themes
With the Holo themes:
- The action bar will show the application icon on the left.
- The activity title will follow the app icon.
- In activities with option menus,you can make selected items directly access from the action bar as action items.
- You can modify the various characteristics of the ActionBar or remove it completely.
2. Material Theme(default in API level 21 or newer)
- The navigation button(“Home button”) occupies the space of the application icon.
- Apps can use their custom colors in the action bar or logo instead of text.
To get the ActionBar associated with an activity, all you have to do is call getActionBar()
.
API Definition
ActionBar is an abstract class that derives from Java.Lang.Object
:
public abstract class ActionBar : Java.Lang.Object
It is a member of the Android.App
namespace.
Retrieving an ActionBar
You can retrieve an action bar associated with a given activity in the following manner:
ActionBar actionBar = this.ActionBar;
Setting ActionBar Title and Subtitle
You can use the Title and Sub-title property to set the title:
actionBar.Title = "My ActionBar Title";
actionBar.Subtitle = "subtitle";
Setting the ActionBar icon
You use the setIcon()
method to set the icon for the action bar:
actionBar.SetIcon(Resource.Drawable.ic_maps_local_restaurant);
Example
Let’s look at a simple example to do the above: set the title, subtitle and icon to our action bar. We’ll use Holo.Light.DarkActionBar
theme.
This is Xamarin Android example:
using Android.App;
using Android.OS;
namespace MrActionBar
{
[Activity(Label = "MrActionBar", MainLauncher = true, Theme = "@android:style/android:Theme.Holo.Light.DarkActionBar", Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
//set actionbar title,subtitle and icons
ActionBar actionBar = this.ActionBar;
actionBar.Title = "My ActionBar Title";
actionBar.Subtitle = "subtitle";
actionBar.SetIcon(Resource.Drawable.ic_maps_local_restaurant);
}
}
}