-1

enter image description here

One String value is coming from my firebase:

if (task.isSuccessful()) {
    //   binding.contentMain.noData.setVisibility(View.GONE);
    for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult()))
        postMap.putAll(document.getData());

    try {
        if (postMap != null)
            //binding.desc1.setText(Objects.requireNonNull(postMap.get(CONTENT)).toString());
            binding.desc1.setText(Html.fromHtml(Objects.requireNonNull(postMap.get(CONTENT)).toString()));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Now, I'll tell you what is coming inside String from Firestore, Actually I only manually written as a String field inside Firestore collection.:

This is the first text. \n\n This is a second text. \n This is the third text. \n\n Done. \n

Make sure that above line is just one String/firestore field.

In firestore, I stored the above value as a String, and fetching that value and showing it in Android. But it is not taking a new line. Instead, it is showing written \n along with other characters.

I tried with and without HTML. with \n and \r\n.

UPDATE: You can see below image how I stored in Firestore. https://drive.google.com/open?id=19yry9top_W8LREw5SEaZKWYWSlnCP1io

  • 2
    This is happening because there is `\\n` instead of `\n`. You need to unescape it: `s.replaceAll("\\\\n", "\\\n");` or if you can just import apache commons then there is a static method `String op = StringEscapeUtils.unescapeJava(s);` – Aniket Sahrawat Jun 02 '20 at 04:33
  • @AniketSahrawat What exactly I need to write in Firesotore field? –  Jun 02 '20 at 06:15
  • 2
    See https://stackoverflow.com/a/48874269/6099347 and https://stackoverflow.com/questions/2840608/how-do-i-add-a-newline-to-a-textview-in-android – Aniket Sahrawat Jun 02 '20 at 06:30
  • @AniketSahrawat In my case, it isn't replacing with \n to \\n ; it I exactly coming what I written manually in collection field. –  Jun 02 '20 at 06:43
  • @AniketSahrawat Check the edited question. –  Jun 02 '20 at 07:18
  • @AniketSahrawat Check the image in question –  Jun 02 '20 at 07:32
  • @ShefaliSingh you want to show those lines in new lines? – OhhhThatVarun Jun 02 '20 at 08:27
  • @OhhhThatVarun yes. I want to show lines as a next lines. –  Jun 02 '20 at 12:40

3 Answers3

0

In firestore you need to put two \s before n to add a new line.

if you are getting data from user or reading it from a file add this line before storing data:

str.replaceAll("\\n", "\n");
Majid Roustaei
  • 1,556
  • 1
  • 20
  • 39
0

use this method:

  public Spanned getHtmlFormattedString(String value) {
        Spanned result = null;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                result = Html.fromHtml(value, Html.FROM_HTML_MODE_COMPACT);
            } else {
                result = Html.fromHtml(value);
            }

        } catch (Exception ex) {
        }
        return result;
    }

in your case :

binding.desc1.setText(getHtmlFormattedString(Objects.requireNonNull(postMap.get(CONTENT)).toString()));

Update Answer

\n is not html tag , if you want to handle with Html.fromHtml you should replace \n with <br/> tag

so in your case :

binding.desc1.setText(getHtmlFormattedString(Objects.requireNonNull(postMap.get(CONTENT)).toString().replaceAll("\\n","<br/>")));
Nickan
  • 466
  • 5
  • 17
0

Your string is

This is the first text. \n\n This is a second text. \n This is the third text. \n\n Done. \n

This is quite weird.

1) Do you want next line after a full stop?

2) Can you edit or format the string at the time of saving?

If your case is 1 then what you can do is

Remove all the /n character and replace with "".

String stringWithoutNewLine = yourString.replaceAll("\n","")

Then it gonna look like this

This is the first text.  This is a second text.  This is the third text.  Done. 

Now remove all the ". " in the string and replace it with new line.

String formattedString = yourString.replaceAll(".
",System.lineSeparator())

You can chain these in one line and it will work because replaceAll returns a new String

Can you try this

if (task.isSuccessful()) {
    //   binding.contentMain.noData.setVisibility(View.GONE);
    for (QueryDocumentSnapshot document: Objects.requireNonNull(task.getResult()))
        postMap.putAll(document.getData());

    try {
        if (postMap != null)
            //binding.desc1.setText(Objects.requireNonNull(postMap.get(CONTENT)).toString());
            String string = postMap.get(CONTENT).toString();
            String finalString = string.replaceAll("\n","").replaceAll(".  ",System.lineSeparator())
            binding.desc1.setText(finalString)
    } catch (Exception e) {
        e.printStackTrace();
    }
}
OhhhThatVarun
  • 3,981
  • 2
  • 26
  • 49
  • Whatever I written in firestore, exactly that is coming. See question, what I written. Ans see image inside question what is coming. –  Jun 02 '20 at 12:43
  • https://drive.google.com/open?id=19yry9top_W8LREw5SEaZKWYWSlnCP1io –  Jun 02 '20 at 13:17
  • I have own project in Firebase. Whatever I want I can write in Firesotore DB manually. And I don't want new line after every full stop. It depends upon some articles. After paragraph I want/added new lines, so that, in android automatically space will come. And In question I gave just example. –  Jun 02 '20 at 17:19
  • @ShefaliSingh I would suggest you is to format the string before saving it to the firebase then you can successfully format the string. Otherwise, you are gonna have hard time formatting strings. – OhhhThatVarun Jun 02 '20 at 17:38