Muhamed Riyas M
Muhamed Riyas M

Reputation: 5173

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

Upvotes: 9

Views: 12882

Answers (9)

AnupamChugh
AnupamChugh

Reputation: 1899

Make sure, you've added a menu item without an icon. Example:

<?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/next_btn"
        android:checked="true"
        android:icon="@android:drawable/ic_input_add"
        android:title="Item"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_settings"
        android:title="Settings" />

</menu>

Thanks to action_settings, it'll automatically add an overflow icon.

Upvotes: 1

Auston Ajith
Auston Ajith

Reputation: 11

  1. Copy your menu xml code.
  2. Delete your menu file
  3. Now create a new menu file in res->menu directory
  4. And name it with different name
  5. Now Run

Upvotes: 0

viren kheni
viren kheni

Reputation: 179

Sorry to late but put this code in your onCreate()

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Upvotes: 2

user2040560
user2040560

Reputation:

If you're running your code on Android 3.0+, the icons in the menu are not shown by design. This is a design decision by Google. You can read more about this on Android developers blog.

Upvotes: 0

rafaelbpa
rafaelbpa

Reputation: 512

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.

Upvotes: 10

Hookah_Smoka
Hookah_Smoka

Reputation: 374

This is default behaviour of the Overflow menu from the ActionBar. If you want to show the Icons in the overflow menu, override the onMenuOpened method in your activity with this code snippit:

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
        if(menu.getClass().getSimpleName().equals("MenuBuilder")){
            try{
                Method m = menu.getClass().getDeclaredMethod(
                        "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            }
            catch(NoSuchMethodException e){
                Log.e("MyActivity", "onMenuOpened", e);
            }
            catch(Exception e){
                throw new RuntimeException(e);
            }
        }
    }
    return super.onMenuOpened(featureId, menu);
}

Upvotes: 0

Sangeeta
Sangeeta

Reputation: 991

make a menu with the code below...

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
   <item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

   <item 
    android:id="@+id/action_search"
    android:title="Search"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="always"
    android:actionViewClass="android.widget.SearchView" />
 </menu>

and set it as your menu..

Upvotes: 0

pamitha
pamitha

Reputation: 55

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;
}

Upvotes: 2

hoomi
hoomi

Reputation: 1912

You have to add the following property app:showAsAction="ifRoom"

Upvotes: 1

Related Questions