Introduction:

Action Bars are powerfull UI feature in introduced in Android Honeycomb, Action bar is a branded top bar of the application that provides easy access to relevant actions on the screen and a shortcut to application’s home screen.Most of the basic functionality of Action bar are backward compatible . action bar is a bar placed atop the screen that houses all the essential actions used in an application. It contains only those actions that are common to the entire application, and not activity specific ones.

The Action Bar is a widget for activities that replaces the traditional title bar at the top of the screen. By default, the Action Bar includes the application logo on the left side, followed by the activity title, and any available items from the Options Menu on the right side.

Benefits of using Action Bars:

1. Very easy and fast access to relevant action on current screen.
2. Easy access to application’s home screen by clicking the application logo at left. This paradigm is already well known from web sites.
3. opportunity to provide branding to the application.
4. Helps user in navigation by indicating his / her current location in app
5. Easy customization.

There are lot of things that can be done on action bars :

  • Action items
  • Tabs
  • Dropdown menu
  • Action views

Implementing Action Bars:
1) Create a Android xml by selecting menu in the /res/menu folder.

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

<item

android:id=“@+id/user_locate”

android:icon=“@android:drawable/ic_input_add”

android:title=“@string/locate”

android:showAsAction = “never”

android:onClick=“onOptionLocate” />……..</menu>

2) Create the actionbar option menu .

@Override

public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater menuInflater = getMenuInflater();

menuInflater.inflate(R.menu.actionmenu, menu);

return true;

}

The menu resource is just like a regular menu resource file, except there’s a new option that you can specify per item, with the name showAsAction. This property is available since API level 12 (Which is Android 3.0) if you run Android 2.3 or lower, this optionIf however you specify ifRoom, it will get displayed when there’s room on the screen. The OS will automatically hide the item when there isn’t enough room to show the item. is simply ignored by the OS.

The showAsAction option tells the OS how to display the action item. If you specify “never” it will hide the item under the menu button that gets displayed at the right hand side of the action bar.

Of course if you always want the item to be displayed on the screen, you can specify the “always” setting for the showAsAction attribute in the menu file.

The last option you can specify is whether the action item should be displayed with our without text. If you append the withText option to the showAsAction attribute, you will get the icon of the action item and the display text for the action item.

3) On to display action bars on the activity you need to activate it as a window feature

protected override void OnCreate(Bundle savedInstanceState)

{

super.OnCreate(savedInstanceState);

// Activate the action bar and display it in navigation         //mode.

RequestWindowFeature(WindowFeatures.ActionBar);

// Initialize the view for the activity

SetContentView(Resource.Layout.MainActivity);

// Initialize the action bar

InitializeActionBar();   }

Adding Tabs to Action Bar:

1) The tabs feature of the action bar combines the possibility to create custom tab like you can on the tab control with the power of fragments.

2)  tabs to work you tell the action bar to go into tabs navigation mode

3)   The important bits for a tab are the name to display and the handler to attach

private void InitializeActionBar()

{

// Set the action bar to Tab mode

ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

// Create a new tab (CreateTabSpec on the TabControl)

var homeTab = ActionBar.NewTab();

homeTab.SetTabListener(

new TabListener<CrumbsOverviewFragment>());

homeTab.SetText(“Crumbs”);

// Add the new tab to the action bar

ActionBar.AddTab(homeTab);

}

4) Implementing the tab handler is done by creating a new class that derives from Java.Lang.Object and implements the ActionBar.ITabListener interface.

public void OnTabSelected(ActionBar.Tab tab,FragmentTransaction ft)

{

ft.Add(Resource.Id.MainFragmentContainer,_fragment,typeof(T).FullName);

}

Adding DropDown to Action Bars:

The third way in which you can customize the action bar is by adding a dropdown to the action bar. For this mode of operation to work you need to switch the action bar to List navigation mode.

OnNavigationListener navListener = new OnNavigationListener() {

String[] strings = getResources().getStringArray(R.array.Contentlist);

@Override

public boolean onNavigationItemSelected(int itemPosition,long itemId)

{

TabListFragment newFragment = new TabListFragment();

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction ft = fragmentManager.beginTransaction();

ft.replace(R.id.mainFrg, newFragment, strings[itemPosition]);

ft.commit();

return true;

}};

spinnerAdapter = ArrayAdapter.createFromResource(ActionBarActivity.this,R.array.

Contentlist,android.R.layout.simple_dropdown_item_1line);

ActionBar actionBar = getActionBar();

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

actionBar.setListNavigationCallbacks(spinnerAdapter, mOnNavigationListener);

The spinner adapter can be derived from BaseAdapter to get a quick and easy foundation for it. It saves on hugely on implementation details

This component will receive the selection the user made in the list, upon which you can for example load a different fragment on the user interface or start another activity.

Add a action view to action bar:

Action views are custom views that display arbitrary controls on the action bar.

Configuring an action view is done by specifying the android:actionViewClass attribute in the menu resource file.

<item

android:id=“@+id/vacuum”

android:icon=“@android:drawable/ic_input_add”

android:title=“search”

android:showAsAction=“ifRoom”

android:actionViewClass=“android.widget.SearchView”

android:onClick=“onOptionVacuum” />

public override bool OnCreateOptionsMenu(IMenu menu)

{

MenuInflater.Inflate(Resource.Menu.ActionItems, menu);

var searchView =  (SearchView)menu.FindItem(Resource.Id.menu_search).ActionView;

searchView.SearchClick += OnSearchClicked;   }

Styling the Action Bar:

Customizing the action bar is a great way to extend your application branding to the standard control components.

To give Style to action style.xml is to be created.

This style must be appended to the manifest as a theme in the application tag. Basically these lines specify that there will be no title bar in the application, and instead will be overlaid by our own custom action bar.

<style name=“MyPopupMenu” parent=“android:style/Widget.Holo.Light.ListPopupWindow”>

<item name=“android:popupBackground”>@drawable/ad_menu_dropdown_panel_holo_light</item>

</style>

<style name=“Theme.AndroidDevelopers” parent=“android:style/Theme.Holo.Light”>

<item name=“android:selectableItemBackground”>@drawable/list_bg</item>

<item name=“android:popupMenuStyle”>@style/MyPopupMenu</item>

</style>

Share the joy

Written by SPECIndia

SPEC INDIA is a ISO 9001:2008 software solutions company based in Ahmedabad, India. Starting its journey in 1989 with just three professionals, SPEC INDIA has grown in revenue and human resource strength by building information bridges between technology and business processes.