2

First of all, I have gone through questions similar to the problem I am facing and those solutions are not working for me.

I have a TextView field on my Android app which is supposed to display multiple paragraphs i.e multiple new lines. I am getting this string from a database present in my online server as a JSON.

The text contains \n in it and I am expecting it to create new lines once it is received by the app. But it displays the whole text without any breaks along with "\n" character.

Below is the text present in my database.

First line. \nSecond line. \nThird line.

JSON string received by me inside the app.

{
    "server_response": [{
        "news_expand": "First line. \\nSecond line. \\nThird line."
    }]
}

Code to extract string from JSON. I have left out the code to get get JSONArray and JSONObject for simplicity.

na_expand = gna_jo.getString("news_expand");

String extracted from the JSON. Got this by printing the na_expand string.

First line. \nSecond line. \nThird line.

Code to display the text in the TextView. Note the below 'na_expand' is an SparseArray present in a different activity hence the 'get(position)' code.

art_expand.setText(na_expand.get(position));

Below is the text I get on the emulator.

First line. \nSecond line. \nThird line.

What am I doing wrong here?

user3884753
  • 255
  • 6
  • 16

6 Answers6

1

I think you should replace \n with \n in your string before setting test to your textview same below

b= b.replaceAll("\\n","\n");
zohreh
  • 1,055
  • 1
  • 9
  • 26
1

So I found a workaround to the problem. As I was not sure where the issue was happening with \n, I modified my text present in the database to have a symbol other than \n. For eg: ~

First line.~Second line.~Third line.

You can use a website like this - https://www.gillmeister-software.com/online-tools/text/remove-line-breaks.aspx to replace the line breaks with any symbol you want.

Next, I used the StringSplitter class to break the string received in JSON and then again join it together with \n.

String joined;
String expand_temp = na_expand.get(position);

TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter('~');
splitter.setString(expand_temp);

StringBuilder stringBuilder = new StringBuilder();
    for (String s_temp : splitter) {
        stringBuilder.append(s_temp + "\n");
    }
    joined = stringBuilder.toString().trim();

This worked! I used this string in setText.

art_expand.setText(joined);
user3884753
  • 255
  • 6
  • 16
0

Try below code

myTextView.setText(Html.fromHtml("yourString with additional html tags"));

It will resolve all the html tags accordingly and effects of the tags will be reflected as well.

NOte: For devices greater than Nougat use below code

myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));

Hope that helps

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
0

The \ character is an escape character in JSON. So, when you get \\n, it actually means \n, not the newline character, which should have been just \n. So what you see is an expected behaviour. The JSON you get should have ideally been:

{
    "server_response": [{
        "news_expand": "First line. \nSecond line. \nThird line."
    }]
}

Get your server to respond properly, otherwise you'll have to strip the unnecessary \.

Rajesh
  • 15,724
  • 7
  • 46
  • 95
  • Any suggestion on what I should do on the server side? I am using a php file to encode my json after running a query on my database. Or should I change the structure of the line present (first line in the question) in my database? – user3884753 Dec 12 '17 at 15:12
  • I see that you handled it in the client side using string replacement. Ideally, you should have inserted actual newline characters in database (\n is just a representation, not the newline character itself). Then your PHP script would have handled it correctly. – Rajesh Dec 13 '17 at 03:57
0

Do you haveandroid:singleLine="true" on your TextView? If yes it will ignore the \n and will place the text in a single line.

Drastaro
  • 11
  • 2
0

You can just add replaceAll("\\n","\n") when you set value to your art_expand EditText. It should be:

art_expand.setText(na_expand.get(position).replaceAll("\\n","\n"));
unedim
  • 91
  • 8