Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update Android Views upon modifications?

I have some methods in my View that modify some of the shapes that are drawn when called. In Java in order to make sure the component is updated I would call repaint(). Is there something that will make sure my view is updated correctly?

I had read somewhere that calling invalidate() in the onDraw() method would keep things up to date and therefore I wouldn't need to have something like repaint() in my methods that modify that shapes that are drawn.

Is this correct, or is there something else I have to do?

EDIT

To add in an example, a method I call in my view is:

public void setLineThickness(int thickness) {
    aLineThickness = thickness;

    if(aLineThicness > 1)
        //repaint();      - Okay in Java but not in Android

}
like image 619
StartingGroovy Avatar asked Jul 20 '11 00:07

StartingGroovy


1 Answers

Calling invalidate() will tell the view it needs to redraw itself (call onDraw) sometime in the future. So if you change something in your view, like the line thickness, call invalidate() after it. That way you know your view will eventually be updated.

All your drawing code should be implemented in onDraw() and your other methods should just change the state of your view, which will then be used to draw it, after you call invalidate().

like image 83
Thomas Dignan Avatar answered Sep 21 '22 03:09

Thomas Dignan