Menu icon does not appear in action bar

I need to inflate a custom menu in a snippet.

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

Can someone say what's 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 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);

}

The resulting screen attached below. I need to have a search icon instead of a menu overflow icon.

enter image description here

+4
source share
8 answers

I know I'm a little late for the party, but I hope I can help someone else. Today I ran into this SUCH problem.

android: showAsAction = "always" : 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>

showAsAction (), .

+8

,

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.mymenu, menu);
    return true;
}
+2

app:showAsAction="ifRoom"

+1

...

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

.

0

ActionBar. , onMenuOpened :

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

Android 3.0+, . Google. Android.

0

, onCreate()

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
0
  • xml .
  • Now create a new menu file in the res-> directory
  • And call him with a different name
  • Now run
0
source

Source: https://habr.com/ru/post/1568289/


All Articles