Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MenuItem Get Method for showAsAction

I am looking for the complimentary method to mentuItem.setShowAsAction(), i.e. menuItem.getShowAsAction() as there doesn't seem to be one.

http://developer.android.com/reference/android/view/MenuItem.html

As I need to record the current state before setting them to MenuItem.SHOW_AS_ACTION_NEVER, so when the orientation of the device changes back to landscape I can return the menu items to their old state.

I need to do this as Honeycomb doesn't provide a new row to show tabs like in ICS. So in honeycomb there is not enough space given to the tabs.

Is there another universal get properties method in Java or Android to find the setting in the XML for the attribute showAsAction in menuitem.

thanks

like image 257
pt123 Avatar asked Dec 05 '12 00:12

pt123


2 Answers

You can use this method, from what was said in the other answer:

@SuppressLint("RestrictedApi")
private int getShowAsActionFlag(MenuItem item) {
    MenuItemImpl itemImpl = ((MenuItemImpl) item);
    if (itemImpl.requiresActionButton()) return MenuItemImpl.SHOW_AS_ACTION_ALWAYS;
    else if (itemImpl.requestsActionButton()) return MenuItemImpl.SHOW_AS_ACTION_IF_ROOM;
    else if (itemImpl.showsTextAsAction()) return MenuItemImpl.SHOW_AS_ACTION_WITH_TEXT;
    else return MenuItemImpl.SHOW_AS_ACTION_NEVER;
}
like image 70
Nicolas Avatar answered Oct 03 '22 10:10

Nicolas


I found out that the below class has this method isActionButton()

android.support.v7.internal.view.menu.MenuItemImpl

Please note that MenuItem is an interface and not a class from which the supposed menuitem object instance was created.

If you are using the android.support.v7 compatibility package, simply cast the menuitem object to MenuItemImpl.

It is done like this:

((MenuItemImpl)item).isActionButton()
like image 32
Jimmy Ilenloa Avatar answered Oct 03 '22 08:10

Jimmy Ilenloa