15

There is a String in Android Java. How do I change it to another given encoding and replace HTML-entities such as & with &?

This is so that international symbols can be displayed correctly.

2 Answers 2

48

to decode Html String you can use Html.fromHtml()

like

Html.fromHtml((String) htmlCode).toString();

if you want reverse

than you can use TextUtils.htmlEncode()

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Replacing HTML entities works. About encoding yet have no tries.
this is not working any more, in googles documentations it is noted that this method is deprecated in API level 24.
Docs also state to use fromHtml(String source, int flags) instead.
1

You can add denpendency:

implementation group: 'org.apache.commons', name: 'commons-text', version: '1.3'

And use StringEscapeUtils.unescapeHtml4(s), then get the result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.