Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. 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="" >





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:



import android.annotation.SuppressLint;
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(, menu);
		return true;

	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

			return true;

			return true;

			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:

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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below: