3

I have looked at the following stack overflow articles to figure out why my action bar items are getting forced into overflow.

I have also tried replacing "ifRoom" with "always," and I'm not getting any console output about issues find the drawable resources (they even show correctly in the menu preview pane)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:id="@+id/menu_search"
    android:icon="@drawable/ic_action_search"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:title="Search"/>
<item android:id="@+id/menu_new"
    android:icon="@drawable/ic_action_new"
    app:showAsAction="ifRoom"
    android:title="New"
    android:orderInCategory="0"/>
</menu>

from MainActivity.java

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

    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    return super.onOptionsItemSelected(item);
}

Here's what it looks like:

action bar items not showing

And here's my res directory:

/res/

Community
  • 1
  • 1
Todd Anderson
  • 1,138
  • 9
  • 21

3 Answers3

9

Use android:showAsAction instead of app:showAsAction.

And when the error Should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto showed up I got a suggestion to suppress and it worked!!!

Use this line>> tools:ignore="AppCompatResource" and add xmlns:tools="http://schemas.android.com/tools" to top.

Exhausted
  • 1,867
  • 2
  • 23
  • 33
Sndn
  • 980
  • 12
  • 20
6

For all your app: attributes, also have an equivalent android: attribute (e.g., app:showAsAction and android:showAsAction).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    This fixed it but Android Studio is highlighting the android:showsAsACtion attributes with an error now. I can ignore it but just wondering why this is needed – Todd Anderson Jun 26 '14 at 20:48
  • 2
    "Should use app:showAsAction with the appcompat library with xmlns:app="http://schemas.android.com/apk/res-auto" – Todd Anderson Jun 26 '14 at 20:53
  • @ToddAnderson: Not sure why that complaint is there. My assumption is that AppCompat delegates to the native action bar on suitable API levels, the way ActionBarSherlock does, and the native action bar will be looking for the `android:` attributes. – CommonsWare Jun 26 '14 at 20:55
0

If your activity extends AppCompatActivity and targetSdk is > 22 and you use com.android.support:appcompat-v7 make sure you use in your xml:

app:showAsAction="ifRoom"

instead of

android:showAsAction="ifRoom"

If you use android: the showAsAction is actually ignored and the items will always go to the overflow menu.