I have two menu items in menu/contacts_menu.xml as :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="#+id/pm_action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/action_search"/>
<item
android:id="#+id/show_online"
android:icon="#drawable/online_icon"
android:orderInCategory="2"
android:showAsAction="ifRoom"
android:title="Show Online"/>
</menu>
Where , in my Fragment :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.contacts_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.show_online) {
// DO SOMETHING
return true;
}
return super.onOptionsItemSelected(item);
}
I have done the same procedure in my others apps, and action items are showing. But in my current app, they are not showing. only if i press the menu button, action items are showing only with text. i want to show the icons on my action bar.
I guess you are using appcompat library? Try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/pm_action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always|collapseActionView"
appcompat:showAsAction="always|collapseActionView"
android:title="#string/action_search" />
<item
android:id="#+id/show_online"
android:icon="#drawable/online_icon"
android:orderInCategory="2"
android:showAsAction="always"
appcompat:showAsAction="always"
android:title="Show Online" />
</menu>
This should definitely work, but for simplicity you can also try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/pm_action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
appcompat:showAsAction="always|collapseActionView"
android:title="#string/action_search" />
<item
android:id="#+id/show_online"
android:icon="#drawable/online_icon"
android:orderInCategory="2"
appcompat:showAsAction="always"
android:title="Show Online" />
</menu>
Related
I'm able to create an option menu in Android through XML file, but I would like to add the second menu programmatically just before the menu is added from XML.
In example: the code below adds a menu item search.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
If I add a new menu item programatically, like
menu.add(0,Menu.FIRST, Menu.NONE,"Gift box").setIcon(R.drawable.jingle_bell).
setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
It is appearing after the Search menu item in Actionbar, but I need it before the search menu item.
What changes do I need to make?
Try this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.yourmenu, menu);
menu.add(0,Menu.FIRST, Menu.NONE,"Gift box").setIcon(R.drawable.ic_refresh).
setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Update:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
android:orderInCategory="200"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
Just set order for your item as,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.yourmenu, menu);
menu.add(Menu.NONE, Menu.FIRST, 1,"Gift box").setIcon(R.drawable.ic_refresh).
setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Third parameter is order for above menu item. Now in your menu.xml set order for your item as,
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
android:orderInCategory="2"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
Try below example
in XML :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_guide"
android:title="title"
android:icon="#drawable/ic_title"
app:showAsAction="always"
/>
then copy below code to your activity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return true;
}
here's my problem:
I have that nice toolbar with the icons in landscape mode:
after expanding the search view and showing the popup menu the "add" item appears (I thought that it shouldn't):
then returning with the back arrow key, as you see, the add button goes:
and you won't find it in the popup menu anymore:
I'm using support:appcompat-v7:25.1.0, and here's my menu code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/app_bar_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:title="Search"
app:showAsAction="always|collapseActionView"
android:enabled="true"
android:visible="true"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:title="Add"
android:enabled="true"
android:icon="#drawable/ic_action_add"
android:visible="true"
app:showAsAction="ifRoom"
android:id="#+id/add" />
<item android:title="Settings"
android:id="#+id/settings"
app:showAsAction="never"
android:icon="#drawable/ic_action_settings"
android:enabled="true"
android:visible="true" />
<item android:title="Feedback"
android:id="#+id/feedbvack"
app:showAsAction="never"
android:icon="#drawable/ic_action_feedback"
android:enabled="true"
android:visible="true" />
</menu>
I can set the add button showAsAction to "always" but I know that this is discouraged.
Does anyone here know why is there this behavior? and how can I prevent that?
Thanks in advance.
You can try to use this:
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
supportInvalidateOptionsMenu();
//or use invalidateOptionsMenu();
return true;
}
});
So when SearchView will be collapsed Toolbar will reallocate items and ifRoom items will be visible. I had this bug too and solved it this way.
Try this
<item
android:id="#+id/app_bar_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:title="Search"
app:showAsAction="always|collapseActionView"
android:enabled="true"
android:visible="true"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:title="Add"
android:enabled="true"
android:icon="#drawable/ic_action_add"
android:visible="true"
app:showAsAction="always"
android:id="#+id/add" />
<item android:title="Settings"
android:id="#+id/settings"
app:showAsAction="never"
android:icon="#drawable/ic_action_settings"
android:enabled="true"
android:visible="true" />
<item android:title="Feedback"
android:id="#+id/feedbvack"
app:showAsAction="never"
android:icon="#drawable/ic_action_feedback"
android:enabled="true"
android:visible="true" />
Set app:showAsAction to always to make sure it will be visible always.
Use of ifRoom has this feature if space available it will show or else it will hide it better use always or never
<item android:title="Add"
android:enabled="true"
android:icon="#drawable/ic_action_add"
android:visible="true"
app:showAsAction="always"
android:id="#+id/add" />
#Kovy has answered above with the fix for this bug and I confirm it fixes the bug. Thank you very much, mate! However, the above function has been deprecated in favour of individual menu items OnActionExpandListener. Example of it is as posted below:
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
supportInvalidateOptionsMenu();
return true;
}
});
Why does not show the action_call.title in the second device ??
I want to show the action_call.title on each device.
this device Android 5.1.1 (api22) 7"
this device Android 6.0 (api23) 5.96"
this is my menu.xml
<?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/action_call"
android:icon="#drawable/call"
android:title="#string/action_call"
android:visible="true"
app:showAsAction="ifRoom|withText" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_info"
android:icon="#drawable/info2"
android:title="#string/action_info"
app:showAsAction="never|withText" />
</menu>
this is my onCreateOptionsMenu on MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem action_call = menu.findItem(R.id.action_call);
action_call.setTitle("+99");
return true;
}
I want to be able to show the free-dot menu always without using ActionBar. Just 3 dots menu + items in it, that's it.
How can I do that? Here's my code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyMainActivity">
<item
android:id="#+id/empty"
android:showAsAction="always"
android:title="#string/dot_menu"
android:icon="#android:drawable/ic_menu_more">
<menu>
<item
android:id="#+id/item1"
android:showAsAction="ifRoom"
android:title="#string/item1" />
<item
android:id="#+id/item2"
android:showAsAction="ifRoom"
android:title="#string/item2" />
<!-- ........... -->
And:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//.....
}
It doesn't show a menu at all.
Try this code, I hope it's helpful.
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/item_admin"
android:showAsAction="ifRoom|withText"
android:title="#string/item_admin"
android:visible="false"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_masterpass_preferences"
android:showAsAction="ifRoom|withText"
android:title="#string/text_1"
android:visible="true"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_shipping_addresses"
android:showAsAction="ifRoom|withText"
android:title="#string/text_2"
android:visible="true"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_switch_language"
android:showAsAction="ifRoom|withText"
android:title="#string/text_3"
android:visible="false"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_support_contact"
android:showAsAction="ifRoom|withText"
android:title="#string/text_4"
android:visible="true"
tools:ignore="AppCompatResource" />
I want to show one icon and 2 items in CollapseActionView on portrait and all 3 icons on landscape mode in ActionBar. I have created two seperate files menu.xml and put it in port and land folders. My port/menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
yourapp:showAsAction="always"
android:inputType="textNoSuggestions"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="#+id/maps"
android:icon="#drawable/checkin"
android:title="#string/maps"
yourapp:showAsAction="collapseActionView" />
<item
android:id="#+id/sort_by"
android:icon="#drawable/ic_action_content_sort"
android:title="#string/sort_by"
yourapp:showAsAction="collapseActionView" />
</menu>
land/menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
yourapp:showAsAction="always"
android:inputType="textNoSuggestions"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="#+id/maps"
android:icon="#drawable/checkin"
android:title="#string/maps"
yourapp:showAsAction="always" />
<item
android:id="#+id/sort_by"
android:icon="#drawable/ic_action_content_sort"
android:title="#string/sort_by"
yourapp:showAsAction="always" />
</menu>
For some reason ActionBar always shows according to port/menu.xml despite device's orientation. What's wrong?
Try to invalidate the menu and recreate using the land/port menu.xml:
boolean isLandscape = false;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
if(isLandscape) {
inflater.inflate(R.land.menu, menu);
else
inflater.inflate(R.port.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE)
isLandscape = true;
else if (config.orientation == Configuration.ORIENTATION_PORTRAIT)
isLandscape = false;
invalidateOptionsMenu()
}
After additional researches I have figured out by myself. I just needed one menu.xml file and not to define showAsAction. My menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
yourapp:showAsAction="ifRoom"
android:inputType="textNoSuggestions"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="#+id/maps"
android:icon="#drawable/checkin"
android:title="#string/maps" />
<item
android:id="#+id/sort_by"
android:icon="#drawable/ic_action_content_sort"
android:title="#string/sort_by"/>
</menu>