Search in sources :

Example 41 with AttributeSet

use of android.util.AttributeSet in project Libraries-for-Android-Developers by eoecn.

the class MenuInflater method inflate.

/**
     * 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.,
     *            <code>R.menu.main_activity</code>)
     * @param menu The Menu to inflate into. The items and submenus will be
     *            added to this Menu.
     */
public void inflate(int menuRes, Menu 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();
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) InflateException(android.view.InflateException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 42 with AttributeSet

use of android.util.AttributeSet in project SmartAndroidSource by jaychou2012.

the class MenuInflater method inflate.

/**
	 * 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.,
	 *            <code>R.menu.main_activity</code>)
	 * @param menu
	 *            The Menu to inflate into. The items and submenus will be added
	 *            to this Menu.
	 */
public void inflate(int menuRes, Menu 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();
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) InflateException(android.view.InflateException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 43 with AttributeSet

use of android.util.AttributeSet in project Carbon by ZieIony.

the class CarbonResources method createFromXml.

/**
     * Create a drawable from an XML document using an optional {@link Resources.Theme}.
     * For more information on how to create resources in XML, see
     * <a href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a>.
     */
public Drawable createFromXml(XmlPullParser parser, Resources.Theme theme) throws XmlPullParserException, IOException {
    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");
    }
    Drawable drawable = createFromXmlInner(parser, attrs, theme);
    if (drawable == null) {
        throw new RuntimeException("Unknown initial tag: " + parser.getName());
    }
    return drawable;
}
Also used : AttributeSet(android.util.AttributeSet) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) VectorDrawable(carbon.drawable.VectorDrawable) LollipopDrawable(carbon.drawable.ripple.LollipopDrawable) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 44 with AttributeSet

use of android.util.AttributeSet in project remusic by aa112901.

the class ColorStateListUtils method createColorStateList.

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;
    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;
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) ColorStateList(android.content.res.ColorStateList) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) TypedValue(android.util.TypedValue)

Example 45 with AttributeSet

use of android.util.AttributeSet in project ElasticDownload by Tibolte.

the class AnimatedVectorDrawable method create.

public static AnimatedVectorDrawable create(Context c, Resources resources, int rid) {
    try {
        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(LOG_TAG, "parser error", e);
    } catch (IOException e) {
        Log.e(LOG_TAG, "parser error", e);
    }
    return null;
}
Also used : AttributeSet(android.util.AttributeSet) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Aggregations

AttributeSet (android.util.AttributeSet)262 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)160 IOException (java.io.IOException)125 XmlResourceParser (android.content.res.XmlResourceParser)113 TypedArray (android.content.res.TypedArray)78 Resources (android.content.res.Resources)46 Test (org.junit.Test)42 TypedValue (android.util.TypedValue)34 InflateException (android.view.InflateException)28 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)24 PackageManager (android.content.pm.PackageManager)22 Context (android.content.Context)20 ComponentName (android.content.ComponentName)18 XmlPullParser (org.xmlpull.v1.XmlPullParser)17 Intent (android.content.Intent)11 RemoteException (android.os.RemoteException)11 Bundle (android.os.Bundle)9 ArrayList (java.util.ArrayList)9 ActivityInfo (android.content.pm.ActivityInfo)7 ApplicationInfo (android.content.pm.ApplicationInfo)7