Take the 2-minute tour ×

My project actually has quite a few lines of code right now, so I'll save you by only including here what matters. I hope it's enough information to make the issue clear.

I have two main classes: a 'GraphView' class, and then the main Activity's code. Within GraphView, I've created a function which, essentially, draws a rectangle on a canvas. It's called drawPixel. In GraphView's onDraw method, I call drawPixel a bunch of times, and it draws rectangles to the screen. Now, in the main code, I've programmatically created a layout and a button. What I want is that instead of calling drawPixel in GraphView's onDraw method, I want to draw those rectangles when I click a button. I tried this by doing:

someButton.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) {
        Canvas canvas = new Canvas(); 
        // TODO Auto-generated method stub
                someGraphView.drawPixel(canvas, i, i); 

It didn't work. The rectangles draw correctly when i call then via onDraw(), but they don't draw at all when I call them from inside a setOnClickListener method. Does anyone know what I'm doing wrong?

share|improve this question
add comment

1 Answer

Once that's written call invalidate() on your View.

share|improve this answer
add comment

Your Answer


