Reputation: 5173
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.
Upvotes: 9
Views: 12882
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
Reputation: 11
Upvotes: 0
Reputation: 179
Sorry to late but put this code in your onCreate()
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Upvotes: 2
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
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
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
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
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