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.
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.
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.
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With