0

I want to set text for a textView by code. My text is something like this :" aaaa \n bbb\n ccc" and if I use setText() method on screen I got exact in this format, and not like this :

"aaaa
bbb
ccc".

If I put in xml android:text="aaaa \n bbb\n ccc" it works fine. I need to set this by code,and not from xml file. How can I do this?

Gabrielle
  • 4,933
  • 13
  • 62
  • 122

3 Answers3

2

Use HTML tags:

myTv.setText(Html.fromHtml("<p>aaaa<br/>bbbb<br/>cccc</p>"));
MByD
  • 135,866
  • 28
  • 264
  • 277
2

Simply use the newline character "\n", like this:

myTextView.setText("aaaa\nbbb\nccc");

That will output:

aaaa
bbb
ccc

Jacob Ras
  • 5,974
  • 1
  • 28
  • 26
  • the strange problem is that I have the string from a link and I can not acces him. I mean I have String data=(String) sh.userList.get("title"); and if I put setText(data) on screen I get all the text, including "\n".Why? – Gabrielle Aug 23 '11 at 12:29
  • Debug your code. I would suggest adding a breakpoint on the line where you do setText(data). If you then run your app in debug mode (F11 if you're using Eclipse), it will pause on that line. Then you can mouseover data to see what exactly is in there. What exactly is userList by the way? – Jacob Ras Aug 23 '11 at 12:39
  • well,it's a little complicate. I use SAX for getting information from a xml. What is really strange is that if I write : String data="aaaa\nbbb\nccc" and setText(data) everything is ok. But now, I have String data=(String) sh.userList.get("title") which is the same trinh like before, but if I put setText(data) is not ok. – Gabrielle Aug 23 '11 at 12:46
  • I used this example for getting the strings I need. – Gabrielle Aug 23 '11 at 12:47
  • You didn't mention you're using SAX ;). Are you creating the xml files yourself? Because if so, you're not adding newlines the right way. Take a look at this StackOverflow question: http://stackoverflow.com/questions/5613535/android-sax-parser-with-newline-character-sample . – Jacob Ras Aug 23 '11 at 13:21
1

Try like this:

setText("aaaa\nbbb\nccc");
jainal
  • 2,973
  • 2
  • 20
  • 18