Android Action Bar
Action Bar
•
Action Bar is the element present at the top of the
activity screen.
• Android ActionBar can contain menu items which
become visible when the user clicks the “menu”
button.
• It is a salient feature of a mobile application that has
a consistent presence over all its activities.
• It provides a visual structure to the app and
contains some of the frequently used elements for
the users.
Action Bar Components
• App Icon: App branding logo or icon will be displayed here
• View Control: A dedicated space to display Application title. Also
provides option to switch between views by adding spinner or tabbed
navigation
• Action Buttons: Some important actions of the app can be added here
• Action Overflow: All unimportant action will be shown as a menu
Designing a Custom ActionBar
Manifest.xml
Setting the ActionBar
<menu xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="@+id/add"
android:icon="@android:drawable/ic_menu_add"
app:showAsAction="always"
android:title="@string/add"/>
<item
android:id="@+id/reset"
android:icon="@android:drawable/ic_menu_revert"
app:showAsAction="always"
android:title="@string/reset"/>
<item
android:id="@+id/about"
android:icon="@android:drawable/ic_dialog_info"
app:showAsAction="never"
android:title="@string/about">
</item>
<item
android:id="@+id/exit"
app:showAsAction="never“
android:title="@string/exit">
</item> </menu>
Menu Configuration
• android:id: attribute specifies the id of the menu item. This works like ids anywhere
else in the Android app. An android:id value starting with a @+id/ will create a
constant in the R.menu constant collection
• android:title: attribute value contains the title of the menu item
• android:icon: attribute references an icon in the drawable directories
• android:showAsAction: This attribute indicates how the given item should be
portrayed in the action bar.We can choose from any of the flags mentioned below:
• always to keep it in the ActionBar at all times
• ifRoom to keep it only if space is available (lowest orderInCategory)
• never this means that the menu item will not be placed in the ActionBar as an icon. It will only be
visible when the menu button is clicked, in the menu that’s popping up
• |withText : we can append this to either always or ifRoom, to indicate that the toolbar button to
be both the icon and the title, not just the icon
android:showAsAction=always
android:showAsAction=never
Android:showAsAction=always|withText
Inflating the Menu Into the Android ActionBar
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Responding to Android Action Bar Events
@Override
public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) {
case R.id.add:
count=(TextView)findViewById(R.id.textView);
count.setText("Add is clicked");
return(true);
case R.id.reset:
count=(TextView)findViewById(R.id.textView);
count.setText("Nothing is selected");
return(true);
case R.id.about:
Toast.makeText(this, R.string.about_toast, Toast.LENGTH_LONG).show();
return(true);
case R.id.exit:
finish();
return(true);
} return(super.onOptionsItemSelected(item)); }
Output