Question

I am trying to add an ActionBar with some menu items inside. I am using API version 11 and the app theme is Theme.Holo.Light:

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

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

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

</manifest>

menu/main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/action_search"
        android:title="HIhi"/>
    <item android:id="@+id/action_compose"
        android:title="hello" />
</menu>

My build.gardle setup is this which showing I am using min API version 11:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

But when I launch the app, the ActionBar is still on the bottom:

enter image description here

I was expecting the ActionBar will be shown up on the top right corner of the app (with 3 dots as collapse menu). Did I miss anything in my setup?

Thanks

Was it helpful?

Solution

You can use parameter android:showAsAction="Always" for each menu item in menu.xml to show your items in action bars

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_add_size"
        android:title="@string/menu_add_item"
        android:orderInCategory="10"
        android:showAsAction="always"
        android:icon="@android:drawable/ic_menu_add" />
</menu>

And you have to remember: Android action bar menu view depends of device hardware control buttons type

EDIT Just read this article to understand differences, enjoy!

P.S. Sorry for my bad english :-(

OTHER TIPS

First of all, to get your options to show in the action bar, you can use android:showAsAction="always" or (the better solution) you can use android:showAsAction="ifRoom" and that way, it will only be in the action bar if it can fit.

So your menu/main.xml would be this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/action_search"
        android:showAsAction="ifRoom"
        android:title="HIhi"/>
    <item android:id="@+id/action_compose"
        android:showAsAction="ifRoom"
        android:title="hello" />
</menu>

However, this will not create the three vertical dots you describe. To do that, you can use a hack with this code:

public void getOverflowMenu()
{
    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if(menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {
        // ignore
    }
}

And then just call that method in your onCreate method like so:

getOverflowMenu();

I don't think this works on all devices. I seem to remember this hack not working on an Android 2.2 device, but that could just be bad memory.

Govtart is generally right, but I can see that you have dependency on appcompat-v7 library. So you should use another namespace for actionbar-specific attributes:

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

I used 'ifRoom' instead of 'always' as this is preferred option.

in your menu/main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:id="@+id/settings"
     android:title="@string/settings"
     android:orderInCategory="0"
     android:showAsAction="always"
      />

  <item android:id="@+id/action_compose"
     android:title="hello"
     android:orderInCategory="1"
     android:showAsAction="always"
      />

</menu>

and in your MainActivity class you have to call the onCreateOptionsMenu function

   @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    // action when action_search was selected
    case R.id.action_search:
      //****do something when the action_search item is clicked
      break;
    // action when action_compose was selected
    case R.id.action_compose:
      //****do something when the action_compose item is clicked
      break;

    default:
      break;
    }

    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top