0

I am using Android Studio context menu to make a new custom view. When i add the new view the build is broken. Why is Android Studio adding code that breaks the build.

I am running Android Studio 2.1.3.

The process goes as follows...

  1. I right click on my src/java/[my_package] folder.
  2. I hit New > UI Component > Custom View
  3. In the subsequent dialog I name my view and hit enter.
  4. I hit make project and I get the following error...
Error:(7) No resource identifier found for attribute 'exampleColor' in package 'com.username.appname'
Error:(7) No resource identifier found for attribute 'exampleDimension' in package 'com.username.appname'
Error:(7) No resource identifier found for attribute 'exampleDrawable' in package 'com.username.appname'
Error:(7) No resource identifier found for attribute 'exampleString' in package 'com.username.appname'

Here is the class that Android Studio created. I have not touched a single line of code here with the exception of redacting my user and app name info.

I seems like the error is the R. can not be resolved in the file.

package com.username.appname;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.example.ringtonegenerator.R;

/**
 * TODO: document your custom view class.
 */
public class SwipeLayout extends LinearLayout {
    private String mExampleString; // TODO: use a default from R.string...
    private int mExampleColor = Color.RED; // TODO: use a default from R.color...
    private float mExampleDimension = 0; // TODO: use a default from R.dimen...
    private Drawable mExampleDrawable;

    private TextPaint mTextPaint;
    private float mTextWidth;
    private float mTextHeight;

    public SwipeLayout(Context context) {
        super(context);
        init(null, 0);
    }

    public SwipeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public SwipeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.SwipeLayout, defStyle, 0);

        mExampleString = a.getString(
                R.styleable.SwipeLayout_exampleString);
        mExampleColor = a.getColor(
                R.styleable.SwipeLayout_exampleColor,
                mExampleColor);
        // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
        // values that should fall on pixel boundaries.
        mExampleDimension = a.getDimension(
                R.styleable.SwipeLayout_exampleDimension,
                mExampleDimension);

        if (a.hasValue(R.styleable.SwipeLayout_exampleDrawable)) {
            mExampleDrawable = a.getDrawable(
                    R.styleable.SwipeLayout_exampleDrawable);
            mExampleDrawable.setCallback(this);
        }

        a.recycle();

        // Set up a default TextPaint object
        mTextPaint = new TextPaint();
        mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextAlign(Paint.Align.LEFT);

        // Update TextPaint and text measurements from attributes
        invalidateTextPaintAndMeasurements();
    }

    private void invalidateTextPaintAndMeasurements() {
        mTextPaint.setTextSize(mExampleDimension);
        mTextPaint.setColor(mExampleColor);
        mTextWidth = mTextPaint.measureText(mExampleString);

        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        mTextHeight = fontMetrics.bottom;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // TODO: consider storing these as member variables to reduce
        // allocations per draw cycle.
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();

        int contentWidth = getWidth() - paddingLeft - paddingRight;
        int contentHeight = getHeight() - paddingTop - paddingBottom;

        // Draw the text.
        canvas.drawText(mExampleString,
                paddingLeft + (contentWidth - mTextWidth) / 2,
                paddingTop + (contentHeight + mTextHeight) / 2,
                mTextPaint);

        // Draw the example drawable on top of the text.
        if (mExampleDrawable != null) {
            mExampleDrawable.setBounds(paddingLeft, paddingTop,
                    paddingLeft + contentWidth, paddingTop + contentHeight);
            mExampleDrawable.draw(canvas);
        }
    }

    /**
     * Gets the example string attribute value.
     *
     * @return The example string attribute value.
     */
    public String getExampleString() {
        return mExampleString;
    }

    /**
     * Sets the view's example string attribute value. In the example view, this string
     * is the text to draw.
     *
     * @param exampleString The example string attribute value to use.
     */
    public void setExampleString(String exampleString) {
        mExampleString = exampleString;
        invalidateTextPaintAndMeasurements();
    }

    /**
     * Gets the example color attribute value.
     *
     * @return The example color attribute value.
     */
    public int getExampleColor() {
        return mExampleColor;
    }

    /**
     * Sets the view's example color attribute value. In the example view, this color
     * is the font color.
     *
     * @param exampleColor The example color attribute value to use.
     */
    public void setExampleColor(int exampleColor) {
        mExampleColor = exampleColor;
        invalidateTextPaintAndMeasurements();
    }

    /**
     * Gets the example dimension attribute value.
     *
     * @return The example dimension attribute value.
     */
    public float getExampleDimension() {
        return mExampleDimension;
    }

    /**
     * Sets the view's example dimension attribute value. In the example view, this dimension
     * is the font size.
     *
     * @param exampleDimension The example dimension attribute value to use.
     */
    public void setExampleDimension(float exampleDimension) {
        mExampleDimension = exampleDimension;
        invalidateTextPaintAndMeasurements();
    }

    /**
     * Gets the example drawable attribute value.
     *
     * @return The example drawable attribute value.
     */
    public Drawable getExampleDrawable() {
        return mExampleDrawable;
    }

    /**
     * Sets the view's example drawable attribute value. In the example view, this drawable is
     * drawn above the text.
     *
     * @param exampleDrawable The example drawable attribute value to use.
     */
    public void setExampleDrawable(Drawable exampleDrawable) {
        mExampleDrawable = exampleDrawable;
    }
}
Scorb
  • 1,654
  • 13
  • 70
  • 144
  • Can you show the source where these errors show up? – Ted Hopp Sep 14 '16 at 02:21
  • My question was answer in this thread. What is the protocol to direct there? http://stackoverflow.com/questions/5819369/error-no-resource-identifier-found-for-attribute-adsize-in-package-com-googl – Scorb Sep 17 '16 at 18:49
  • Just close your own question. You might also edit it to put in a link to the other question and explain you are closing this as a duplicate. – Ted Hopp Sep 18 '16 at 20:24

1 Answers1

0

When you add a new View through this menu, Studio populates it with some example code and resources. It appears some of the example code references attributes that it does not add. You will need to either provide the resources it requires (exampleColor etc.) in your resource files or change the example references to real ones.

K. Beier
  • 11
  • 1
  • Hmm. I would prefer if it either properly create the attributes, or simply not reference them at all. Seems like that makes more sense than have a tool that creates unbuildable code... – Scorb Sep 14 '16 at 02:26
  • @ScottF - I'm also using AS 2.1.3 and the code it generates is error-free for me. Perhaps something else is going on. Look for an error in one of the resource files. Perhaps there's a name collision between the generated code and your existing code. (Definitely quite possible if this isn't the first new custom view code you had AS generate for you.) – Ted Hopp Sep 14 '16 at 02:36
  • Hmm interesting. This is definitely the first custom view I have made in this project. But it is good to know that AS has the potential to do it correctly. Does your code match mine? – Scorb Sep 14 '16 at 03:16