BigTobster
BigTobster

Reputation: 849

How to add an ActionBar from separate class in Android using AppCompat?

I am building a very simple app that has 1 Action Bar. I am using the AppCompat libraries to support from Android 2.1 or above in a vaguely uniform fashion.

I am following the Google Guides and have become a little stuck. I am a bit of a noob so please bear with me :)

My main class extends Activity so I created a new class which extends ActionBarActivity.

package com.example.simpledice;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;

public class DiceActionBar extends ActionBarActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dice_action_bar);
        ActionBar actionBar = getSupportActionBar();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.dice_action_bar, menu);
        return true;
    }

}

Then I added it like this:

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DiceActionBar diceActionBar = new DiceActionBar();

This does compile and deploys but no action bar. I have also tried something similar with fragments but no joy (couldn't even get them to compile!). I am only adding a single item to the action bar so it does seem a little overkill as it stands.

Can anyone tell me where I'm going wrong?

EDIT
Updated code as per Tanis.7x answer. Unfortunately, ActionBar is still not displaying. Updated code:

public class MainActivity extends ActionBarActivity
{
    private ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actionBar = getSupportActionBar();
        actionBar.show();
}

@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

}

Manifest:

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
        android:uiOptions="splitActionBarWhenNarrow">
        <activity
            android:name="com.example.simpledice.MainActivity"
            android:label="@string/app_name" 
            android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

res/menu/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/mute"
          android:title="@string/muteOption"
          android:showAsAction="ifRoom"
          />

</menu>

Upvotes: 1

Views: 3852

Answers (2)

BigTobster
BigTobster

Reputation: 849

Thanks for answers Tanix. Your video found the solution in the end. The problem was here:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/mute"
          android:title="@string/muteOption"
          android:showAsAction="ifRoom"
          />
</menu>

When using any pre-honeycomb features, any items listed in menu that are after honeycomb should be in XML namespace "yourapp", like this:

<menu 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:SimpleDice="http://schemas.android.com/apk/res-auto"
         >
    <item android:id="@+id/action_search"
          SimpleDice:icon="@drawable/mute"
          android:title="@string/muteOption"
          SimpleDice:showAsAction="ifRoom"
          />
</menu>

Action Bar comes up like a dream now.

Solved.

Upvotes: 2

Bryan Herbst
Bryan Herbst

Reputation: 67259

ActionBarActivity is an Activity, as you can see from the ActionBarActivity documentation. Simply creating a new Activity does nothing; you need to either launch the new DiceActionBar Activity, or change the way you are using ActionBarActivity.

In this case, you need to change MainActivity to extend ActionBarActivity instead of Activity. Thus MainActivity should look like this:

public class MainActivity extends ActionBarActivity {
    // The rest of your MainActivity code
}

Upvotes: 2

Related Questions