Example usage for android.util Xml asAttributeSet

List of usage examples for android.util Xml asAttributeSet

Introduction

In this page you can find the example usage for android.util Xml asAttributeSet.

Prototype

public static AttributeSet asAttributeSet(XmlPullParser parser) 

Source Link

Document

Return an AttributeSet interface for use with the given XmlPullParser.

Usage

From source file:Main.java

public static Bundle fromXml(XmlPullParser parser) {
    Bundle bundle = new Bundle();
    AttributeSet attr = Xml.asAttributeSet(parser);
    for (int i = 0; i < attr.getAttributeCount(); i++) {
        bundle.putString(attr.getAttributeName(i), attr.getAttributeValue(i));
    }/*from  w ww. j  a va 2  s  .c  om*/
    return bundle;
}

From source file:Main.java

/**
 * Get the xml as a AttributeSet//  w  w w  .  j ava2 s . c om
 *
 * @param is XML InputStream
 * @return AttributeSet representation of the xml stream 
 * @throws XmlPullParserException
 */
public static AttributeSet getAttributeSet(InputStream is) throws XmlPullParserException {
    XmlPullParser parser = Xml.newPullParser();
    parser.setInput(is, null);
    AttributeSet set = Xml.asAttributeSet(parser);
    return set;

}

From source file:Main.java

private static Animation createAnimationFromXml(Context c, XmlPullParser parser)
        throws XmlPullParserException, IOException {

    return createAnimationFromXml(c, parser, null, Xml.asAttributeSet(parser));
}

From source file:android.support.v7.content.res.AppCompatColorStateListInflater.java

/**
 * Creates a ColorStateList from an XML document using given a set of
 * {@link Resources} and a {@link Theme}.
 *
 * @param r Resources against which the ColorStateList should be inflated.
 * @param parser Parser for the XML document defining the ColorStateList.
 * @param theme Optional theme to apply to the color state list, may be
 *              {@code null}.//from  w  w w.ja  v a2s. co  m
 * @return A new color state list.
 */
@NonNull
public static ColorStateList createFromXml(@NonNull Resources r, @NonNull XmlPullParser parser,
        @Nullable Resources.Theme theme) throws XmlPullParserException, IOException {
    final AttributeSet attrs = Xml.asAttributeSet(parser);

    int type;
    while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
        // Seek parser to start tag.
    }

    if (type != XmlPullParser.START_TAG) {
        throw new XmlPullParserException("No start tag found");
    }

    return createFromXmlInner(r, parser, attrs, theme);
}

From source file:Main.java

static LayoutAnimationController createLayoutAnimationFromXml(Context c, XmlPullParser parser)
        throws XmlPullParserException, IOException {

    return createLayoutAnimationFromXml(c, parser, Xml.asAttributeSet(parser));
}

From source file:com.bilibili.magicasakura.utils.ColorStateListUtils.java

static ColorStateList createColorStateList(Context context, int resId) {
    if (resId <= 0)
        return null;

    TypedValue value = new TypedValue();
    context.getResources().getValue(resId, value, true);
    ColorStateList cl = null;/*from  w w  w  .j  a v a2s.c om*/
    if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        //Assume that "color/theme_color_primary" and "color/theme_color_profile" have the same color value;
        //However, "color/theme_color_primary" need to replace by themeId, "color/theme_color_profile" not.
        //If use value.data may cause "color/theme_color_profile" still been replaced by themeId
        cl = ColorStateList.valueOf(ThemeUtils.replaceColorById(context, value.resourceId));
    } else {
        final String file = value.string.toString();
        try {
            if (file.endsWith("xml")) {
                final XmlResourceParser rp = context.getResources().getAssets()
                        .openXmlResourceParser(value.assetCookie, file);
                final AttributeSet attrs = Xml.asAttributeSet(rp);
                int type;

                while ((type = rp.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
                    // Seek parser to start tag.
                }

                if (type != XmlPullParser.START_TAG) {
                    throw new XmlPullParserException("No start tag found");
                }

                cl = createFromXmlInner(context, rp, attrs);
                rp.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }
    return cl;
}

From source file:Main.java

/**
 * Retrieve an AttributeSet from a XML.//from   ww w .j a va2 s .  c om
 *
 * @param parser the XmlPullParser to use for the xml parsing.
 * @param searchedNodeName the name of the target node.
 * @return the AttributeSet retrieved from specified node.
 * @throws IOException
 * @throws XmlPullParserException
 */
public static AttributeSet getAttributeSet(XmlResourceParser parser, String searchedNodeName)
        throws XmlPullParserException, IOException {
    AttributeSet attrs = null;
    int type;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
    }
    String nodeName = parser.getName();
    if (!"alias".equals(nodeName)) {
        throw new RuntimeException();
    }
    int outerDepth = parser.getDepth();
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
            continue;
        }
        nodeName = parser.getName();
        if (searchedNodeName.equals(nodeName)) {
            outerDepth = parser.getDepth();
            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
                    && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
                if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                    continue;
                }
                nodeName = parser.getName();
                attrs = Xml.asAttributeSet(parser);
                break;
            }
            break;
        } else {
            skipCurrentTag(parser);
        }
    }
    return attrs;
}

