I have been having a lot of trouble trying to fix this one and couldn't find any solutions for this anywhere.
Currently I have the following ActionBar option menu
<?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_search"
android:icon="#drawable/ic_search"
android:title="#string/menu_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
<item
android:id="#+id/action_sort_by"
android:title="#string/sort_by"
app:showAsAction="never" />
<item
android:id="#+id/action_refresh"
android:title="#string/action_refresh"
app:showAsAction="never" />
</menu>
And everything works perfectly fine except if the options menu is opened while the search option is also opened:
The target layout as expected
The search option opened
This is the step that causes the problem, as you can see the Search option is now in the menu
Because of the previous step the search button is gone when closing the menu and the search option
If I don't open the options menu while the search option is also opened then everything works perfectly well, however this is something that cannot be expected for users to do and they might open the menu with the search options opened, leading to the search icon to disappear, resulting in a very problematic user experience.
How can I fix this, how can I prevent this problem from happening while keeping that same layout? Search icon visible, everything else in the 3 dots options menu.
To always show the search icon, simple change app:showAsAction="ifRoom|collapseActionView" to app:showAsAction="always|collapseActionView"
Related
I implemented the overflow button myself using the following base XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="Settings"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="always">
<menu>
<item
android:id="#+id/action_logout"
android:title="Log out"
android:showAsAction="never" />
<item
android:id="#+id/action_offer_advice"
android:title="Advice"
android:showAsAction="never" />
</menu>
</item>
<item
android:id="#+id/action_create"
android:title="Create"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"/>
</menu>
I did this because I want the Overflow button to be on every Android device regardless of a physical menu button. I opted for this over the hacky solution offered many times across Stackoverflow. That being said, I'm a bit fuzzy with ActionBars and I want to be positive that Android's default overflow won't show up for users that don't have a physical menu button. It would be very awkward to have my implementation of the overflow button with Android's default one right next to it.
Am I worrying over nothing? Is this a legitimate concern? If it is a legitimate concern, what could I do to handle that?
Personally I would say that this is not a concern. There are many apps native to the Android operating system (ex. the gallery app) that show the overflow menu despite the device having a physical menu button.
Another situation you may not have considered is the possibility that the user's menu button is not functional on the device, causing your action bar tidying to become a problem.
There is undoubtably a way to achieve this, but what you're trying to achieve may cause more problems than solve.
A common pattern I see in many apps that provide some search functionality is to have the search widget clear the other menu icons off of the ActionBar. This works for me right now just by using ifRoom for showAsAction in my menu items. I'd like to show more menu items in the actionbar though, because it seems that the ActionBar is very conservative with how it determines ifRoom and consequently at least one important menu item is pushed to the overflow (perhaps someone smarter than me knows why they do that).
So how can I set showAsAction as always while still clearing the menu items off of the ActionBar when the Search Widget is activated? When I try that now, the menu items don't move, and the search widget is condensed.
Here's my menu:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="collapseActionView|always"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/action_stores"
android:title="#string/action_stores"
android:icon="#drawable/ic_action_stores"
android:showAsAction="always" />
<item
android:id="#+id/action_shopping_lists"
android:title="#string/action_shopping_lists"
android:icon="#drawable/ic_action_lists"
android:showAsAction="always" />
<item
android:id="#+id/action_preferences"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
My searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
Couldn't you set an onClickListener on the search widget that then runs some few lines of code to make the other MenuItems change from 'always' to 'ifRoom'?
The few lines of code within the onClickListener would contain something like this:
myMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
whereas myMenuItem is your MenuItem that should go from 'always' to 'ifRoom'.
You can change it back later in an appropriate method.
I have two menu items: Settings and Exit. When I run my code, with both set to showAsAction:"always", none of them show up on the screen when I press the menu button on my phone. This also happens if I set both to "ifRoom". However, if I set one to "never", the other one will show up. How can I get both items to show up? I am running a ~3.5 inch android 4.1.2 phone. I am using the android:theme="#style/Theme.AppCompat" theme.
<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"
tools:context=".MainActivity" >
<group android:checkableBehavior="single">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="ifRoom|withText"
android:icon="#drawable/sun"/>
<item android:id="#+id/exit_the_app"
android:title="#string/options_exit_text"
app:showAsAction="ifRoom|withText"
android:orderInCategory="101"
android:icon="#drawable/night"/>
</group>
</menu>
This is the screen when both items are set to "always" or "ifRoom", no menu items show up:
This is the screen when settings is set to "never", the exit item shows up:
There seems to be enough room, so how do I make both items show up? Thank you.
When showAsAction is set to ifRoom or always the item is shown in the action bar instead of the overflow menu (only if there's room in the case of ifRoom). This is the expected behavior.
If you want them to both show up in the overflow menu, set showAsAction to never for both.
I suggest reading the documentation: http://developer.android.com/guide/topics/ui/actionbar.html
I have a problem: I want to put a search Icon on the Action Bar. In Android Studio it shows it on the Action Bar, but when I open the App it only shows it in the menu..
Here is my xml code:
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom"
android:title="#string/action_search"/>
I tried many things but it wont work. I hope you can help me.
It still doesn't work I really don't have a clue why.
You're not specifying your ActionView class, which is probably part of the problem. Here is the xml for a search action view that I use in one of my applications that works fine.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search_user"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/magnifing_glass"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/action_search"/>
</menu>
EDIT:
If you're not trying to use it as an ActionView, disregard the above and use showAsAction="always".
Your problem is either that there is not enough space on the action bar to display the icon, or your device has a dedicated menu button.
How do you manage Icons with ABS and Gingerbread? As you can see from the Screenshots, I have a menu item "Help", that sits in the action bar, when there's room for it. (when nothing is selected). The icon looks good so far. But when it moves into the options menu, I probably should use another icon for better visibility :)
How do you normally do that? Any ideas? This is my menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/new_button"
android:showAsAction="always"
android:title="new"
android:icon="#drawable/content_new_calendar"/>
<item
android:id="#+id/share_button"
android:visible="false"
android:showAsAction="ifRoom"
android:title="share"
android:icon="#drawable/social_share"/>
<item
android:id="#+id/help_button"
android:showAsAction="ifRoom"
android:title="help"
android:icon="#drawable/action_help"/>
</menu>
I know how to create different menus for various api levels, etc. But here, the same phone is showing the icon in the action bar and in the options menu. The simplest solution is to set android:showAsAction="never", but from user reviews I learned, that the App is difficult to understand (especially the Widget-Part), so I would love to have the help menu visible. Any ideas?
P.S. I know that the App is ugly and unfinished. It's work in progress and will be polished :)
Try using the force overflow option like so:
<item name="absForceOverflow">true</item>