humanbeing
humanbeing

Reputation: 1697

Android Action Bar won't show items

I cannot get icons to show in the action bar.

I am using the most recent SDK with eclipse.

I am trying to do this tutorial http://developer.android.com/training/basics/actionbar/adding-buttons.html

My MainActivity.java is

package com.example.myfirstapp;

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

public class MainActivity extends ActionBarActivity   {

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

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

My main.xml under menu is

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

</menu>

But I dont see the menu items. It looks like this menu missing items Any help on how to get these menu items shows would be useful.

Thank you.

Upvotes: 0

Views: 2711

Answers (6)

despotbg
despotbg

Reputation: 778

Just put

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

and change

app:showAsAction="always" 

to

android:showAsAction="always"

Upvotes: 1

user2189399
user2189399

Reputation: 11

It also took me a while for this issue after I followed the tutorial step by step. Funny how, my problem is that I used light color search button image while chose the dark Holo theme. Therefore, my search button simply becomes invisible (it is there) in the dark background. You may try to adjust your theme if you encouter same issue.

Upvotes: 0

Breno Salgado
Breno Salgado

Reputation: 1962

I'm following the tutorial too, I think the mistake is here:

// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);

It should be

getMenuInflater().inflate(R.menu.main_activity_actions, menu);

Upvotes: 0

Prateek Verma
Prateek Verma

Reputation: 85

I am pretty sure you must have already done this, but did you download the icons and put them inside your res > drawable folders? If you are using a dark action bar, be sure to install the light colored icons and vice versa. I followed the same tutorial and it works for me. Your code looks correct.

Upvotes: 1

jac1013
jac1013

Reputation: 408

i think your problem is here:

return super.onCreateOptionsMenu(menu);

change it to this:

return Boolean.TRUE;

Source: Menus in android and an old project of mine.

Upvotes: 0

Benito Bertoli
Benito Bertoli

Reputation: 25803

Quoting from the docs:

The showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

So do the following modifications:

<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="always" />
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          yourapp:showAsAction="always" >
    </item>
</menu>

Upvotes: 4

Related Questions