Example usage for android.content.res XmlResourceParser getAttributeNameResource

List of usage examples for android.content.res XmlResourceParser getAttributeNameResource

Introduction

In this page you can find the example usage for android.content.res XmlResourceParser getAttributeNameResource.

Prototype

public int getAttributeNameResource(int index);

Source Link

Document

Return the resource ID associated with the given attribute name.

Usage

From source file:com.wit.and.dialog.xml.XmlDialogParser.java

/**
 * <p>/*w  w  w .j  ava  2 s .com*/
 * </p>
 *
 * @param parser
 * @return
 */
public DialogFragment parseXmlDialog(XmlResourceParser parser) {
    O options = onCreateEmptyOptions();

    if (options != null) {
        // Set up default icon.
        options.icon(getDefaultIcon());

        final int attrCount = parser.getAttributeCount();
        // Parse global attributes.
        for (int i = 0; i < attrCount; i++) {
            onParseAttribute(parser, parser.getAttributeNameResource(i), i, options);
        }
    }

    // Create dialog.
    return onCreateDialog(options);
}

From source file:com.wit.and.dialog.internal.xml.XmlDialogInflater.java

/**
 * <p>/*from w w  w.ja  va2  s.c  o  m*/
 * Inflates dialog fragment instance form the given XML dialogs set resource using one
 * of the registered XML dialog parsers.
 * </p>
 *
 * @param dialogID  Id of dialog in the given <var>xmlSetRes</var> to inflate.
 * @param xmlSetRes Resource of XML file placed in the application resources. All XML dialogs in this XML file
 *                  must be placed inside <b>&lt;Dialogs&gt&lt;/Dialogs&gt;</b> root tag.
 * @return New instance of {@link DialogFragment} or its derived classes, depends on the XML dialog
 * root tag.
 * @throws XmlDialogInflater.UnsupportedXmlDialogTagException
 * @throws java.security.InvalidParameterException
 * @throws java.lang.IllegalStateException
 * @see #registerParser(Class, String)
 */
public DialogFragment inflateDialog(int dialogID, int xmlSetRes) {
    XmlResourceParser xmlParser = mResources.getXml(xmlSetRes);
    if (xmlParser == null)
        return null;

    DialogFragment dialog = null;

    long time;
    if (DEBUG) {
        time = System.currentTimeMillis();
    }

    try {
        int xmlEvent;
        boolean dialogsTagResolved = false;

        while ((xmlEvent = xmlParser.getEventType()) != XmlResourceParser.END_DOCUMENT) {
            switch (xmlEvent) {
            case XmlResourceParser.START_DOCUMENT:
                break;
            case XmlResourceParser.START_TAG:
                String tag = xmlParser.getName();

                if (!dialogsTagResolved) {
                    // Check valid root tag.
                    if (!tag.equals(DIALOGS_SET_ROOT_TAG)) {
                        throw new UnsupportedXmlDialogTagException(
                                "Only 'Dialogs' tag is allowed as root tag of XML dialogs set.");
                    }
                    dialogsTagResolved = true;
                } else {
                    // Check for empty dialog.
                    if (xmlParser.getAttributeCount() == 0) {
                        throw new IllegalStateException("Empty dialogs are not allowed.");
                    }

                    // Find the dialog with requested id.
                    // Note, that first attribute of each dialog must be its "id" to preserve
                    // fast parsing of xml file.
                    final int attr = xmlParser.getAttributeNameResource(0);
                    if (attr != android.R.attr.id) {
                        throw new InvalidParameterException(
                                "First attribute of XML dialog must be always 'android:id'.");
                    }

                    // Finally check the dialog id.
                    final int id = xmlParser.getAttributeResourceValue(0, 0);
                    if (dialogID == id) {
                        dialog = parseDialogInner(xmlParser);
                    }
                }
                break;
            }
            if (dialog != null) {
                break;
            }
            xmlParser.next();
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (DEBUG) {
        Log.d(TAG, "Parsing of XML dialog from dialogs set in "
                + Long.toString(System.currentTimeMillis() - time) + "ms");
    }

    return dialog;
}