Example usage for android.util AttributeSet getAttributeResourceValue

List of usage examples for android.util AttributeSet getAttributeResourceValue

Introduction

In this page you can find the example usage for android.util AttributeSet getAttributeResourceValue.

Prototype

public int getAttributeResourceValue(int index, int defaultValue);

Source Link

Document

Return the value of attribute at 'index' as a resource identifier.

Usage

From source file:Main.java

/**
 * Applies stirng to View by Reflection/*from ww w. j av  a  2s  .co  m*/
 *
 * @param context      Context
 * @param view         View to apply to.
 * @param attrs        Attributes of Layout
 */
public static void applyResourceStringToView(Context context, View view, AttributeSet attrs) {
    if (attrs != null) {
        Resources res = context.getResources();
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            int resId = attrs.getAttributeResourceValue(i, 0);
            if (resId > 0) {
                String resType = res.getResourceTypeName(resId);
                if (resType.equals("string")) {
                    String attrName = attrs.getAttributeName(i);
                    try {
                        Method method = view.getClass().getMethod("set" + capitalize(attrName),
                                CharSequence.class);
                        method.setAccessible(true);
                        method.invoke(view, res.getString(resId));
                    } catch (NoSuchMethodException e) {
                    } catch (Exception e) {
                    }
                }
            }
        }
    }
}

From source file:com.openAtlas.bundleInfo.maker.PackageLite.java

private static boolean parseApplication(PackageLite packageLite, XmlPullParser xmlPullParser,
        AttributeSet attributeSet) throws Exception {
    int i;// ww  w.  j  a  va2  s  .c o  m
    String str = packageLite.packageName;
    for (i = 0; i < attributeSet.getAttributeCount(); i++) {
        String attributeName = attributeSet.getAttributeName(i);
        if (attributeName.equals("name")) {
            packageLite.applicationClassName = buildClassName(str, attributeSet.getAttributeValue(i));
        } else if (attributeName.equals("icon")) {
            packageLite.applicationIcon = attributeSet.getAttributeResourceValue(i, 0);
        } else if (attributeName.equals("label")) {
            packageLite.applicationLabel = attributeSet.getAttributeResourceValue(i, 0);
        } else if (attributeName.equals("description")) {
            packageLite.applicationDescription = attributeSet.getAttributeResourceValue(i, 0);
        }
    }

    final int innerDepth = xmlPullParser.getDepth();

    int type;
    while ((type = xmlPullParser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG || xmlPullParser.getDepth() > innerDepth)) {
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
            continue;
        }

        String tagName = xmlPullParser.getName();
        if (tagName.equals("activity")) {

            parseComponentData(packageLite, xmlPullParser, attributeSet, false, Component.ACTIVITY);

        } else if (tagName.equals("receiver")) {

            parseComponentData(packageLite, xmlPullParser, attributeSet, true, Component.RECEIVER);

        } else if (tagName.equals("service")) {

            parseComponentData(packageLite, xmlPullParser, attributeSet, true, Component.SERVISE);

        } else if (tagName.equals("provider")) {

            parseComponentData(packageLite, xmlPullParser, attributeSet, false, Component.PROVIDER);

        } else if (tagName.equals("activity-alias")) {
        } else if (xmlPullParser.getName().equals("meta-data")) {

            parseMetaData(xmlPullParser, attributeSet, packageLite);

        } else if (tagName.equals("uses-library")) {
        } else if (tagName.equals("uses-package")) {
        } else {
        }
    }

    return true;
}

From source file:de.vanita5.twittnuker.util.ThemeUtils.java

public static int findAttributeResourceValue(AttributeSet attrs, String name, int defaultValue) {
    for (int i = 0, j = attrs.getAttributeCount(); i < j; i++) {
        if (attrs.getAttributeName(i).equals(name))
            return attrs.getAttributeResourceValue(i, defaultValue);
    }//from  www . ja  va2s. c o m
    return defaultValue;
}

From source file:me.henrytao.mdcore.core.MdCompat.java

/**
 * A temporary fix for android.content.res.ColorStateList.inflate
 *///w w w.  j  a v  a  2  s  .c om
public static ColorStateList getColorStateList(Context context, int resId)
        throws IOException, XmlPullParserException {
    XmlResourceParser parser = context.getResources().getXml(resId);
    AttributeSet attrs = Xml.asAttributeSet(parser);
    Resources r = context.getResources();

    final int innerDepth = parser.getDepth() + 2;
    int depth;
    int type;

    List<int[]> customStateList = new ArrayList<>();
    List<Integer> customColorList = new ArrayList<>();

    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG || depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }
        // Parse all unrecognized attributes as state specifiers.
        int j = 0;
        final int numAttrs = attrs.getAttributeCount();
        int color = 0;
        float alpha = 1.0f;
        int[] stateSpec = new int[numAttrs];
        for (int i = 0; i < numAttrs; i++) {
            final int stateResId = attrs.getAttributeNameResource(i);
            switch (stateResId) {
            case android.R.attr.color:
                int colorAttrId = attrs.getAttributeResourceValue(i, 0);
                if (colorAttrId == 0) {
                    String colorAttrValue = attrs.getAttributeValue(i);
                    colorAttrId = Integer.valueOf(colorAttrValue.replace("?", ""));
                    color = getColorFromAttribute(context, colorAttrId);
                } else {
                    color = ContextCompat.getColor(context, colorAttrId);
                }
                break;
            case android.R.attr.alpha:
                try {
                    alpha = attrs.getAttributeFloatValue(i, 1.0f);
                } catch (Exception e) {
                    String alphaAttrValue = attrs.getAttributeValue(i);
                    alpha = getFloatFromAttribute(context, Integer.valueOf(alphaAttrValue.replace("?", "")));
                }
                break;
            default:
                stateSpec[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId;
            }
        }
        stateSpec = StateSet.trimStateSet(stateSpec, j);
        color = modulateColorAlpha(color, alpha);

        customColorList.add(color);
        customStateList.add(stateSpec);
    }

    int[] colors = new int[customColorList.size()];
    int[][] states = new int[customStateList.size()][];
    int i = 0;
    for (int n = states.length; i < n; i++) {
        colors[i] = customColorList.get(i);
        states[i] = customStateList.get(i);
    }
    return new ColorStateList(states, colors);
}

From source file:com.app.bolayam.view.APTabPageIndicator.java

private void initCustomStyleAttribute(AttributeSet attrs) {
    String style = "style";
    for (int i = 0; i < attrs.getAttributeCount(); i++) {
        if (style.equals(attrs.getAttributeName(i))) {
            mCustomStyleAttribute = attrs.getAttributeResourceValue(i, -1);
            break;
        }// w w w .j av a2s.  com
    }
}