Example usage for android.content.res Resources getXml

List of usage examples for android.content.res Resources getXml

Introduction

In this page you can find the example usage for android.content.res Resources getXml.

Prototype

@NonNull
public XmlResourceParser getXml(@XmlRes int id) throws NotFoundException 

Source Link

Document

Return an XmlResourceParser through which you can read a generic XML resource for the given resource ID.

Usage

From source file:Main.java

public static XmlResourceParser getXml(Context context, int res) {
    Resources resource = getResources(context);// .getResources();
    return resource.getXml(res);
}

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

/**
 * Inflates a {@link ColorStateList} from resources, honouring theme attributes.
 *//*  w w w  .j a v  a2 s  .  c o m*/
@Nullable
private static ColorStateList inflateColorStateList(Context context, int resId) {
    if (isColorInt(context, resId)) {
        // The resource is a color int, we can't handle it so return null
        return null;
    }

    final Resources r = context.getResources();
    final XmlPullParser xml = r.getXml(resId);
    try {
        return AppCompatColorStateListInflater.createFromXml(r, xml, context.getTheme());
    } catch (Exception e) {
        Log.e(LOG_TAG, "Failed to inflate ColorStateList, leaving it to the framework", e);
    }
    return null;
}

From source file:com.wnafee.vector.compat.AnimatedVectorDrawable.java

public static AnimatedVectorDrawable create(Context c, Resources resources, int rid) {
    try {//from w  w w.  j a  v  a 2 s . c om
        final XmlPullParser parser = resources.getXml(rid);
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
            // Empty loop
        }
        if (type != XmlPullParser.START_TAG) {
            throw new XmlPullParserException("No start tag found");
        } else if (!ANIMATED_VECTOR.equals(parser.getName())) {
            throw new IllegalArgumentException("root node must start with: " + ANIMATED_VECTOR);
        }

        final AnimatedVectorDrawable drawable = new AnimatedVectorDrawable();
        drawable.inflate(c, resources, parser, attrs, null);

        return drawable;
    } catch (XmlPullParserException e) {
        Log.e(LOGTAG, "parser error", e);
    } catch (IOException e) {
        Log.e(LOGTAG, "parser error", e);
    }
    return null;
}

From source file:Main.java

public static Object getResource(Context context, Field field, int value) {
    Resources resources = context.getResources();
    Class type = field.getType();

    if (type.isAssignableFrom(Boolean.TYPE) || type.isAssignableFrom(Boolean.class))
        return resources.getBoolean(value);
    else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) {
        return resources.getInteger(value);
    } else if (type.isAssignableFrom(ColorStateList.class))
        return resources.getColorStateList(value);
    else if (type.isAssignableFrom(XmlResourceParser.class))
        return resources.getXml(value);
    else if (type.isAssignableFrom(Float.TYPE) || type.isAssignableFrom(Float.class))
        return resources.getDimension(value);
    else if (type.isAssignableFrom(Drawable.class))
        return resources.getDrawable(value);
    else if (type.isAssignableFrom(Animation.class))
        return AnimationUtils.loadAnimation(context, value);
    else if (type.isAssignableFrom(Movie.class))
        return resources.getMovie(value);
    else if (type.isAssignableFrom(String.class))
        return resources.getString(value);
    else if (type.isArray()) {
        if (type.getName().equals("[I")) {
            return resources.getIntArray(value);
        } else if (type.isAssignableFrom(String[].class)) {
            return resources.getStringArray(value);
        }/*  w  w w .  j av a  2 s .c  om*/
    }

    return null;
}

From source file:android.support.graphics.drawable.AnimatedVectorDrawableCompat.java

/**
 * Create a AnimatedVectorDrawableCompat object.
 *
 * @param context the context for creating the animators.
 * @param resId   the resource ID for AnimatedVectorDrawableCompat object.
 * @return a new AnimatedVectorDrawableCompat or null if parsing error is found.
 *//*w  w  w  .j  a  va  2  s.c  om*/
