简体   繁体   中英

Display ® registered trademark symbol in Android v4.0 WebView?

The source HTML string (including the symbol) is coming from the strings.xml resource file, and is destined to be displayed in a WebView. I've tested with this in the resources:

<string name="MY_STRING">®</string>
  • Using the actual trademark symbol in the resources (®), the projects builds, but when displayed in the WebView it shows as "®" (ie an "A" circumflex, followed by the registered trademark symbol) - ie two characters are shown, the first incorrect & unwanted.
  • I see the same result when using the entity reference, &#174;
  • Using &reg; fails, and the project does not build.

This is the code that pushes the string resource into the WebView:

String html = getString(R.string.MY_STRING);
((WebView)findViewById(R.id.terms_web_view)).loadData(html, "text/html", "UTF-8");

I also tried this, but it did not help: webView.getSettings().setDefaultTextEncodingName("UTF-8");

I pushed the HTML string to Logcat, and it looks fine - it shows the symbol correctly. So if the string is ok, and the WebView is set to use UTF-8, why is the symbol not displaying correctly?

UPDATE I tested on other devices. I can only reproduce this issue on a Galaxy Nexus on Android v4.04. On a Nexus One v2.3.x, Wildfire S on v2.3.x and a Samsung Tab 10.1 on v3.2, it works fine. I've changed the question title to clarify this is an ICS issue.

String resources are not designed to hold arbitrary HTML, including arbitrary entity references.

You might be able to get an arbitrary entity reference to work if you pre-escape it:

<string name="MY_STRING">&amp;reg;</string>

IIRC, that should decode to &reg; after your call to getString() .

At the end of the day, you need to get &reg; to WebView . If you cannot determine a way to do that with a string resource, you will need to store this value someplace else.

You are using loadData(html, "text/html", "UTF-8"); This method expect a html string in your variable html. But it is not. Try

String html = "<html><body>My text is ®</body></html>";

for instance.

--updated to have a full html document

And if you store it in a resource file use :

<string name="MY_STRING" formatted="false"><html><body>My text is ®</body></html></string>

This is not and Android developer answer but just something simple if you're trying to text somebody and neither Ⓡ nor ™ aren't available as symbols (like on my LG phone Android 7.0). Just copy those symbols from this message thread and send them in a message to yourself or stuff them in a quick memo. Then you have them readily available for future use.

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