0

ok so I have my array, im calling my array, but it doesn't update the background of the canvas? what am I doing wrong? perhaps some tutorial links would be helpful here. Thanks.

             int[] myImageList = {
            R.drawable.lettersa, R.drawable.lettersb,
            R.drawable.lettersc, R.drawable.lettersd,
            R.drawable.letterse, R.drawable.lettersf, R.drawable.lettersg,
            R.drawable.lettersh, R.drawable.lettersi, R.drawable.lettersj,
            R.drawable.lettersk, R.drawable.lettersl, R.drawable.lettersm,
            R.drawable.lettersn, R.drawable.letterso, R.drawable.lettersp,
            R.drawable.lettersq, R.drawable.letterss, R.drawable.letterst,
            R.drawable.lettersu, R.drawable.lettersv, R.drawable.lettersw,
            R.drawable.lettersx, R.drawable.lettersy, R.drawable.lettersz};


                 mNextBtn.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        int i;
                        if (mNextBtn.equals(mNextBtn)) {

                            for   (i=0;i<myImageList.length;i++)
                            {
                                         mSCanvas.setBackgroundResource(i);
                            }

                            return;
                        }   

                        mNextBtn.setEnabled(mSCanvas.isUndoable());
                        }

                });
3
  • Basically I want the canvas background to change to the next letter to trace upon click of the button Commented Mar 26, 2013 at 14:30
  • I really did not get your question, after this code what is happening now and why you have an if condition inside click handler if (mNextBtn.equals(mNextBtn)) like this ? Commented Mar 26, 2013 at 14:34
  • Also you need to call invalidate() at the view that you draw the canvas in. This forces the view to redraw with the changed canvas. Commented Mar 26, 2013 at 14:36

1 Answer 1

1

Basically I want the canvas background to change to the next letter to trace upon click of the button

I recommend a different approach:

mNextBtn.setOnClickListener(new OnClickListener() {
    int i = 0;
    public void onClick(View v) {
        mSCanvas.setBackgroundResource(myImageList[i]);
        i++;
        if(i >= myImageList.length)
             i = 0;

        // I'm not sure what you want to do here, so I left it:
        mNextBtn.setEnabled(mSCanvas.isUndoable());
        return;
    }
});

I changed three main points:

  1. Only set one background resource at a time, the loop sets every background resource in a few milliseconds and always leaves you with the last value.
  2. The drawable resource is myImageList[i] not i in mSCanvas.setBackgroundResource()
  3. I don't know of any situation when this mNextBtn.equals(mNextBtn) is false...
Sign up to request clarification or add additional context in comments.

2 Comments

worked like a charm thanks. and I wanted to leave the button unclickable unless something was drawn.
it worked, but then I had a problem with the resource not changing because the background was already 0, so I changed int i = 1 and it works and loops through correctly. Thanks for the awesome help

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.