Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Menu icon is not displaying in action bar

I need to inflate custom menu in Fragment.

I have only one menu item.But the icon is not displaying.

Can someone tell what is wrong with my code

My menu.xml

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

<item
    android:id="@+id/search"
    android:icon="@android:drawable/ic_search_category_default"
    app:showAsAction="always"
    android:title="Search"/></menu>

And I set in onCreateView()

    setHasOptionsMenu(true);
    getActivity().invalidateOptionsMenu();

And inflating the menu

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Auto-generated method stub

    inflater = getActivity().getMenuInflater();

    inflater.inflate(R.menu.menu, menu);

}

Resulting screen attached below. I need to have the search icon instead of the menu overflow icon.

enter image description here

like image 838
Muhamed Riyas M Avatar asked Dec 19 '14 08:12

Muhamed Riyas M


People also ask

Where is the action overflow menu icon?

The right-hand side of the action bar shows the actions. The action buttons (3) show the most important actions of your app. Actions that do not fit in the action bar are moved to the action overflow, and an overflow icon appears on the right.

How do I add items to Action Bar?

To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

What are action bar icons?

The ActionBar, now known as the App Bar, is a consistent navigation element that is standard throughout modern Android applications. The ActionBar can consist of: An application icon. An "upward" navigation to logical parent.


2 Answers

I know I'm a little late for the party, but hope I can help someone else. Today I was facing this SAME problem.

I fixed using android:showAsAction="always" instead of app:showAsAction="always"

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

<item android:id="@+id/bluetooth_status_off"
    android:orderInCategory="0"
    android:icon="@drawable/bluetooth_red"
    android:title="@string/app_name"
    android:showAsAction="always" />
</menu>

on the showAsAction is underlined with red (warning), but works fine.

like image 143
rafaelbpa Avatar answered Sep 28 '22 13:09

rafaelbpa


I Think You should use

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

<item
android:id="@+id/search"
android:icon="@drawable/ic_search_category_default"
app:showAsAction="always"
android:title="Search"/></menu>

and

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.mymenu, menu);
    return true;
}
like image 42
pamitha Avatar answered Sep 28 '22 12:09

pamitha