Example usage for android.util AttributeSet getAttributeBooleanValue

List of usage examples for android.util AttributeSet getAttributeBooleanValue

Introduction

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

Prototype

public boolean getAttributeBooleanValue(int index, boolean defaultValue);

Source Link

Document

Return the boolean value of attribute at 'index'.

Usage

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 a2 s  .c  o  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:it.scoppelletti.mobilepower.widget.DateControl.java

/**
 * Costruttore./*from www . jav a 2s  .com*/
 * 
 * @param ctx   Contesto.
 * @param attrs Attributi.
 */
public DateControl(Context ctx, AttributeSet attrs) {
    super(ctx, attrs);

    int i, n;
    String name;

    myIsEmptyAllowed = true;
    myIsResetEnabled = false;

    n = attrs.getAttributeCount();
    for (i = 0; i < n; i++) {
        name = attrs.getAttributeName(i);
        if (DateControl.STATE_DIALOGTAG.equals(name)) {
            myDialogTag = attrs.getAttributeValue(i);
        } else if (DateControl.STATE_ISEMPTYALLOWED.equals(name)) {
            myIsEmptyAllowed = attrs.getAttributeBooleanValue(i, myIsEmptyAllowed);
        } else if (DateControl.STATE_ISRESETENABLED.equals(name)) {
            myIsResetEnabled = attrs.getAttributeBooleanValue(i, myIsResetEnabled);
        }
    }

    init(ctx);
}

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

/**
 * Fill in this object based on the contents of an XML "selector" element.
 *///from ww w .j a  v  a  2 s .  c om
private static ColorStateList inflate(@NonNull Resources r, @NonNull XmlPullParser parser,
        @NonNull AttributeSet attrs, @Nullable Resources.Theme theme)
        throws XmlPullParserException, IOException {
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    int type;
    int defaultColor = DEFAULT_COLOR;

    int[][] stateSpecList = new int[20][];
    int[] colorList = new int[stateSpecList.length];
    int listSize = 0;

    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;
        }

        final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.ColorStateListItem);
        final int baseColor = a.getColor(R.styleable.ColorStateListItem_android_color, Color.MAGENTA);

        float alphaMod = 1.0f;
        if (a.hasValue(R.styleable.ColorStateListItem_android_alpha)) {
            alphaMod = a.getFloat(R.styleable.ColorStateListItem_android_alpha, alphaMod);
        } else if (a.hasValue(R.styleable.ColorStateListItem_alpha)) {
            alphaMod = a.getFloat(R.styleable.ColorStateListItem_alpha, alphaMod);
        }

        a.recycle();

        // Parse all unrecognized attributes as state specifiers.
        int j = 0;
        final int numAttrs = attrs.getAttributeCount();
        int[] stateSpec = new int[numAttrs];
        for (int i = 0; i < numAttrs; i++) {
            final int stateResId = attrs.getAttributeNameResource(i);
            if (stateResId != android.R.attr.color && stateResId != android.R.attr.alpha
                    && stateResId != R.attr.alpha) {
                // Unrecognized attribute, add to state set
                stateSpec[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId;
            }
        }
        stateSpec = StateSet.trimStateSet(stateSpec, j);

        // Apply alpha modulation. If we couldn't resolve the color or
        // alpha yet, the default values leave us enough information to
        // modulate again during applyTheme().
        final int color = modulateColorAlpha(baseColor, alphaMod);
        if (listSize == 0 || stateSpec.length == 0) {
            defaultColor = color;
        }

        colorList = GrowingArrayUtils.append(colorList, listSize, color);
        stateSpecList = GrowingArrayUtils.append(stateSpecList, listSize, stateSpec);
        listSize++;
    }

    int[] colors = new int[listSize];
    int[][] stateSpecs = new int[listSize][];
    System.arraycopy(colorList, 0, colors, 0, listSize);
    System.arraycopy(stateSpecList, 0, stateSpecs, 0, listSize);

    return new ColorStateList(stateSpecs, colors);
}

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

/**
 * A temporary fix for android.content.res.ColorStateList.inflate
 *//*from w  w w .  jav a 2s. 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:android.content.pm.PackageParser.java

private static ApkLite parseApkLite(String codePath, Resources res, XmlPullParser parser, AttributeSet attrs,
        int flags, Signature[] signatures) throws IOException, XmlPullParserException, PackageParserException {
    final Pair<String, String> packageSplit = parsePackageSplitNames(parser, attrs, flags);

    int installLocation = PARSE_DEFAULT_INSTALL_LOCATION;
    int versionCode = 0;
    int revisionCode = 0;
    boolean coreApp = false;
    boolean multiArch = false;
    boolean extractNativeLibs = true;

    for (int i = 0; i < attrs.getAttributeCount(); i++) {
        final String attr = attrs.getAttributeName(i);
        if (attr.equals("installLocation")) {
            installLocation = attrs.getAttributeIntValue(i, PARSE_DEFAULT_INSTALL_LOCATION);
        } else if (attr.equals("versionCode")) {
            versionCode = attrs.getAttributeIntValue(i, 0);
        } else if (attr.equals("revisionCode")) {
            revisionCode = attrs.getAttributeIntValue(i, 0);
        } else if (attr.equals("coreApp")) {
            coreApp = attrs.getAttributeBooleanValue(i, false);
        }//  w w w  . jav a 2s.c o  m
    }

    // Only search the tree when the tag is directly below <manifest>
    int type;
    final int searchDepth = parser.getDepth() + 1;

    final List<VerifierInfo> verifiers = new ArrayList<VerifierInfo>();
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG || parser.getDepth() >= searchDepth)) {
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
            continue;
        }

        if (parser.getDepth() == searchDepth && "package-verifier".equals(parser.getName())) {
            final VerifierInfo verifier = parseVerifier(res, parser, attrs, flags);
            if (verifier != null) {
                verifiers.add(verifier);
            }
        }

        if (parser.getDepth() == searchDepth && "application".equals(parser.getName())) {
            for (int i = 0; i < attrs.getAttributeCount(); ++i) {
                final String attr = attrs.getAttributeName(i);
                if ("multiArch".equals(attr)) {
                    multiArch = attrs.getAttributeBooleanValue(i, false);
                }
                if ("extractNativeLibs".equals(attr)) {
                    extractNativeLibs = attrs.getAttributeBooleanValue(i, true);
                }
            }
        }
    }

    return new ApkLite(codePath, packageSplit.first, packageSplit.second, versionCode, revisionCode,
            installLocation, verifiers, signatures, coreApp, multiArch, extractNativeLibs);
}