Example usage for android.util AttributeSet getAttributeCount

List of usage examples for android.util AttributeSet getAttributeCount

Introduction

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

Prototype

public int getAttributeCount();

Source Link

Document

Returns the number of attributes available in the set.

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));
    }/* w ww . j a  v a2  s  .c  o  m*/
    return bundle;
}

From source file:Main.java

/**
 * Applies stirng to View by Reflection//www .  j  a  va  2 s  . c  o 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.bilibili.magicasakura.utils.ColorStateListUtils.java

protected static int[] extractStateSet(AttributeSet attrs) {
    int j = 0;/*from  w  w  w.j  a  v  a 2s. co  m*/
    final int numAttrs = attrs.getAttributeCount();
    int[] states = new int[numAttrs];
    for (int i = 0; i < numAttrs; i++) {
        final int stateResId = attrs.getAttributeNameResource(i);
        switch (stateResId) {
        case 0:
            break;
        case android.R.attr.color:
        case android.R.attr.alpha:
            // Ignore attributes from StateListDrawableItem and
            // AnimatedStateListDrawableItem.
            continue;
        default:
            states[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId;
        }
    }
    states = StateSet.trimStateSet(states, j);
    return states;
}

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

private static boolean parseApplication(PackageLite packageLite, XmlPullParser xmlPullParser,
        AttributeSet attributeSet) throws Exception {
    int i;/*from   w ww  . ja  va 2  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:com.openAtlas.bundleInfo.maker.PackageLite.java

private static void parseComponentData(PackageLite packageLite, XmlPullParser xmlPullParser,
        AttributeSet attributeSet, boolean isDisable, Component mComponent) throws XmlPullParserException {

    String pkgName = packageLite.packageName;
    for (int index = 0; index < attributeSet.getAttributeCount(); index++) {
        if (attributeSet.getAttributeName(index).equals("name")) {
            String mComponentName = attributeSet.getAttributeValue(index);
            if (mComponentName.startsWith(".")) {
                mComponentName = pkgName.concat(mComponentName);
            }// w ww .  ja v a 2s  . c o  m
            switch (mComponent) {
            case PROVIDER:
                packageLite.providers.add(mComponentName);
                break;

            case ACTIVITY:
                packageLite.activitys.add(mComponentName);
                break;
            case SERVISE:
                packageLite.services.add(mComponentName);
                break;
            case RECEIVER:
                packageLite.receivers.add(mComponentName);
                break;
            default:
                break;
            }

            if (isDisable && !(TextUtils.equals(mComponentName,
                    XMLDISABLECOMPONENT_SSO_ALIPAY_AUTHENTICATION_SERVICE)
                    && TextUtils.equals(mComponentName, XMLDISABLECOMPONENT_SSO_AUTHENTICATION_SERVICE))) {
                packageLite.disableComponents.add(mComponentName);
            }

        }
    }

}

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

private static void parseMetaData(XmlPullParser xmlPullParser, AttributeSet attributeSet,
        PackageLite mPackageLite) throws XmlPullParserException, IOException {
    int i = 0;/*from  w w  w. j  a  va  2s. c om*/

    String mTagValue = null;
    String mTagName = null;
    int i2 = 0;
    while (i < attributeSet.getAttributeCount()) {
        String attributeName = attributeSet.getAttributeName(i);
        if (attributeName.equals("name")) {
            mTagName = attributeSet.getAttributeValue(i);
            i2++;
        } else if (attributeName.equals("value")) {
            mTagValue = attributeSet.getAttributeValue(i);
            i2++;
        }
        if (i2 >= 2) {
            break;
        }
        i++;
    }
    if (!(mTagName == null || mTagValue == null)) {
        if (mTagName.equals("dependency")) {
            String[] dependencys = mTagValue.split(",");
            for (String string : dependencys) {
                mPackageLite.dependency.add(string);
            }
            //   System.out.println("PackageLite.parseMetaData()"+mTagValue);
        }
        //bundle.putString(str2, str);
    }
    //return bundle;
}

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

/**
 * A temporary fix for android.content.res.ColorStateList.inflate
 *//*from  w ww . j  av a2 s.  c o  m*/
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: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);
    }//ww w  .  j a v a 2  s  .c  om
    return defaultValue;
}

From source file:dev.ronlemire.data.ListViewFragment.java

@Override
public void onInflate(Activity parentActivity, AttributeSet attrs, Bundle bundle) {
    Log.v(ListViewFragment.TAG, "in ListViewFragment onInflate. AttributeSet contains:");
    for (int i = 0; i < attrs.getAttributeCount(); i++) {
        Log.v(ListViewFragment.TAG, "    " + attrs.getAttributeName(i) + " = " + attrs.getAttributeValue(i));
    }/*from  ww  w  .  java 2s. c  o m*/
    super.onInflate(parentActivity, attrs, bundle);
}

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;
        }/*from w  w  w.j  a v a  2 s.c o m*/
    }
}