-1

I'm trying to create a game but am currently facing an issue with rendering text from Strings.xml to a SurfaceView.

Example Strings:

<string name="somestring">Some String
              SOme more string</string>

<string name="somestring">Some String\nSOme more string</string>

The two above are the attempts I have tried, but it still shows up as single line when drawn on canvas using canvas.drawText(getString(R.string.<resource>));

How can I make it show as two lines, preferably without splitting the Strings?

Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149

3 Answers3

0

Things don't work same while drawing stuff on canvas in android!

drawText() twice with an offset Y-position to simulate a newline!

canvas.drawText("Some String", 100, 100, paint);
canvas.drawText("SOme more string", 100, 150, paint);
johnrao07
  • 6,253
  • 3
  • 29
  • 50
0

¿Can you try this?

<string name="example">"Some string
        hey"</string>

The line break should work because the string is wrapped in quotes.

cutiko
  • 8,436
  • 3
  • 41
  • 53
-1

I'm no expert but it ocurres me that you can use an special char like "-" in your xml file and the replace it with java code with the "\n" char.

String somesTring = getString(R.string.<resource>).replace("-","\n");

Then use it in drawtext

This works if drawtext allows "\n" char.

Regards!

Albert
  • 58
  • 7