Android Actionbar Example

This article will show you how to create and use action bar menus in android application. There are two activity 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 image to app / res / drawable folder.
  2. Create a action menu xml file under app / res / menu folder.
    android actionbar menu definication xml file
  3. Override onCreateOptionsMenu(Menu menu) method in activity class. You can initialize action bar menus in this method.
  4. Override onOptionsItemSelected(MenuItem item) method in activity class. This method is invoked when user click a menu in action bar.

2. Android Action Bar Examples.

This example contains two activity. The two activity are interactive use the action bar menu.

  1. ActionBarActivity
  2. ActionBarTargetActivity

android actionbar example release

2.1 ActionBarActivity Layout Xml File.

This file is saved in app / res / layout folder.

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.

This file is saved in app / res / menu folder.

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.

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 a AlertDialog to show 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.

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.

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.

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 a AlertDialog to show message.
    private void showAlertDialog(String message)
    {
        AlertDialog alertDialog = new AlertDialog.Builder(ActionBarTargetActivity.this).create();
        alertDialog.setMessage(message);
        alertDialog.show();
    }
}
(Visited 569 times, 2 visits today)
READ :   Android Marshmallow Runtime Permission Example

2 Comments


  1. Incomplete… 🙁
    Source code would be nice…

    Reply

Leave a Reply

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.