Search in sources :

Example 1 with AttributeSet

use of android.util.AttributeSet in project cw-omnibus by commonsguy.

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 2 with AttributeSet

use of android.util.AttributeSet in project material-icon-lib by code-mc.

the class MaterialMenuInflater method afterInflate.

private void afterInflate(int menuRes, Menu menu) {
    IconData root = new IconData(0, 0, 0);
    XmlResourceParser parser = null;
    try {
        parser = mContext.getResources().getLayout(menuRes);
        AttributeSet attrs = Xml.asAttributeSet(parser);
        parseMenu(parser, attrs, root);
    } 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();
        // populate the menu with the parsed icons
        populateIcons(menu, root, mDefaultColor);
    }
}
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 3 with AttributeSet

use of android.util.AttributeSet in project GreenDroid by cyrilmottier.

the class ItemAdapter method createFromXml.

/**
     * Creates an ItemAdapter from a given XML document. Called on a parser
     * positioned at a tag in an XML document, tries to create an ItemAdapter
     * from that tag.
     * 
     * @param context The Context in which the ItemAdapter will be used in
     * @param parser The XmlPullParser
     * @return a new ItemAdapter constructed with the content of the file
     *         pointed by <em>xmlId</em>
     * @throws XmlPullParserException
     * @throws IOException
     */
public static ItemAdapter createFromXml(Context context, XmlPullParser parser) 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");
    }
    if (!parser.getName().equals("item-array")) {
        throw new XmlPullParserException("Unknown start tag. Should be 'item-array'");
    }
    final List<Item> items = new ArrayList<Item>();
    final int innerDepth = parser.getDepth() + 1;
    final Resources r = context.getResources();
    int depth;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        if (depth > innerDepth) {
            continue;
        }
        String name = parser.getName();
        Item item;
        if (name.equals("text-item")) {
            item = new TextItem();
        } else if (name.equals("longtext-item")) {
            item = new LongTextItem();
        } else if (name.equals("description-item")) {
            item = new DescriptionItem();
        } else if (name.equals("separator-item")) {
            item = new SeparatorItem();
        } else if (name.equals("progress-item")) {
            item = new ProgressItem();
        } else if (name.equals("drawable-item")) {
            item = new DrawableItem();
        } else if (name.equals("subtitle-item")) {
            item = new SubtitleItem();
        } else if (name.equals("subtext-item")) {
            item = new SubtextItem();
        } else if (name.equals("thumbnail-item")) {
            item = new ThumbnailItem();
        } else {
            // ItemAdapter and creates our own items via XML?
            throw new XmlPullParserException(parser.getPositionDescription() + ": invalid item tag " + name);
        }
        if (item != null) {
            item.inflate(r, parser, attrs);
            items.add(item);
        }
    }
    return new ItemAdapter(context, items);
}
Also used : SubtitleItem(greendroid.widget.item.SubtitleItem) LongTextItem(greendroid.widget.item.LongTextItem) TextItem(greendroid.widget.item.TextItem) ArrayList(java.util.ArrayList) ProgressItem(greendroid.widget.item.ProgressItem) DrawableItem(greendroid.widget.item.DrawableItem) Item(greendroid.widget.item.Item) LongTextItem(greendroid.widget.item.LongTextItem) TextItem(greendroid.widget.item.TextItem) DescriptionItem(greendroid.widget.item.DescriptionItem) ThumbnailItem(greendroid.widget.item.ThumbnailItem) SeparatorItem(greendroid.widget.item.SeparatorItem) SubtitleItem(greendroid.widget.item.SubtitleItem) SubtextItem(greendroid.widget.item.SubtextItem) DrawableItem(greendroid.widget.item.DrawableItem) ProgressItem(greendroid.widget.item.ProgressItem) SubtextItem(greendroid.widget.item.SubtextItem) AttributeSet(android.util.AttributeSet) ThumbnailItem(greendroid.widget.item.ThumbnailItem) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) LongTextItem(greendroid.widget.item.LongTextItem) SeparatorItem(greendroid.widget.item.SeparatorItem) DescriptionItem(greendroid.widget.item.DescriptionItem)

Example 4 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 5 with AttributeSet

use of android.util.AttributeSet in project android_frameworks_base by ParanoidAndroid.

the class SearchableInfo method getActivityMetaData.

/**
     * Get the metadata for a given activity
     * 
     * @param context runtime context
     * @param xml XML parser for reading attributes
     * @param cName The component name of the searchable activity
     * 
     * @result A completely constructed SearchableInfo, or null if insufficient XML data for it
     */
private static SearchableInfo getActivityMetaData(Context context, XmlPullParser xml, final ComponentName cName) {
    SearchableInfo result = null;
    Context activityContext = createActivityContext(context, cName);
    if (activityContext == null)
        return null;
    // forward through the file until it's reading the tag of interest.
    try {
        int tagType = xml.next();
        while (tagType != XmlPullParser.END_DOCUMENT) {
            if (tagType == XmlPullParser.START_TAG) {
                if (xml.getName().equals(MD_XML_ELEMENT_SEARCHABLE)) {
                    AttributeSet attr = Xml.asAttributeSet(xml);
                    if (attr != null) {
                        try {
                            result = new SearchableInfo(activityContext, attr, cName);
                        } catch (IllegalArgumentException ex) {
                            Log.w(LOG_TAG, "Invalid searchable metadata for " + cName.flattenToShortString() + ": " + ex.getMessage());
                            return null;
                        }
                    }
                } else if (xml.getName().equals(MD_XML_ELEMENT_SEARCHABLE_ACTION_KEY)) {
                    if (result == null) {
                        // Can't process an embedded element if we haven't seen the enclosing
                        return null;
                    }
                    AttributeSet attr = Xml.asAttributeSet(xml);
                    if (attr != null) {
                        try {
                            result.addActionKey(new ActionKeyInfo(activityContext, attr));
                        } catch (IllegalArgumentException ex) {
                            Log.w(LOG_TAG, "Invalid action key for " + cName.flattenToShortString() + ": " + ex.getMessage());
                            return null;
                        }
                    }
                }
            }
            tagType = xml.next();
        }
    } catch (XmlPullParserException e) {
        Log.w(LOG_TAG, "Reading searchable metadata for " + cName.flattenToShortString(), e);
        return null;
    } catch (IOException e) {
        Log.w(LOG_TAG, "Reading searchable metadata for " + cName.flattenToShortString(), e);
        return null;
    }
    return result;
}
Also used : Context(android.content.Context) AttributeSet(android.util.AttributeSet) 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