A guide to implementing the Android ActionBar: Examples and Tutorials

Discover everything about the Android ActionBar in our latest blog post – from setup to implementation! Learn how to add menu items, handle events, and design a user-friendly interface.

What is the Android ActionBar?

The Android ActionBar is a menu bar that runs across the screen of an activity in Android. It can contain menu items that become visible when the user clicks the “Menu” button. In general, an ActionBar consists of the following four components:

  1. App Icon: This displays the brand logo or symbol of the app.
  2. Title Display: A dedicated area for displaying the application title. Also provides the ability to switch between views by adding a spinner or tabbed navigation.
  3. Action Buttons: This is where important app actions can be added.
  4. Overflow Menu: Any less important actions are displayed as a menu.

Setting up the Android ActionBar

All activities using the “Theme.Holo” theme or a theme derived from it automatically include an ActionBar.

Creating the Android ActionBar Menu

The easiest way to integrate toolbar icons and actions into the ActionBar is by creating a menu XML resource file in the “res/menu” directory. You can add menu items in the XML file as follows:

Responding to Android ActionBar Events

To determine when the user clicks on any of the menu items, we need to override the onOptionsItemSelected() method of the MainActivity.

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        // Cases for each menu item
    }
    return(super.onOptionsItemSelected(item));
}

Example Project Structure and Implementation

The main functions of the menu items are implemented in the MainActivity. Here we change the content of a TextView, display a toast message, or exit the application depending on which menu item was selected.

 // MainActivity.java
package com.journaldev.actionbar;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            // Cases for each menu item with corresponding functions
        }
        return(super.onOptionsItemSelected(item));
    }
}

Conclusion

The Android ActionBar is a powerful way to integrate important actions and features into your app. With the right tools and resources, you can design a user-friendly interface that meets the needs of your users. In this tutorial, we covered the basics of ActionBar setup and usage. For further information, you can also explore creating a custom ActionBar.
With this tutorial, you should have a good introduction to working with the Android ActionBar. We hope it was helpful for you!

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: