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.
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.
You can call
setVisibility on the
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.”
have you checked the demo samples ?
they have this feature there on “feature toggles” .