@Nullable
public static AnimatedVectorDrawableCompat create(@NonNull Context context, @DrawableRes int resId) {
    if (Build.VERSION.SDK_INT >= 23) {
        final AnimatedVectorDrawableCompat drawable = new AnimatedVectorDrawableCompat(context);
        drawable.mDelegateDrawable = ResourcesCompat.getDrawable(context.getResources(), resId,
                context.getTheme());
        drawable.mDelegateDrawable.setCallback(drawable.mCallback);
        drawable.mCachedConstantStateDelegate = new AnimatedVectorDrawableDelegateState(
                drawable.mDelegateDrawable.getConstantState());
        return drawable;
    }
    Resources resources = context.getResources();
    try {
        final XmlPullParser parser = resources.getXml(resId);
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
            // Empty loop
        }
        if (type != XmlPullParser.START_TAG) {
            throw new XmlPullParserException("No start tag found");
        }
        return createFromXmlInner(context, context.getResources(), parser, attrs, context.getTheme());
    } catch (XmlPullParserException e) {
        Log.e(LOGTAG, "parser error", e);
    } catch (IOException e) {
        Log.e(LOGTAG, "parser error", e);
    }
    return null;
}

From source file:com.hippo.vectorold.drawable.VectorDrawable.java

/** @hide */
public static VectorDrawable create(Resources resources, int rid) {
    try {/* ww  w  . ja  va2 s  .co m*/
        final XmlPullParser parser = resources.getXml(rid);
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
            // Empty loop
        }
        if (type != XmlPullParser.START_TAG) {
            throw new XmlPullParserException("No start tag found");
        }

        final VectorDrawable drawable = new VectorDrawable();
        drawable.inflate(resources, parser, attrs);

        return drawable;
    } catch (XmlPullParserException e) {
        Log.e(LOGTAG, "parser error", e);
    } catch (IOException e) {
        Log.e(LOGTAG, "parser error", e);
    }
    return null;
}

From source file:com.wnafee.vector.compat.VectorDrawable.java

public static VectorDrawable create(Resources resources, int rid) {
    try {/*from ww w .  ja va 2 s .  co  m*/
        final XmlPullParser parser = resources.getXml(rid);
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
            // Empty loop
        }
        if (type != XmlPullParser.START_TAG) {
            throw new XmlPullParserException("No start tag found");
        } else if (!SHAPE_VECTOR.equals(parser.getName())) {
            throw new IllegalArgumentException("root node must start with: " + SHAPE_VECTOR);
        }

        final VectorDrawable drawable = new VectorDrawable();
        drawable.inflate(resources, parser, attrs, null);

        return drawable;
    } catch (XmlPullParserException e) {
        Log.e(LOGTAG, "parser error", e);
    } catch (IOException e) {
        Log.e(LOGTAG, "parser error", e);
    }
    return null;
}

From source file:com.grottworkshop.gwsvectorsandboxlib.VectorDrawable.java

/** @hide */
static VectorDrawable create(Resources resources, int rid) {
    try {/*from  w  w  w.  j ava2 s  .c  o m*/
        final XmlPullParser parser = resources.getXml(rid);
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
            // Empty loop
        }
        if (type != XmlPullParser.START_TAG) {
            throw new XmlPullParserException("No start tag found");
        }

        final VectorDrawable drawable = new VectorDrawable();
        drawable.inflate(resources, parser, attrs);

        return drawable;
    } catch (XmlPullParserException e) {
        Log.e(LOGTAG, "parser error", e);
    } catch (IOException e) {
        Log.e(LOGTAG, "parser error", e);
    }
    return null;
}

From source file:com.bettervectordrawable.lib.graphics.drawable.VectorDrawable.java

/** @hide */
public static VectorDrawable create(Resources resources, int rid) {
    try {/*  w ww  . ja va2  s.  co  m*/
        final XmlPullParser parser = resources.getXml(rid);
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
            // Empty loop
        }
        if (type != XmlPullParser.START_TAG) {
            throw new XmlPullParserException("No start tag found");
        }
        final VectorDrawable drawable = new VectorDrawable();
        drawable.inflate(resources, parser, attrs);
        return drawable;
    } catch (XmlPullParserException e) {
        Log.e(LOGTAG, "parser error", e);
    } catch (IOException e) {
        Log.e(LOGTAG, "parser error", e);
    }
    return null;
}

