Tutorials  /  Linux Basics

A guide to implementing the Android ActionBar: Examples and Tutorials

Ccentron Redaktion · June 2024 ·3 min read ·Linux Basics, Tutorial

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.
VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

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.

Code
 @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.

Go
 // 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!

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren