ActionBarappMenu

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.

create-new-project

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.

check-create-new-activity

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.

drawables

3. Creating the menu items

Open res/menu/main.xml file :

menu-main-xml

And paste the following code :

main.xml:

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

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/home"
        android:icon="@drawable/home"
        android:orderInCategory="1"
        android:showAsAction="always"
        android:title="Home"/>

    <item
        android:id="@+id/java"
        android:icon="@drawable/java"
        android:orderInCategory="2"
        android:showAsAction="always"
        android:title="Java"/>

    <item
        android:id="@+id/android"
        android:icon="@drawable/android"
        android:orderInCategory="3"
        android:showAsAction="always"
        android:title="Android"/>

</menu>

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:

main-src-file

MainActivity.java:

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 {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	@SuppressLint("NewApi")
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

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

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		case R.id.home:
			homeActivity();
			return true;

		case R.id.java:
			javaActivity();
			return true;

		case R.id.android:
			androidActivity();
			return true;

		default:
			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:

main-screen

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

java-option

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

Nikos Maravitsas

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button