0

For an app I am developing, I want to graph the results from a pedometer. The pedometer uses a service to measure the amount of steps someone makes in the background. I have a custom view called DrawView, in which I want to draw my results. I call the drawPoint method from my service whenever a step is measured. Then from my drawPoint method I try to call onDraw() using invalidate(). According to my logs, drawPoint is being called but onDraw() is not. So my question is:

Why is onDraw() not being called, and how should I make DrawView call it?


public class DrawView extends View {

...

@Override
public void onDraw(Canvas canvas) {
    for (Point point : points) {
        canvas.drawCircle(point.x, point.y, 5, paint);
        Log.d("Checks", "onDraw is called");
    }
}

public void drawPoint(int counter) {
    Log.d("Checks", "Point coordinates: " + 40 + ", " + counter);

    Point point = new Point();
    point.y = 40;
    point.x = counter;
    points.add(point);
    invalidate();
}
}
Zero
  • 1,864
  • 3
  • 21
  • 39

2 Answers2

1

Instead calling directly view.drawPoint() from service, use handler to update the ui.

Check the link update ui from background service

Sreejith B Naick
  • 1,203
  • 7
  • 13
0

invalidate() must be called from the UI thread. Try calling postInvalidate() instead.

http://developer.android.com/reference/android/view/View.html#invalidate()

TiemenSchut
  • 256
  • 1
  • 5