简体   繁体   中英

Why doesn't Html.fromHtml not working for String in Android?

在此处输入图像描述

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

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");

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/>")));

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM