Dev01
Dev01

Reputation: 4222

Android: New line character from mysql does not work in textview

I am saving some text in mysql db (longtext type) which contains new line character something like:

Hello World
This is a test
Thanks

I am sending value in db via php like this:

$message = 'Hello World' . "\n";
$message .= 'This is a test' . "\n";
$message .= 'Thanks;

I can see that new line character is saved in db and it shows fine.

However in TextView I see all text joined, there is no new line character. This is how TextView shows it:

Hello WorldThis is a testThanks

What I have tried:

FYI, if i type same message containing new line in android application itself then TextView shows newline characters correctly but it is just not getting new line character coming from mysql db :(

Thanks for the help

Upvotes: 0

Views: 1034

Answers (2)

Rohit5k2
Rohit5k2

Reputation: 18112

Replace \n with "html br tag" and

use this method yourTextView.setText(Html.fromHtml(text));

Upvotes: 0

petey
petey

Reputation: 17140

use

msg.replaceAll("\n","<br />");

then on the text view use

myTextView.setText(Html.fromHtml(msg));

Upvotes: 2

Related Questions