Home » Android » core » app » ActionBar » Android ActionBar Example

About Nikos Maravitsas

Avatar photo
Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Android ActionBar Example

In this tutorial we are going to see how to  create a simple Android Menu using ActionBar. It is a very handy kit when you want to create simple Menus for your application. In this tutorial we are going to see how to create a simple menu with 3 options and with Icons.
For this tutorial, we will use the following tools in a Windows 64-bit platform:

  1. JDK 1.7
  2. Eclipse 4.2 Juno
  3. Android SKD 4.2

1. Create a new Android Project

Open Eclipse IDE and go to File -> New -> Project -> Android -> Android Application Project.

You have to specify the Application Name, the Project Name and the Package name in the appropriate text fields and then click Next.


In the next window make sure the “Create activity” option is selected in order to create a new activity for your project, and click Next. This is optional as you can create a new activity after creating the project, but you can do it all in one step.


Select “BlankActivity” and click Next.

You will be asked to specify some information about the new activity.  In the Layout Name text field you have to specify the name of the file that will contain the layout description of your app. In our case the file res/layout/main.xml will be created. Then, click Finish.

2. Add Drawable resources

In order to display Icons instead of text in the ActionBar, we need to add the images we want in the drawable resources. Go to package Explorer. You can drag and drop the images from your local files to the project files. As you can see I’ve copied 3 images.


3. Creating the menu items

Open res/menu/main.xml file :


And paste the following code :


<menu xmlns:android="http://schemas.android.com/apk/res/android" >





Now let’s see what all these attributes do:

  • First of all, android:orderInCategory dictates the position of the Item in the ActionBar.
  • android:showAsAction defines the way we want the Item to be displayed. For example, in our case we want the items to be always visible. We can change that to ifRoom. Now, Android will see if there is sufficient room in the screen, and if there is the option will be displayed. Else it will be places to the Overflow option. Additionally you can choose never as value. In this case the option will be hidden.
  • android:icon will define the icon of the option, which as you can see, will be a drawable resource.

4. Code the Main Activity

Use the Package Explorer to navigate to the Java file of the Activity you’ve created:



package com.javacodegeeks.android.androidactionbarexample;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {

	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.main, menu);
		return true;

	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		case R.id.home:
			return true;

		case R.id.java:
			return true;

		case R.id.android:
			return true;

			return super.onOptionsItemSelected(item);


	public void homeActivity() {
		Toast.makeText(this, "Home Option Selexted", Toast.LENGTH_SHORT).show();

	public void javaActivity() {
		Toast.makeText(this, "Java Option Selexted", Toast.LENGTH_SHORT).show();

	public void androidActivity() {
		Toast.makeText(this, "Android Option Selexted", Toast.LENGTH_SHORT).show();


5. Run the application

This is the main screen of our Application:


Now, when you press the on one of the options :


Of course you can customize the Actions as much as you want…

Download Eclipse Project

This was an Android Action Bar and Menus Example. Download the Eclipse Project of this tutorial: AndroidActionBarExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

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

Inline Feedbacks
View all comments