I'm writing an Android app based on a MainActivity with several attached Fragments. The ActionBar displays navigation tabs, along with icons. Each fragment has a set of icons associated with it; MainActivity has a 'Close' icon which is also displayed. I display them by calling setHasOptionsMenu(true), and inflating the menu xml in onCreateOptionsMenu.
Menu XML example:
<item
android:id="#+id/apply"
android:orderInCategory="50"
android:title="#string/apply"
app:showAsAction="always"
android:showAsAction="always"/>
<item
android:id="#+id/reset"
android:orderInCategory="75"
android:title="#string/reset"
app:showAsAction="always"
android:showAsAction="always"/>
</menu>
Right now, sometimes the icons display correctly, and other times the action bar appears to be missing icons. The missing icons don't appear in the overflow (I have a physical menu button) either. I've tried hiding all menus except the one belonging to the active fragment in PageView.setOnPageChangeListener, but that hasn't helped.
Thanks!
Remove app:showAsAction=... attributes and delete the app namespace from the menu xml file.
Related
Before you Tag as a duplicate question: I've read:
Action buttons doesn't show up on Action Bar?
Action Bar not showing action view icons
and
Android: Icons don't appear in Action Bar
But the issue is not resolved. My action bar will only hold the menu items in the overflow. I cannot get the icons to appear on the action bar itself.
The code for my menu is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/homeIcon"
android:title="#string/Home"
android:icon="#drawable/ic_home_black_24dp"
android:orderInCategory="1"
app:showAsAction="always" /></menu>
I have declared the support header files but it hasn't made a difference.
I tried replacing the word 'app' in "app:showAsAction" with my app name as it appears in #strings but it will not accept it because the app name has white space.
it is called my App instead of myApp.
Is there that the reason why i can't get any icons in my actionBar, if yes is there a way to resolve it.. without recreating my app? i tried changing the name in #strings and that didn't resolve the issue, so it could just as easily be something else.
Thanks in advance community!
Probably you are not using AppCompat (or not using it in this particular situation) then android:showAsAction. Yes, Android Studio will complain and recommend to use app:showAsAction but it works.
I have a toolbar, to which I have attached a sliding tab layout, using these two classes: SlidingTabLayout, SlidingTabStrip.
When I long press an item, the contextual action bar appears and overlays the toolbar, using <item name="windowActionModeOverlay">true</item> in my styles.xml. The problem is that the Tabs are still clickable, and swipable. I have tried setClickable(false), which didn't work.
How do I make the tabs not clickable, so that I can then change the "state look" of the tabs to a disabled state, with the code in a xml file within the drawable folder, as seen below.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#color/primary_dark" />
<item android:drawable="#android:color/transparent" />
Any help is much appreciated thanks.
Put a flag in your SlidingTabLayout isActionModeEnabled.
Set it every time Action mode is created and unset it on every destruction.
Based on which configure the onClick() of TabClickListener class so that if isActionModeEnabled == true then do nothing and change the background of all tabViews or whatever you want to do with it.
I have a menu defined in an XML file such a "home_menu.xml" like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_user_preferences"
android:showAsAction="never"
android:title="#string/action_user_preferences"/>
<item
android:id="#+id/action_logout"
android:showAsAction="never"
android:title="#string/action_logout"/>
</menu>
In my "Home" activity, I can access this menu by pushing the menu button in a device that has a physical button, but I don't want to use action bar so I want to add a Navigation Drawer for this purpose as Google Maps does.
It would be great if I could use the same onOptionsItemSelected() method defined in my Home activity to handle menu actions.
You won't be able to do that.
You'll have to do it manually.
Or semi-manually. You could in onCreateOptionsMenu get all the menu items with title, icon and id and generate the view (listview for instance) for the drawer.
And use a shared method to handle the click that takes the id in parameter.
But if you want to hide/show some elements in the menu, you'll have to update your listview manually since onCreateOptionsMenu won't be called. And if you have multiple fragment with menus you'll also have to do something.
So I would recommend to use the drawer even when there is a hardware menu key.
I'm having a problem when I try to set one item in my actionbar as always visible and 4 more icons as dropdown items with the following layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search_movies"
android:icon="#drawable/action_search"
android:showAsAction="always"
android:title="Search"/>
<item
android:id="#+id/movies"
android:icon="#drawable/action_video"
android:showAsAction="collapseActionView"
android:title="Movies"/>
<item
android:id="#+id/theaters"
android:icon="#drawable/action_location_map"
android:showAsAction="collapseActionView"
android:title="Theaters"/>
<item
android:id="#+id/preferences"
android:icon="#drawable/action_settings"
android:showAsAction="collapseActionView"
android:title="Preferences"/>
<item
android:id="#+id/contact"
android:icon="#drawable/action_about"
android:showAsAction="collapseActionView"
android:title="Contact"/>
</menu>
The result is just the first item showing and the rest are not visible, not even as a dropdown. This is using ActionBarSherlock and a 2.3 Android device.
The question is, how can I get the icons to follow this layout:
EDIT:
The problem I had was because when you are using the actionbar with a device that has a "menu" hardware button the 3-dot dropdown does not shows off, the 4 other items are only displayed if you press the menu hardware button. Does anyone knows if this behaviour can be modified?
Hmmm, maybe I misunderstood, but if you wish to places those remaining four items into the overflow action menu (the 3-dot icon) then using android:showAsAction="never" instead of "collapseActionView" should do it.
...Tried a couple ways, but this did the trick:
Force overflow menu in ABS
I've met the same problem and my solution is quite simple. (I didn't use HoloEverywhere.)
The idea comes from the ABS sample project, whose drop-down menu can be displayed on pre-4.0 devices as well by using a submenu. So, my idea is using a submenu to disguise the 3-dot icon. Here's the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu sub = menu.addSubMenu("More");
sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
sub.getItem().setIcon(R.drawable.ic_menu);
getSupportMenuInflater().inflate(R.menu.activity_main, sub);
return true;
}
Since the "More" menu doesn't have a MenuItem.SHOW_AS_ACTION_WITH_TEXT attribute, so the word "More"(or whatever you named) will actually not be displayed on the action bar. The only displayed icon R.drawable.ic_menu can be copied from ABS source code res/drawable-xxdpi folders named "abs__ic_menu_moreoverflow_normal_holo_dark.png", which is the so-called 3-dot icon. And the R.menu.activity_main is your menu xml.
It works!
I have 3 menu icons on the menu bar, but everytime it only shows 2 icons, the last one is in nowhere.
my questions are:
1. there is enough space for 3 icons, why only 2 are shown?
2. if the system thinks the space is not enough for the 3rd icon, why doesn't it combine the 2nd and 3rd icon into an overflow menu?
Below is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/refresh"
android:icon="#drawable/ic_menu_refresh"
android:showAsAction="ifRoom" />
<item android:id="#+id/add_homework"
android:icon="#android:drawable/ic_menu_edit"
android:showAsAction="ifRoom" />
<item android:id="#+id/set_groupid"
android:icon="#android:drawable/ic_menu_preferences"
android:showAsAction="ifRoom" />
</menu>
and this snippet is in my MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
I have 3 menu icons on the menu bar
For the purposes of this answer, I am assuming that by "menu bar" you are referring to the action bar.
but everytime it only shows 2 icons, the last one is in nowhere
The last one is available by pressing the MENU button, for devices (or emulators) that have an off-screen MENU button.
there is enough space for 3 icons, why only 2 are shown?
Presumably because Android disagrees with your assessment of whether or not there is enough space for 3 icons.
if the system thinks the space is not enough for the 3rd icon, why doesn't it combine the 2nd and 3rd icon into an overflow menu?
I have no idea why you think forcing the 2nd icon -- which, by your admission, fits -- into the overflow menu would be a good idea. The 3rd menu item is in the overflow menu, which is accessed via the MENU button on devices that have one or a three-vertical-dots button on the action bar for devices that lack a MENU button.
I encountered this same issue and resolved it by doing two things, although I think the main reason is the second item. I was using the Android Asset Studio to create icons for my Actionbar menu.
In Android Asset Studio, I set the icon to "trim".
In my menu/activity_main.xml definition file (or whatever you call your menu definition file) I defined my menu icons with android:showAsAction="always"
It looked something like this:
<item android:id="#+id/menu_test"
android:icon="#drawable/ic_menu_test"
android:title="#string/menu_test"
android:showAsAction="always" />