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?

0

3 Answers 3

2

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

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

That will output:

aaaa
bbb
ccc

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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?
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.
I used this example for getting the strings I need.
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: stackoverflow.com/questions/5613535/… .
2

Use HTML tags:

myTv.setText(Html.fromHtml("<p>aaaa<br/>bbbb<br/>cccc</p>")); 

1 Comment

This is actually a good alternative if you are having problems with other methods.
1

Try like this:

setText("aaaa\nbbb\nccc"); 

2 Comments

This will not work. This will output "aaaa \nbbb \nccc" on the screen because you're escaping the "\n" by adding backslashes before it.
sorry.I understood the reverse one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.