Java Code Examples for com.android.tools.lint.detector.api.LintUtils#getChildCount()

The following examples show how to use com.android.tools.lint.detector.api.LintUtils#getChildCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChildCountDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
    int childCount = LintUtils.getChildCount(element);
    String tagName = element.getTagName();
    if (tagName.equals(SCROLL_VIEW) || tagName.equals(HORIZONTAL_SCROLL_VIEW)) {
        if (childCount > 1 && getAccurateChildCount(element) > 1) {
            context.report(SCROLLVIEW_ISSUE, element,
                    context.getLocation(element), "A scroll view can have only one child");
        }
    } else {
        // Adapter view
        if (childCount > 0 && getAccurateChildCount(element) > 0) {
            context.report(ADAPTER_VIEW_ISSUE, element,
                    context.getLocation(element),
                    "A list/grid should have no children declared in XML");
        }
    }
}
 
Example 2
Source File: UseCompoundDrawableDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
    int childCount = LintUtils.getChildCount(element);
    if (childCount == 2) {
        List<Element> children = LintUtils.getChildren(element);
        Element first = children.get(0);
        Element second = children.get(1);
        if ((first.getTagName().equals(IMAGE_VIEW) &&
                second.getTagName().equals(TEXT_VIEW) &&
                !first.hasAttributeNS(ANDROID_URI, ATTR_LAYOUT_WEIGHT)) ||
            ((second.getTagName().equals(IMAGE_VIEW) &&
                    first.getTagName().equals(TEXT_VIEW) &&
                    !second.hasAttributeNS(ANDROID_URI, ATTR_LAYOUT_WEIGHT)))) {
            // If the layout has a background, ignore since it would disappear from
            // the TextView
            if (element.hasAttributeNS(ANDROID_URI, ATTR_BACKGROUND)) {
                return;
            }

            // Certain scale types cannot be done with compound drawables
            String scaleType = first.getTagName().equals(IMAGE_VIEW)
                    ? first.getAttributeNS(ANDROID_URI, ATTR_SCALE_TYPE)
                    : second.getAttributeNS(ANDROID_URI, ATTR_SCALE_TYPE);
            if (scaleType != null && !scaleType.isEmpty()) {
                // For now, ignore if any scale type is set
                return;
            }

            context.report(ISSUE, element, context.getLocation(element),
                    "This tag and its children can be replaced by one `<TextView/>` and " +
                            "a compound drawable");
        }
    }
}
 
Example 3
Source File: UselessViewDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
    int childCount = LintUtils.getChildCount(element);
    if (childCount == 0) {
        // Check to see if this is a leaf layout that can be removed
        checkUselessLeaf(context, element);
    } else {
        // Check to see if this is a middle-man layout which can be removed
        checkUselessMiddleLayout(context, element);
    }
}