0% found this document useful (0 votes)
59 views6 pages

The Action Bar: Licensed Under Creative Commons Attribution 2.5 License. All Rights Reserved

The document discusses the Android action bar, which replaces the older "Menu" button. It describes how to implement an action bar by extending ActionBarActivity, declaring menu items in an XML menu resource, and handling click events on those items. The action bar identifies the current app and activity and provides prominent access to common actions through icons and text.

Uploaded by

alolmani_oop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views6 pages

The Action Bar: Licensed Under Creative Commons Attribution 2.5 License. All Rights Reserved

The document discusses the Android action bar, which replaces the older "Menu" button. It describes how to implement an action bar by extending ActionBarActivity, declaring menu items in an XML menu resource, and handling click events on those items. The action bar identifies the current app and activity and provides prominent access to common actions through icons and text.

Uploaded by

alolmani_oop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CS 193A

The Action Bar

This document is copyright (C) Marty Stepp and Stanford Computer Science.
Licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Action Bar (link)
● action bar: top-level menu of app functions
– replaces older "Menu" button
(which is now discouraged in Android 3+)
– identifies current activity/app to user
– make common actions prominent and available
– make less common actions available through a drop-down menu
Support for action bar
● make activity class extend ActionBarActivity
– write methods: onCreateOptionsMenu,
onOptionsItemSelected

● declare the menu items in res/menu/menu_activity.xml


– decide which items have icons, which have text,
which should appear in main menu, which in "overflow" submenu
– need to place icon image files in res/drawable folder

● handle events
– write code in onOptionsItemSelected to check what option was clicked
and respond accordingly
ActionBarActivity
public class MainActivity extends ActionBarActivity {
...

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater(); // reads XML
inflater.inflate(R.menu.menu_main, menu); // to create
return super.onCreateOptionsMenu(menu); // the menu
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO: handle clicks on the menu items
return super.onOptionsItemSelected(item);
}
}
Menu bar XML data
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">

<item android:id="@+id/action_send" android:title="Send"


android:icon="@drawable/iconsend"
app:showAsAction="always" />
<item android:id="@+id/action_archive" android:title="Archive"
android:icon="@drawable/iconarchive"
app:showAsAction="always" />
<item android:id="@+id/action_open" android:title="Open"
android:icon="@drawable/iconopen" />
</menu>

● showAsAction can be always, never, ifRoom, withText, ...


onOptionsItemSelected
public class MainActivity extends ActionBarActivity {
...
/* Handles presses on the action bar items. */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_send) {
// do something;
} else if (item.getItemId() == R.id.action_archive) {
// do something;
} else if (item.getItemId() == R.id.action_open) {
// do something;
}
return super.onOptionsItemSelected(item);
}
}

You might also like