From source file:com.android.leanlauncher.IconCache.java

public void loadIconPackDrawables(boolean forceReload) {
    final long t = SystemClock.uptimeMillis();
    synchronized (mIconPackDrawables) {
        if (!forceReload && mIconPackDrawables.size() > 0) {
            return;
        }//from  w ww. j a  v a  2 s . c  om

        // load appfilter.xml from the icon pack package
        try {
            XmlPullParser xpp = null;

            final Resources iconPackRes = getIconPackResources();
            if (iconPackRes == null) {
                return;
            }

            int appfilterid = iconPackRes.getIdentifier("appfilter", "xml", mCurrentIconTheme);
            if (appfilterid > 0) {
                xpp = iconPackRes.getXml(appfilterid);
            } else {
                // no resource found, try to open it from assets folder
                try {
                    InputStream is = iconPackRes.getAssets().open("appfilter.xml");

                    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                    factory.setNamespaceAware(true);
                    xpp = factory.newPullParser();
                    xpp.setInput(is, "utf-8");
                } catch (IOException e) {
                    Log.d(TAG, "Can't find appfilter.xml file in : " + mCurrentIconTheme);
                }
            }

            if (xpp != null) {
                int eventType = xpp.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_TAG) {
                        if ("item".equals(xpp.getName())) {
                            String componentName = null;
                            String drawableName = null;

                            for (int i = 0; i < xpp.getAttributeCount(); i++) {
                                if ("component".equals(xpp.getAttributeName(i))) {
                                    componentName = xpp.getAttributeValue(i);
                                } else if ("drawable".equals(xpp.getAttributeName(i))) {
                                    drawableName = xpp.getAttributeValue(i);
                                }
                            }

                            if (TextUtils.isEmpty(componentName) || TextUtils.isEmpty(drawableName)) {
                                eventType = xpp.next();
                                continue;
                            }

                            try {
                                componentName = componentName.substring(componentName.indexOf('{') + 1,
                                        componentName.indexOf('}'));
                            } catch (StringIndexOutOfBoundsException e) {
                                Log.d(TAG, "Can't parse icon for package = " + componentName);
                                eventType = xpp.next();
                                continue;
                            }

                            ComponentName componentNameKey = ComponentName.unflattenFromString(componentName);
                            if (componentNameKey != null) {
                                mIconPackDrawables.put(componentNameKey, drawableName);
                            } else {
                                Log.d(TAG, "ComponentName can't be obtained from: " + componentName);
                            }
                        } else if ("iconback".equals(xpp.getName())) {
                            for (int i = 0; i < xpp.getAttributeCount(); i++) {
                                if (xpp.getAttributeName(i).startsWith("img")) {
                                    mIconBackgrounds.add(loadBitmapFromIconPack(xpp.getAttributeValue(i)));
                                }
                            }
                        } else if ("iconmask".equals(xpp.getName())) {
                            if (xpp.getAttributeCount() > 0 && "img1".equals(xpp.getAttributeName(0))) {
                                mIconMask = loadBitmapFromIconPack(xpp.getAttributeValue(0));
                            }
                        } else if ("iconupon".equals(xpp.getName())) {
                            if (xpp.getAttributeCount() > 0 && "img1".equals(xpp.getAttributeName(0))) {
                                mIconFront = loadBitmapFromIconPack(xpp.getAttributeValue(0));
                            }
                        } else if ("scale".equals(xpp.getName())) {
                            if (xpp.getAttributeCount() > 0 && "factor".equals(xpp.getAttributeName(0))) {
                                mIconScaleFactor = Float.valueOf(xpp.getAttributeValue(0));
                            }
                        }
                    }
                    eventType = xpp.next();
                }
            }
            Log.d(TAG, "Finished parsing icon pack: " + (SystemClock.uptimeMillis() - t) + "ms");
        } catch (XmlPullParserException e) {
            Log.d(TAG, "Cannot parse icon pack appfilter.xml" + e);
        } catch (IOException e) {
            Log.d(TAG, "Exception loading icon pack " + e);
        }
    }
}