3
votes

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/

3

3 Answers

5
votes

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

9
votes

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.

0
votes

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.