Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android string.xml reading html tags problem

Tags:

string

android

In Android project's strings.xml file i have following html text

<?xml version="1.0" encoding="utf-8"?>  <resources>  <string name="myHeadStr"><b><u>bold, underline </u></b></string> ...  </resources> 

When i read this into getString(R.string.myHeadStr) it gives only text "bold, underline" it forgets the html tags and ....

how to read complete string with html tags from string.xml

like image 805
d-man Avatar asked Jan 09 '11 05:01

d-man


People also ask

What is@ string in Android studio?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.

Is HTML supported in Android?

text. HTML that processes HTML strings into displayable styled text and then we can set the text in our TextView. We can also use WebView for displaying HTML content. Currently Android does not support all the HTML tags but it supports all major tags.

How to write special characters in string xml in Android?

How can I write character & in the strings. xml? In android studio, you can simply press Alt+Enter and it will convert for you.


2 Answers

Use XML CDATA

<string name="demoStr"><Data><![CDATA[ <b>ABC</b> ]]> </Data></string> 

getString() will be got "<b>ABC</b>"

like image 162
meizilp Avatar answered Oct 18 '22 09:10

meizilp


Replace < with &lt;

<string name="myHeadStr">&lt;b>&lt;u>bold, underline &lt;/u>&lt;/b></string> 

Then, when retrieving:

Html.fromHtml(getResources().getString(R.string.myHeadStr)); 

This is the prescribed way of doing in android documentation. Read the paragraph titled: "Styling with HTML markup" in this link: http://developer.android.com/guide/topics/resources/string-resource.html

like image 42
Sarwar Erfan Avatar answered Oct 18 '22 09:10

Sarwar Erfan