Putting a menu item in the ActionBar - Android Honeycomb

With my usual apology of late that I don't have much time for writing these days, if you're working with Android Honeycomb (3.2) or newer, and want to put a menu item in the ActionBar (Action Bar) for an Activity, I hope this source code will help.

To put a menu item in the Action Bar, you'll need to edit these files:

  • The Java source code for your Activity (MainActivity.java in my case)
  • Create a menu file (mainmenu.xml)
  • Your Android manifest

For more advanced purposes you may need to edit other files, but for one basic menu item in the ActionBar, that's all you need.

The app activity: MainActivity.java

Here's the source code for my main Java Activity class. I've put some documentation in the source code.

package com.devdaily.fptrackerlite;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity 
extends Activity
{
  private Menu theMenu = null;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    theMenu = menu;
    // reference to /res/menu/mainmenu.xml
    new MenuInflater(getApplication()).inflate(R.menu.mainmenu, menu);
    return (super.onCreateOptionsMenu(menu));
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
  
    // code to handle the menu item tap
    if (item.getItemId() == R.id.calc) {
      Toast.makeText(this, "Got your tap", Toast.LENGTH_LONG).show();
      return true;
    }

  // this is how you handle the home button tap (not really needed for this example)
    if (item.getItemId() == android.R.id.home) {
      Toast.makeText(this, "Got menu item tap", Toast.LENGTH_LONG).show();
      return true;
    }

    return (super.onOptionsItemSelected(item));
  }
}

The menu: mainmenu.xml

Next, here's what /res/menu/mainmenu.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/calc"
    android:title="Calc"
    android:orderInCategory="1"
    android:showAsAction="always"/>
</menu>

That creates a menu item without an icon. If you prefer an icon, use this instead:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/close"
    android:title="Calc"
    android:orderInCategory="3"
    android:icon="@drawable/calc" 
    android:showAsAction="always"/>
</menu>

For this second example you'll need a file named calc.png in /res/drawable.

Android manifest

To get the menu working in the ActionBar, I think you also have to specify a minSdkVersion ... I'm not sure of the exact version, but since I'm targeting Honeycomb (which is Android v3.2, and therefore minSdkVersion 13), I specify minSdkVersion = 13 here.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.devdaily.fptrackerlite"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk android:minSdkVersion="13" android:targetSdkVersion="13" />

  <application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >

    <activity
      android:name=".MainActivity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

  </application>

</manifest>

That's all you need to get a menu item going in the Android Honeycomb ActionBar, but I'll share the XML view file for my Activity here as well.

The view: main.xml

Here's what the view file for my Activity looks like. Note there are no references to the menu.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="top"
  android:orientation="vertical" >

  <LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
      android:id="@+id/ilfMainLabel"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="@string/ilf_main_label"
      android:textAppearance="?android:attr/textAppearanceLarge" />

  </LinearLayout>

  <EditText
    android:id="@+id/ilfsTextArea"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cursorVisible="true"
    android:inputType="textMultiLine"
    android:singleLine="false"
    android:typeface="monospace"
    android:gravity="top|left" 
    >

    <requestFocus />
  </EditText>

</LinearLayout>

Also, here's /res/values/strings.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <string name="app_name">FPTrackerLite</string>
  <string name="ilf_main_label">Add ILFs (one per line)</string>

</resources>

Summary

In summary, I think that's all you need to get a menu item displaying and working in the Action Bar of Android 3.2 and newer.

Post new comment