2

I have trouble with newline in textview (i read text from assets txt file). See screenshot:

enter image description here

My code:

 TextView txt = (TextView) findViewById(R.id.txt);

    AssetManager assetManager = getAssets();

    InputStream input;
    try {
        input = assetManager.open("book.txt");

         int size = input.available();
         byte[] buffer = new byte[size];
         input.read(buffer);
         input.close();

         String text = new String(buffer);
         txt.setText(text);
    } catch (IOException e) {

    }

Some lines of book.txt:

Мим должен молчать Автор pinskiy

--Мама-мама! Смотри! Грустный клоун! – бесцеремонно показывала на него пальцем девочка лет восьми.

-- Маш, это не клоун – это мим, - не сбавляла скорости мама, пытаясь успеть на трамвай. Девочка, не успевая за мамой, болталась у нее на руке, но продолжала гнуть свою линию: «А почему клоун грустный, мама?» - «Спроси у него сама».

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • Maybe make a few lines of the book.txt available online so we can see what is in there. Each new line may be a Carraige Return + Line Feed (Windows style) when only the Line Feed is necessary. The funny character you're seeing may be the unneeded Carraige Return (character 0x0D or 13 in decimal). – MikeHelland Jan 19 '14 at 09:49
  • In Notepad, when you do **File-Save As** on the bottom you can set the encoding to to **UTF-8**. If you already saved it as UTF-8, it is something else. Android uses UTF-8 as the default. – Rick Falck Jan 19 '14 at 09:56
  • Looks like a case of `\r\n` newlines. You only need the `\n`. Posting the file as text however doesn't reveal the actual characters in book.txt. – laalto Jan 19 '14 at 10:17
  • have u tried my soln provided in this que?? or u have solved it at your own?. – Manmohan Badaya Jan 21 '14 at 11:40

2 Answers2

0

try it once. may it help u.

txt.setText(Html.fromHtml(text));

or u can replace that character with "\n" and now can use this again if that not worked.

Manmohan Badaya
  • 2,326
  • 1
  • 22
  • 35
0

Thank you all! I have converted my file from txt to html, and now my code looks like:

 AssetManager assetManager = getAssets();
     InputStream inputStream = assetManager.open("book.html");
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
     int i;
     try {
     i = inputStream.read();
      while (i != -1)
      {
       byteArrayOutputStream.write(i);
       i = inputStream.read();
      }
      inputStream.close();
      } catch (IOException e) {

     }

      TextView txt = (TextView) findViewById(R.id.txt);   
      txt.setText(Html.fromHtml(byteArrayOutputStream.toString()));