WISHY
WISHY

Reputation: 11999

Add next line to string to be displayed in text view, android?

I have string value which comes from server in XML format.

For eg

<rss version="2.0" xmlns:Data="http://www.google.com">
<Data: Mesage="Dear Member,\\nWe wish you and your family a Very Happy New Year 2015./>
</rss?

Where \\n represents a new line. When I add "\n" to the string on server it becomes "\\n".

And it is displayed in textview as

Dear Member,\\nWe wish you and your family a Very Happy New Year 2015.

I am setting the text as

textview.setText(data_message.replaceAll("\\n", "\n"));
textview.setText(data_message.replaceAll("\\n",System.getProperty("line.separator");

I have tried both. But next line does'nt appears.

How do I add next line.

@almas

On using your code. The output I get is

Dear Member,\
We wish you and your family a Very Happy New Year 2015.

Upvotes: 1

Views: 765

Answers (3)

Elango
Elango

Reputation: 411

you can try this,

  String text = "Dear Member,\\nWe wish you and your family a Very Happy New Year 2015.";
  textView.setText(text.replaceAll("\\\n", "/n"));

or

  String text = "Dear Member,\\nWe wish you and your family a Very Happy New Year 2015.";
  textView.setText(text.replaceAll("\\\n",System.getProperty("line.separator"));

Upvotes: 1

Vasilov Artur
Vasilov Artur

Reputation: 1487

The problem is in the first param in replaceAll method, which also needs escaping. This construction is ugly, but works correct:

    String text = "Dear Member,\\nWe wish you and your family a Very Happy New Year 2015.";
    textView.setText(text.replaceAll("\\\\n", "\n"));

Upvotes: 1

SMA
SMA

Reputation: 37023

Try using replace api as you are not using regex with your pattern as below:

String data_message = "Dear Member,\\nWe wish you and your family a Very Happy New Year 2015.";
System.out.println(data_message.replace("\\n", System.getProperty("line.separator")));
Output:
Dear Member,
We wish you and your family a Very Happy New Year 2015.

Upvotes: 0

Related Questions