FileDocCategorySizeDatePackage
MenuInflater.javaAPI DocAndroid 5.1 API20848Thu Mar 12 22:22:10 GMT 2015android.view

MenuInflater

public class MenuInflater extends Object
This class is used to instantiate menu XML files into Menu objects.

For performance reasons, menu inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use MenuInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R. something file.)

Fields Summary
private static final String
LOG_TAG
private static final String
XML_MENU
Menu tag name in XML.
private static final String
XML_GROUP
Group tag name in XML.
private static final String
XML_ITEM
Item tag name in XML.
private static final int
NO_ID
private static final Class[]
ACTION_VIEW_CONSTRUCTOR_SIGNATURE
private static final Class[]
ACTION_PROVIDER_CONSTRUCTOR_SIGNATURE
private final Object[]
mActionViewConstructorArguments
private final Object[]
mActionProviderConstructorArguments
private android.content.Context
mContext
private Object
mRealOwner
Constructors Summary
public MenuInflater(android.content.Context context)
Constructs a menu inflater.

see
Activity#getMenuInflater()


                
       
        mContext = context;
        mActionViewConstructorArguments = new Object[] {context};
        mActionProviderConstructorArguments = mActionViewConstructorArguments;
    
public MenuInflater(android.content.Context context, Object realOwner)
Constructs a menu inflater.

see
Activity#getMenuInflater()
hide

        mContext = context;
        mRealOwner = realOwner;
        mActionViewConstructorArguments = new Object[] {context};
        mActionProviderConstructorArguments = mActionViewConstructorArguments;
    
Methods Summary
private java.lang.ObjectfindRealOwner(java.lang.Object owner)

        if (owner instanceof Activity) {
            return owner;
        }
        if (owner instanceof ContextWrapper) {
            return findRealOwner(((ContextWrapper) owner).getBaseContext());
        }
        return owner;
    
android.content.ContextgetContext()

        return mContext;
    
private java.lang.ObjectgetRealOwner()

        if (mRealOwner == null) {
            mRealOwner = findRealOwner(mContext);
        }
        return mRealOwner;
    
public voidinflate(int menuRes, Menu menu)
Inflate a menu hierarchy from the specified XML resource. Throws {@link InflateException} if there is an error.

param
menuRes Resource ID for an XML layout resource to load (e.g., R.menu.main_activity)
param
menu The Menu to inflate into. The items and submenus will be added to this Menu.

        XmlResourceParser parser = null;
        try {
            parser = mContext.getResources().getLayout(menuRes);
            AttributeSet attrs = Xml.asAttributeSet(parser);
            
            parseMenu(parser, attrs, menu);
        } catch (XmlPullParserException e) {
            throw new InflateException("Error inflating menu XML", e);
        } catch (IOException e) {
            throw new InflateException("Error inflating menu XML", e);
        } finally {
            if (parser != null) parser.close();
        }
    
private voidparseMenu(org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs, Menu menu)
Called internally to fill the given menu. If a sub menu is seen, it will call this recursively.

        MenuState menuState = new MenuState(menu);

        int eventType = parser.getEventType();
        String tagName;
        boolean lookingForEndOfUnknownTag = false;
        String unknownTagName = null;

        // This loop will skip to the menu start tag
        do {
            if (eventType == XmlPullParser.START_TAG) {
                tagName = parser.getName();
                if (tagName.equals(XML_MENU)) {
                    // Go to next tag
                    eventType = parser.next();
                    break;
                }
                
                throw new RuntimeException("Expecting menu, got " + tagName);
            }
            eventType = parser.next();
        } while (eventType != XmlPullParser.END_DOCUMENT);
        
        boolean reachedEndOfMenu = false;
        while (!reachedEndOfMenu) {
            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (lookingForEndOfUnknownTag) {
                        break;
                    }
                    
                    tagName = parser.getName();
                    if (tagName.equals(XML_GROUP)) {
                        menuState.readGroup(attrs);
                    } else if (tagName.equals(XML_ITEM)) {
                        menuState.readItem(attrs);
                    } else if (tagName.equals(XML_MENU)) {
                        // A menu start tag denotes a submenu for an item
                        SubMenu subMenu = menuState.addSubMenuItem();
                        registerMenu(subMenu, attrs);

                        // Parse the submenu into returned SubMenu
                        parseMenu(parser, attrs, subMenu);
                    } else {
                        lookingForEndOfUnknownTag = true;
                        unknownTagName = tagName;
                    }
                    break;
                    
                case XmlPullParser.END_TAG:
                    tagName = parser.getName();
                    if (lookingForEndOfUnknownTag && tagName.equals(unknownTagName)) {
                        lookingForEndOfUnknownTag = false;
                        unknownTagName = null;
                    } else if (tagName.equals(XML_GROUP)) {
                        menuState.resetGroup();
                    } else if (tagName.equals(XML_ITEM)) {
                        // Add the item if it hasn't been added (if the item was
                        // a submenu, it would have been added already)
                        if (!menuState.hasAddedItem()) {
                            if (menuState.itemActionProvider != null &&
                                    menuState.itemActionProvider.hasSubMenu()) {
                                registerMenu(menuState.addSubMenuItem(), attrs);
                            } else {
                                registerMenu(menuState.addItem(), attrs);
                            }
                        }
                    } else if (tagName.equals(XML_MENU)) {
                        reachedEndOfMenu = true;
                    }
                    break;
                    
                case XmlPullParser.END_DOCUMENT:
                    throw new RuntimeException("Unexpected end of document");
            }
            
            eventType = parser.next();
        }
    
private voidregisterMenu(MenuItem item, android.util.AttributeSet set)
The method is a hook for layoutlib to do its magic. Nothing is needed outside of LayoutLib. However, it should not be deleted because it appears to do nothing.

    
private voidregisterMenu(SubMenu subMenu, android.util.AttributeSet set)
The method is a hook for layoutlib to do its magic. Nothing is needed outside of LayoutLib. However, it should not be deleted because it appears to do nothing.