Home » Android » android – Menu icon is not displaying in action bar

android – Menu icon is not displaying in action bar

Posted by: admin June 15, 2020 Leave a comment

Questions:

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

How to&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.

Answer:

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

Answer:

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

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

Answer:

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

Answer:

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..

Answer:

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

Answer:

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.

Answer:

  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

Answer:

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.