-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
  • 27,060
  • 21
  • 118
  • 148

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,690
  • 4
  • 32
  • 55
  • Yes, the \n should work ok on Android check [this](http://stackoverflow.com/questions/5460256/string-resource-new-line-n-not-possible) – 4gus71n Jul 20 '16 at 16:39
  • \n works with raw code, but it doesn't appear to work with xml files – Zoe Jul 20 '16 at 18:13
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
  • 9,887
  • 3
  • 45
  • 59
-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