0

I am new to Java. This is my code. I create an array in onCreate(), how can I use it in onDraw()? Thanks!

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    int Circle_list[] = { Color.BLACK, 20, 20, 50 };
}

private class MyViewCircle extends View {

    public MyViewCircle(Context context) {
        super(context);

        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(Circle_list[1], Circle_list[2], Circle_list[3], paint);
    }
}
1
  • Offtopic. Small improvement: create and initialize paint object in constructor, not in onDraw(). It is better for performance, because onDraw() is called many times and you don't need to create paint object every time. Commented Dec 6, 2011 at 2:52

3 Answers 3

1

Currently, your array is a local variable known only inside 'onCreate' method. You have to change its scope by defining it as an instance variable in order for onDraw to have access to it.

    private int[] Circle_list = null ;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);         
        Circle_list = new int[] { Color.BLACK, 20, 20, 50 };     
    }  
    private class MyViewCircle extends View {          
        public MyViewCircle(Context context) {
            super(context);              
        // TODO Auto-generated constructor stub         
        }
        @Override
        protected void onDraw(Canvas canvas) {
           // TODO Auto-generated method stub
           super.onDraw(canvas);
           Paint paint = new Paint();
           paint.setAntiAlias(true);
           paint.setColor(Color.WHITE);
           canvas.drawCircle(Circle_list[1], Circle_list[2], Circle_list[3],
                paint);         
        }     
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Referencing nested classes from XML is a pain. Also, it prevents certain errors from being caught at build time and it's difficult (perhaps impossible) to use custom attributes (which, I'll grant, OP is unlikely to be using). Also, I don't believe that the nested class can be private.
btw if Color you're using refers to the AWT type Color, then you can't assign Color.BLACK (type Color) to an int.
java.awt.Color is not part of the Android api. android.graphics.Color.BLACK is an int value.
To clarify my earlier comment about nested classes being private -- it's allowed in Java, but I don't think the class is then usable in an XML layout file in Android.
@Ted Hopp - Thanks, good to know. Didn't notice Android tag so was going off general principal.
1

In class MyCircleView create a method to pass in a circle array.

public class MyViewCircle extends View {
    int[] circle_list;

    public MyViewCircle(Context context) {
        super(context);

        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(Circle_list[1], Circle_list[2], Circle_list[3],
                paint);
    }

    public void setCircleList(int[] circles) {
        circle_list = circles;
    }
}

Then in your activity's onCreate method, do the following:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    int Circle_list[] = { Color.BLACK, 20, 20, 50 };
    MyViewCircle view = (MyViewCircle) findViewById(R.id.circle_view);
    view.setCircleList(Circle_list);
}

(For R.id.circle_view, substitute whatever id is appropriate for your circle view as defined in the xml.)

Comments

0

If you make Circle_list a member variable of the outer class, you can access that variable from the inner class.

Your code currently has Circle_list declared inside a method, so it's not accessible outside that method. The code below should show you how to do that. Note that your array syntax is not quite right, it should be of the form "type[] var", not "type var[]"

class MyClass extends Something {

    //Declare variables here
    private int[] Circle_list; //Note the int[], not int var[] as in C/C++

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        int[] Clist = { Color.BLACK, 20, 20, 50 };
        Circle_list = Clist;
    }

    private class MyViewCircle extends View {

        public MyViewCircle(Context context) {
            super(context);

        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.WHITE);
            canvas.drawCircle(Circle_list[1], Circle_list[2], Circle_list[3],
                paint);
        }
    }

2 Comments

@Ted Hopp According to the java documentation, nested classes can have any access specifier: docs.oracle.com/javase/tutorial/java/javaOO/nested.html
I wasn't taking in general about nested classes being private. I know that's fine. I just don't think you can then use them in an XML layout file in Android.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.