From source file:android.support.v7.graphics.drawable.VectorDrawableCompat.java

public static VectorDrawableCompat createFromResource(Resources res, int id) {
    XmlPullParserFactory xppf = null;/*from   www  .  j  a va2  s. com*/
    XmlPullParser parser = null;
    try {
        xppf = XmlPullParserFactory.newInstance();
        parser = xppf.newPullParser();
        InputStream is = res.openRawResource(id);
        parser.setInput(is, null);
        // TODO: Use this getXml when the aapt is able to help us to keep the
        // attributes for v-21 in the compiled version.
        // XmlPullParser parser = res.getXml(id);
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        final VectorDrawableCompat drawable = new VectorDrawableCompat();
        drawable.inflateInternal(res, parser, attrs);
        return drawable;
    } catch (XmlPullParserException e) {
        Log.e(LOG_TAG, "XmlPullParser exception for res id : " + id);
    }
    return null;
}

From source file:Main.java

private static Interpolator createInterpolatorFromXml(Context c, XmlPullParser parser)
        throws XmlPullParserException, IOException {

    Interpolator interpolator = null;/*  w w  w .  j a  va2 s. c  o m*/

    // Make sure we are on a start tag.
    int type;
    int depth = parser.getDepth();

    while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
            && type != XmlPullParser.END_DOCUMENT) {

        if (type != XmlPullParser.START_TAG) {
            continue;
        }

        AttributeSet attrs = Xml.asAttributeSet(parser);

        String name = parser.getName();
        switch (name) {
        case "linearInterpolator":
            interpolator = new LinearInterpolator(c, attrs);
            break;
        case "accelerateInterpolator":
            interpolator = new AccelerateInterpolator(c, attrs);
            break;
        case "decelerateInterpolator":
            interpolator = new DecelerateInterpolator(c, attrs);
            break;
        case "accelerateDecelerateInterpolator":
            interpolator = new AccelerateDecelerateInterpolator(c, attrs);
            break;
        case "cycleInterpolator":
            interpolator = new CycleInterpolator(c, attrs);
            break;
        case "anticipateInterpolator":
            interpolator = new AnticipateInterpolator(c, attrs);
            break;
        case "overshootInterpolator":
            interpolator = new OvershootInterpolator(c, attrs);
            break;
        case "anticipateOvershootInterpolator":
            interpolator = new AnticipateOvershootInterpolator(c, attrs);
            break;
        case "bounceInterpolator":
            interpolator = new BounceInterpolator(c, attrs);
            break;
        default:
            throw new RuntimeException("Unknown interpolator name: " + parser.getName());
        }

    }

    return interpolator;

}

From source file:android.support.transition.TransitionInflater.java

/**
 * Loads a {@link Transition} object from a resource
 *
 * @param resource The resource id of the transition to load
 * @return The loaded Transition object// w w w .  j  a  va 2s .  c  o m
 * @throws android.content.res.Resources.NotFoundException when the
 *                                                         transition cannot be loaded
 */
public Transition inflateTransition(int resource) {
    XmlResourceParser parser = mContext.getResources().getXml(resource);
    try {
        return createTransitionFromXml(parser, Xml.asAttributeSet(parser), null);
    } catch (XmlPullParserException e) {
        throw new InflateException(e.getMessage(), e);
    } catch (IOException e) {
        throw new InflateException(parser.getPositionDescription() + ": " + e.getMessage(), e);
    } finally {
        parser.close();
    }
}