The Action Bar: Licensed Under Creative Commons Attribution 2.5 License. All Rights Reserved
The Action Bar: Licensed Under Creative Commons Attribution 2.5 License. All Rights Reserved
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
● 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">