1

i have a string that will get picked up by an intentExtra in my next class and be set as the text for a multiline TextView. i was wodnering if there is a way to split the text up so they are under eachother, so i have this string:

maint = "Here is some info about nothing." +
                "Some more info about nothing." +
                "And a little more about nothing";

so usually they would be displayed in a textView like this:

Here is some info about nothing. Some more info about nothing. And a little more info about nothing.

is there a way so they would end the line after each one of those separate pieces were written into the TextView

like so:

Here is some info about nothing.
Some more info about nothing.
And a little more about nothing.

it just seems like it would be too much with a separate TextView for each of those separate sentences.

thanks in advance

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
Nicholas Pesa
  • 2,156
  • 2
  • 24
  • 45

2 Answers2

3

Add a /n to where you want to have a newline.

maint = "Here is some info about nothing.\n" +
        "Some more info about nothing.\n" +
        "And a little more about nothing";

You could also format it using Html. Add a where you want a newline.

maint = "Here is some info about nothing.</br>" +
        "Some more info about nothing.</br>" +
        "And a little more about nothing";

If you format it with HTML, you need to parse it first before passing it to the textview:

myTextView.setText(Html.fromHtml(maint));
onit
  • 6,306
  • 3
  • 24
  • 31
  • ok sweet, thanks, so if i just use the '\n' i dont have to parse? – Nicholas Pesa Feb 09 '12 at 18:52
  • @NickPesa Yes you do not need to parse if you just use '\n'. For your reference, '\n' is the newline character in Java. If you want more complicated text formatting, such as differently colored words, you can use HTML parsing. – onit Feb 09 '12 at 18:53
  • yes i remember using it in c++ ive done before, but i was not sure if java used it as well – Nicholas Pesa Feb 09 '12 at 18:54
  • @NickPesa np. I would appreciate an accepted answer if it solved your problem though. – onit Feb 09 '12 at 18:54
  • i did it, it wouldnt let me at first, it told me i had to wait to accept, but i got it now – Nicholas Pesa Feb 09 '12 at 19:10
1

This should do what you want ("\n" being a newline character)

maint = "Here is some info about nothing.\n" +
                "Some more info about nothing.\n" +
                "And a little more about nothing\n";
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294