How to align text vertically?

Example to centre on cx and cy: private final Rect textBounds = new Rect(); //don’t new this up in a draw method public void drawTextCentred(Canvas canvas, Paint paint, String text, float cx, float cy){ paint.getTextBounds(text, 0, text.length(), textBounds); canvas.drawText(text, cx – textBounds.exactCenterX(), cy – textBounds.exactCenterY(), paint); } Why doesn’t height()/2f work the same? exactCentre() = … Read more

Android Canvas.drawText

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!!

Android Center text on canvas

Try the following: Paint textPaint = new Paint(); textPaint.setTextAlign(Paint.Align.CENTER); int xPos = (canvas.getWidth() / 2); int yPos = (int) ((canvas.getHeight() / 2) – ((textPaint.descent() + textPaint.ascent()) / 2)) ; //((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center. canvas.drawText(“Hello”, xPos, yPos, textPaint);