Search code examples
androidandroid-canvasdrawtext

Android Canvas.drawText


I have a view, I'm drawing with the Canvas object in the onDraw(Canvas canvas) method. My code is:

Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawPaint(paint);

paint.setColor(android.R.color.black);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);

The problem is the text doesn't show through the background, what am I doing wrong? If I remove the canvas.drawPaint(paint) and paint.setColor(android.R.color.black) you can see the text on the screen.....


Solution

  • Worked this out, turns out that android.R.color.black is not the same as Color.BLACK. Changed the code to:

    Paint paint = new Paint(); 
    paint.setColor(Color.WHITE); 
    paint.setStyle(Style.FILL); 
    canvas.drawPaint(paint); 
    
    paint.setColor(Color.BLACK); 
    paint.setTextSize(20); 
    canvas.drawText("Some Text", 10, 25, paint); 
    

    and it all works fine now!!