Android Actionbar Example

This article will show you how to create and use action bar menus in android applications. There are two activities in this example, each has it’s own action bar. When you click one action bar menu, the other activity will be displayed.

1. Create Android Action Bar Steps

  1. Copy all action bar menu icon images to the app/res/drawable folder.
  2. Create an action menu XML file under the app/res/menu folder.
  3. Override onCreateOptionsMenu(Menu menu) method in the activity class. You can initialize action bar menus in this method.
  4. Override onOptionsItemSelected(MenuItem item) method in the activity class. This method is invoked when the user clicks a menu in the action bar.

2. Android Action Bar Examples.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/PN2Z99ldsBk

  1. This example contains two activities ( ActionBarActivityActionBarTargetActivity ).
  2. The two activities are interactive use the action bar menu.

2.1 ActionBarActivity Layout XML File.

  1. This file is saved in the app/res/layout folder.
  2. activity_action_bar.xml
    <Button
        android:id="@+id/showActionBarButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show Action Bar"/>

2.2 ActionBarActivity ActionBar Menu XML File.

  1. This file is saved in the app/res/menu folder.
  2. action_bar_example_menu.xml
    <menu xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:id="@+id/menuNews"
            android:icon="@drawable/icon_news_32"
            android:title="News"
            app:showAsAction="always|withText" />
    
        <item
            android:id="@+id/menuFinance"
            android:icon="@drawable/icon_finance_32"
            android:title="Finance"
            app:showAsAction="always|withText" />
    
        <item
            android:id="@+id/menuHide"
            android:title="Hide Action Bar"
            app:showAsAction="ifRoom" />
    
        <item
            android:id="@+id/menuExit"
            android:title="Exit"
            app:showAsAction="ifRoom" />
    
    </menu>

2.3 ActionBarActivity Java File.

  1. ActionBarActivity.java
    package com.dev2qa.example;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    
    public class ActionBarActivity extends AppCompatActivity {
    
        ActionBar actionBar = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_action_bar);
    
            // Get ActionBar
            actionBar = getSupportActionBar();
            // Set below attributes to add logo in ActionBar.
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setDisplayUseLogoEnabled(true);
            actionBar.setLogo(R.drawable.icon_tech_32);
    
            actionBar.setTitle("Dev2Qa.com");
    
            // When click this button, action bar will be displayed.
            Button showActionBarButton = (Button)findViewById(R.id.showActionBarButton);
            showActionBarButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    actionBar.show();
                }
            });
    
        }
    
    
        /* This method will be called by system when app initialize. */
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the action bar menu from menu xml file.
            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(R.menu.action_bar_example_menu, menu);
            return super.onCreateOptionsMenu(menu);
        }
    
        /* This method will be invoked when user select a menu in action bar. */
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            // Get user select menu id and title.
            int itemId = item.getItemId();
            String menuTitle = (String)item.getTitle();
    
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("You clicked menu ");
            stringBuffer.append(menuTitle);
    
            String message = stringBuffer.toString();
    
            switch (itemId)
            {
                // When click News menu display ActionBarTargetActivity.
                case R.id.menuNews:
                    Intent intent = new Intent(this, ActionBarTargetActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    break;
                case R.id.menuFinance:
                    this.showAlertDialog(message);
                    break;
                case R.id.menuHide:
                    actionBar.hide();
                    break;
                case R.id.menuExit:
                    // If user click exit menu then finish this activity.
                    finish();
                    break;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        // Show an AlertDialog to show a message.
        private void showAlertDialog(String message)
        {
            AlertDialog alertDialog = new AlertDialog.Builder(ActionBarActivity.this).create();
            alertDialog.setMessage(message);
            alertDialog.show();
        }
    }

2.4 ActionBarTargetActivity Layout Xml File.

  1. activity_action_bar_target.xml
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This activity will be start when you click a menu item in previous activity's action bar."
        android:textSize="20dp"/>

2.5 ActionBarTargetActivity ActionBar Menu Xml File.

  1. action_bar_example_target_menu.xml
    <menu xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:id="@+id/menuBack"
            android:icon="@drawable/icon_left_arrow_32"
            android:title="Back"
            app:showAsAction="ifRoom|withText" />
    
        <item
            android:id="@+id/menuMusic"
            android:icon="@drawable/icon_music_32"
            android:title="Music"
            app:showAsAction="ifRoom|withText" />
    
        <item
            android:id="@+id/menuSports"
            android:icon="@drawable/icon_sports_32"
            android:title="Sports"
            app:showAsAction="ifRoom|withText" />
    
    
    </menu>

2.6 ActionBarTargetActivity Java File.

  1. ActionBarTargetActivity.java
    package com.dev2qa.example;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    
    public class ActionBarTargetActivity extends AppCompatActivity {
    
        ActionBar actionBar = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_action_bar_target);
    
            // Get ActionBar
            actionBar = getSupportActionBar();
            // Set below attributes to add logo in ActionBar.
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setDisplayUseLogoEnabled(true);
            actionBar.setLogo(R.drawable.icon_news_32);
    
            actionBar.setTitle("Dev2Qa.com - News");
        }
    
        /* This method will be called by system when app initialize. */
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the action bar menu from menu xml file.
            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(R.menu.action_bar_example_target_menu, menu);
            return super.onCreateOptionsMenu(menu);
        }
    
        /* This method will be invoked when user select a menu in action bar. */
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            // Get user select menu id and title.
            int itemId = item.getItemId();
            String menuTitle = (String)item.getTitle();
    
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("You clicked menu ");
            stringBuffer.append(menuTitle);
    
            String message = stringBuffer.toString();
    
            switch (itemId)
            {
                case R.id.menuSports:
                    this.showAlertDialog(message);
                    break;
                case R.id.menuBack:
                    Intent intent = new Intent(this, ActionBarActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    break;
                case R.id.menuMusic:
                    this.showAlertDialog(message);
                    break;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        // Show an AlertDialog to show a message.
        private void showAlertDialog(String message)
        {
            AlertDialog alertDialog = new AlertDialog.Builder(ActionBarTargetActivity.this).create();
            alertDialog.setMessage(message);
            alertDialog.show();
        }
    }

2 thoughts on “Android Actionbar Example”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.