3

I'm doing an Android Game, and I'm using a function like this to show texts on the device screen:

public void drawString(String text, int x, int y, Paint paint) {
    canvas.drawText(text, x, y, paint);

}

And I try to show the following message:

g.drawString("Player: " + playerString+ " :\n" + messageString,SCREENWIDTH / 2, SCREENHEIGHT / 2, paint);

However instead of a newline (\n) I get a strange character (a square).

Anyone can help me?

Thanks

Kara
  • 6,115
  • 16
  • 50
  • 57
user2204353
  • 195
  • 1
  • 2
  • 10

2 Answers2

5

Instead of drawString call drawText and for break lines call drawText twice with Y offset.

look here for example Draw multi-line text to Canvas

Community
  • 1
  • 1
Elior
  • 3,178
  • 6
  • 37
  • 67
5
public void drawString(Canvas canvas, String text, int x, int y, TextPaint paint) {
   if (text.contains("\n")) {
      String[] texts = text.split("\n");
      for (String txt : texts) {
         canvas.drawText(txt, x, y, paint);
         y += paint.getTextSize();
      }
   } else {
      canvas.drawText(text, x, y, paint);
   }
}
msal
  • 947
  • 1
  • 9
  • 30
Caronte
  • 129
  • 2
  • 2