0

Im making a simple network app and want to load the chat log on start up, this works fine but it formats the text as one line

Writing to file

CharSequence cs = tv.getText ();
final String str = cs + "\r\n" + s;
//Write to text file
try {
FileOutputStream fos = openFileOutput("Chat Log", Context.MODE_APPEND);
fos.write(s.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}



}
catch (IOException e1)
{

e1.printStackTrace();
}
//close the socket
socket.close(); 

Reading Text

final TextView tv = (TextView)findViewById(R.id.textView1);
try {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(
openFileInput("Chat Log")));
String inputString;
StringBuffer stringBuffer = new StringBuffer();
while ((inputString = inputReader.readLine()) != null) {
stringBuffer.append(inputString + "\r\n");
tv.append(inputString);
}

} catch (IOException e) {
e.printStackTrace();
}

Current Result Screenshot

1 Answers1

0

You should be able to save formatting using Html.toHtml and Html.fromHtml.

Something along the lines of:

String htmlFormatted = Html.toHtml(tv.getText());
// .. save to file ..

Then:

Spanned htmlFormatted = Html.fromHtml(chatLog);
tv.setText(htmlFormatted);

See these similar questions:

Community
  • 1
  • 1
silverjam
  • 400
  • 1
  • 7