I was wondering if it were at all possible to have checkable icons in an action bar in ICS? Not with a check, but with a android:stateChecked property to play around with. I want it so when the user clicks on the icon, they enter a certain mode (and the background of the icon would be a bright color to let them know they are still in that mode), and when they click it again they are taken out of that mode. I've played around with some selector xmls but nothing has worked...Any ideas?
EDIT: I just learned that Checkable items cannot appear in main menus, they can only be in submenus. However, is there a way to still get the behavior of a checkable? It's easy enough to have a boolean like isInModeA, but is there a way to programmatically change the background/icon of an Action bar item?
There is a way to programmatically change the background/icon.
You can use ActionBar.setBackgroundDrawable, for instance, or setCustomView, or setIcon (found on the same page).
You may consider using a dropdown-like ActionProvider to provide e.g. a radio-button-like "on/off" rather than a checkbox.
One approach you could try is to keep the state saved in some variable and just display different items based on this state. Obviously, some of these items can change the state. You might look at doing this by implementing a custom action provider.
Related
I have already looked at how to highlight a menu item on selection? and I am also using Android Action Bar Style Generator. They are both essentially saying the same thing, from what I can tell. And neither does what I need. When I click on a menuItem. I want the item to remain highlight, until I click another one which would in turn become highlighted. How do I do that? I have even tried enabling a menuItem but seems to have no effect either. There seems to be no option to set a menu item as selected or focussed. So does anyone know how I might do this?
I am reading about action bars in BusyCoder's Android guide. There is an example:
<item
android:id="#+id/add"
android:actionLayout="#layout/add"
android:icon="#android:drawable/ic_menu_add"
android:showAsAction="ifRoom"
android:title="#string/add"/>
What is the point to have both android:actionLayout and android:icon defined? What are the conditions when the icon would show up?
Thanks for helping out Android noob
What are the conditions when the icon would show up?
I can't rule out the possibility that the action bar will decide that there is not enough room for the actionLayout, but there is enough room for an action item, which would therefore show the icon. Also, with ActionBarSherlock, the icon is shown even in the overflow on Android 2.x devices, and so again if the actionLayout would be too large, the icon might get used. Also, if I used ifRoom|collapseActionView instead of just ifRoom, the icon would be used.
That being said:
My sample does not actually watch for this theoretical action item, should it appear, to do anything if the user clicks on it
I am guessing at possible action bar behavior, as I have not tried any experiments to force the action bar to decide whether there is enough room for all actionLayout-configured items
here android:icon is defined to supply an icon for your menu item. This is always optional but it is recommended to have an icon for the menu item.
android:actionLayout is defined to supply a custom view for the menu item again this is optional but if u need some Image Views, Text Views, Edit Text Views etc. in your menu item, this is the way to go.
If you need more help see these guides. These I think will be more helpful to you.
Action Bar Android Developer Guide
Menu Resource Android Developer Guide
Also take a look at this example. This may give you a much better idea about android Action bars.
vimaltuts.com/android-tutorial-for-beginners/android-action-bar-tab-menu-example
Thank You!
I'm developing an app that uses ActionBarSherlock.
As you know, using android:showAsAction attribute, you can display an action menu on action bar. And, the number of menu items displayed on action bar differs according to user's environment(resolution, portrait/landscape and so on).
So, is it possible to get it by code?
No, you can't.
It varies based on a few factors and it would be safe if you just assume that it varies and can be anywhere from two to five (including a potential overflow menu item).
Did you try menu.size()? I'm not sure what you need exactly.
I was thinking doesn't this do the trick?: getSupportActionBar().getNavigationItemCount();
In my app i am using bottom navigation. I am accessing the menu items by accessing the size() property on menu.
for bottom navigation :-
bottomNavigation.menu.size()
I am using ActionBarSherlock. I wish to be able to make two buttons appear in the Action Bar in response to a certain user operation. The user operation is completely unrelated to the Action Bar. The visibility of the buttons needs to be controlled by calling a method. Also, response to clicking those buttons shall be handled by my own application code.
The buttons shall ideally look just like those that are created when defining menu items as Action Items using android:showAsAction="ifRoom|withText", as illustrated here.
My problem is that as far as I can tell, the standard ActionBar API provides no such mechanism to show or hide Action Item buttons at will, and the only time that the menu items can be defined is within onCreateOptionsMenu() which is of course called by the system.
My belief is that the only way I'm going to add buttons like this and show / hide them at will is to create a custom layout for them and make use of .setCustomView() to place them into the Action Bar. Would people generally agree with that, or is there something I have missed?
If I do go down the route of using .setCustomView(), I would like my buttons to look identical to Action Item buttons that ActionBarSherlock displays for a menu item that has the attribute android:showAsAction="ifRoom|withText". To do this, can anyone advise me which particular theme, style, or layouts from within the ActionBarSherlock library I should make use of? I have already tried to use R.layout.abs__action_menu_item_layout, but attempting to inflate this layout produces an exception relating to a colorStateList when attempting inflation of the CapitalizingButton that the layout contains.
You can call setVisibility on the MenuItem instances.
The documentation states that "You can safely hold on to menu (and any items created from it), making modifications to it as desired, until the next time onCreateOptionsMenu() is called."
If you want those two buttons to have the look and feel of menu items, then you should make them menu items. Your assumption that menu items can only be defined in onCreateOptionsMenu() is incorrect, because there's also a method called onPrepareOptionsMenu(), that will be called each time right before the menu is shown. Together with an activity's invalidateOptionsMenu() method, you can easily create a menu dat reflects the current state in realtime.
The alternative is to keep a reference to the individual MenuItem objects, as the documentation states its save to hold on to those, and change their visibility when appropriate. You may still have to call invalidateOptionsMenu() to update the menu- I can't remember from the top of my head. (Edit: Jake beat me to it on this one)
Personally, I prefer the first approach, since you keep all menu-related logic grouped together and the visibility is based on some sort of state/model. The second option may be more straightforward to implement, depending on your current code, but may result in menu-stuff all over the place.
have you checked the demo samples ?
they have this feature there on "feature toggles" .
In the Honeycomb ActionBar, I am trying to determine the position of the icons that appear as my ActionBar's 'showAsAction' options. Several of my icons are set to appear only if there is room. I want to include helpful comments under each icon when the application is first run. To properly position the comments , I need an idea of where these icons are.
I have called getActionBar() from the activity and the icons don't appear in the ActonBar's View. I know I can get a handle on the Menu Item through the onPrepare and the onCreate for the OptionsMenu, but they don't contain any positioning information since the ActionBar has not been rendered. Any ideas? I have also explored the onMenuVisibilityListener, but the ActionBar at that point still does not contain any information that I can use to calculate the relative positions of the option menu icons.
If there is a way to also just determine which actionbar items are actually appearing, I can calculate where to place things too.
I want to include helpful comments under each icon when the application is first run.
Then load a different menu XML resource on the first run, one that has withText along with ifRoom for android:showAsAction.
You could make this a configuration option for the user, so the user gets to choose which style to use.
If there is a way to also just determine which actionbar items are actually appearing, I can calculate where to place things too.
Except that you can't actually place anything there. The toolbar buttons take up the full action bar height.