More MenuItems shown than needed after rotation in Android - android

I have an app (for 4.0 and above) which has couple of MenuItems in the ActionBar (one with 'always' and the rest with 'ifRoom' or 'never' property).
So the issue goes like this...
If I launch the app in landscape mode, the ActionBar looks like the following:
When I rotate to landscape from portrait, the ActionBar looks like this:
As you can see, more icons appear than they should be, i.e no space is left for title bar and the tabs menu (Navigation Menu). Actually, the 4 items are shown in the portrait mode (it's a split ActionBar) and it seems that the Android doesn't realise that there's no SplitActionBar there anymore.
Please suggest what I can do to fix this? :)
Edit
My menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_share"
android:icon="#drawable/ic_menu_share"
android:showAsAction="always"
android:title="#string/share"/>
<item
android:id="#+id/action_viewToggle"
android:icon="#drawable/ic_menu_view_as_grid"
android:showAsAction="ifRoom"
android:title="#string/switch_view"/>
<item
android:id="#+id/action_upload"
android:icon="#drawable/ic_menu_upload"
android:showAsAction="ifRoom"
android:title="#string/upload"/>
<item
android:id="#+id/action_newFolder"
android:icon="#drawable/ic_menu_new_folder"
android:showAsAction="ifRoom"
android:title="#string/create_folder"/>
<item
android:id="#+id/action_multiselect"
android:icon="#drawable/ic_menu_select_all"
android:showAsAction="ifRoom"
android:title="#string/selection_mode"/>
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_menu_refresh"
android:showAsAction="ifRoom"
android:title="#string/refresh"/>
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_menu_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>

Make sure you take care of each menu item visibility in your menu.xml file, check out the xml ShowAsAction attribute.
here's an example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/SOME_MENU_ITEM"
android:icon="#drawable/SOME_ICON"
android:showAsAction="ifRoom" | "never" | "withText" | "always" | "collapseActionView"
android:title="SOME_TITLE"/>
.
.

One of the important attribute xml for menu items is,
android:showAsAction ,that defines the visibility of the action item.
From your code it is evident that if there is room in action bar it'll display it.
So if you don't want it to appear specify android:showAsAction value as never so that whatever the orientation is it'll appear only in the overflow menu.

Related

Creating a popup options menu in a contextual action bar

I have an app with a NoActionBar theme. In my main activity I have an options menu that I created manually on the top of the screen (or by utilizing the built in device's options button).
In this main activity, I have a fragment with a listView where I apply the action mode long click functionality, to show the contextual action bar (CAB) for further user options.
Now, I try adding an options item to my CAB so it will contain some options like selecting all items in the listView, but since it's an item of the CAB, I can't really show the popup menu like in a regular activity. Further more, I want all the options menu callbacks (such as onOptionsItemSelected) to stay in the context of the CAB, in order to be able to continue to perform actions on the CAB.
Here's the code of my CAB:
<?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_delete"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/ic_action_delete"
android:title="Delete"/>
<item
android:id="#+id/action_overflow"
app:showAsAction="always"
android:orderInCategory="200"
android:icon="#drawable/ic_action_overflow"
android:title="Options"
android:visible="false"/>
</menu>
Apparently I missed the built in feature of the CAB - a built in overflow menu that collapses some of the actions' items once the screen is too small to show them all.
Another manipulation that needs to be done in order to always collapse certain actions under that overflow menu is to set for each one of them:
android:showAsAction="never"
app:showAsAction="never"
So, say we have 3 actions (delete, selece_all, add) in the CAB, and we want two of them (select_all, add) to be collapsed always under the built in overflow menu, we'll set this in the CAB's xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mm="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_delete"
android:orderInCategory="100"
mm:showAsAction="always"
android:icon="#drawable/ic_action_delete"
android:title="Delete"/>
<item
android:id="#+id/action_select"
android:orderInCategory="200"
android:showAsAction="never"
mm:showAsAction="never"
android:title="#string/select_all"/>
<item
android:id="#+id/action_add"
android:orderInCategory="300"
android:showAsAction="never"
mm:showAsAction="never"
android:title="#string/button_add"/>

Hide, unhide ActionBar icons with Search Widget

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.

Items in Action Bar won't show in App

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.

Overflow ActionBar actions not showing in Menu for Android 2.x

I'm using ActionBarSherlock to provide a unified UI for my Android 2.x and Android 4.x users. I have a menu with 6 items.
On a 2.x, 480px wide device with an hdpi screen, only 5 of the icons show. The device has a hardware Menu button, but when I tap it, nothing shows up. I expected it to popup and show the Action that wasn't able to fit.
I expect either the three-vertical-dots button to appear in the ActionBar to show a dropdown with the overflowed actions or I expect the physical Menu button to show the old style menu with the overflowed actions.
What am I missing or what am I doing wrong?
Here is my defined menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/add"
android:icon="#drawable/ic_menu_btn_add"
android:showAsAction="always"
android:title="Add"/>
<item
android:id="#+id/calculateNPV"
android:icon="#drawable/menu_icon_npv"
android:showAsAction="always"
android:title="NPV"/>
<item
android:id="#+id/calculateIRR"
android:icon="#drawable/menu_icon_irr"
android:showAsAction="always"
android:title="IRR/YR"/>
<item
android:id="#+id/send"
android:icon="#android:drawable/ic_menu_share"
android:showAsAction="always"
android:title="#string/share_pdf"/>
<item
android:id="#+id/graph"
android:icon="#drawable/ic_menu_gallery"
android:showAsAction="always"
android:title="#string/view_cashflow_diagram"/>
<item
android:id="#+id/deleteReorder"
android:icon="#drawable/ic_menu_clear_playlist"
android:showAsAction="always"
android:title="#string/delete_reorder_cashflows"/>
</menu>

Android 3.0: Changing order of icons in the action bar

I am working with the Android 3.0.
I have in the action bar 3 items and one of them is the menu of the application, it also has sub-menu in it.
I want to change the order of the icons so the menu won't be in the right place, I want it to be between the two other items. I tried to change the order in the xml with no success.
Does anyone has an idea how to set the order?
Try setting android:menuCategory and android:orderInCategory for your menu items to set the ordering.
Example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/launch_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/search_programs"
android:menuCategory="container"
android:orderInCategory="2"
android:showAsAction="always" />
<item android:id="#+id/preferences_screen"
android:icon="#android:drawable/ic_menu_preferences"
android:title="#string/preferences" />
<item android:id="#+id/refresh"
android:icon="#drawable/ic_menu_refresh"
android:menuCategory="container"
android:orderInCategory="1"
android:title="#string/refresh_items"
android:showAsAction="ifRoom" />
</menu>
It will have the same effect as declaring launch_search -action as last, but this way you'll have your actions in the right order for 2.x and older devices while being able to control action ordering for 3.x -devices.
The menu will always go to the right, you cannot control its placement.

Categories

Resources