Question

Hi all: I'm writing a class that inherit from TextView, and override its onDraw() method, but in the method, my invoke of canvas.drawText() doesn't seems to work, the code just like below:

protected void onDraw(Canvas canvas) {
    // super.onDraw(canvas);
    Paint paint = new Paint();
    paint.setColor(android.graphics.Color.WHITE);
    paint.setTextSize(20);

    String text = "hello";
    canvas.drawText(text, 0, 0, paint);
}
Was it helpful?

Solution

It isn't drawing anything because the text coordinates are bottom left. Since you're trying to draw on 0,0, it will draw above the screen.

Try changing the last line to:

canvas.drawText(text, 0, 20, paint);

OTHER TIPS

Excellent suggestions all around, great job guys really. Next time though it would be nice if you ask the guy in a comment or something whether or not he's tried the completely obvious before posting it as an answer. Do you really think that the second he got to a point that wasn't working he just came straight to Stack Overflow without experimenting?

I do have an alternate suggestion, that crazily enough is based on the entire question and not just the part that could be answered without much actual knowledge.

I would recommend trying your drawText call on a Canvas that's not in a TextView subclass as that way it won't be overridden by the several hundred lines of code in TextView that manage it's drawable state.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top