android string.xml reading html tags problem
Use XML CDATA
<string name="demoStr"><Data><![CDATA[ <b>ABC</b> ]]> </Data></string>
getString() will be got "<b>ABC</b>"
HTML tags not working within strings.xml
Use getText(int)
instead of getString(int)
.
Why will the simple HTML tags not work yet this other method works just fine?
Because the getString(int)
method will return a string, that is stripped of styled text information, like the docs say.
As per the Android developer guidelines the getText(int)
method does not:
getText(int)
will retain any rich text styling applied to the string.
The <string name="name"><b>bold text here</b></string>
solution with getString()
works because you've encoded the less than sign.
Android formatting xml string loses its html tags
use Html.fromHtml
builder.setMessage(Html.fromHtml(text));
when you apply the formatting, the CharSequence
is converted back to String
, and you need the Spannable
with the html information.
From the doc:
Sometimes you may want to create a styled text resource that is also
used as a format string. Normally, this won't work because the
String.format(String, Object...) method will strip all the style
information from the string. The work-around to this is to write the
HTML tags with escaped entities, which are then recovered with
fromHtml(String), after the formatting takes place.
try with <b>
in place of <b>
and with </b>
in place </b>
Android string with HTML failed
You have to wrap the string content into <Data></Data>
<string name="my_string"><Data><![CDATA[<p>This is a paragraph!</p>]]> </Data></string>
android string.xml reading html tags problem
Android: getString() from resources loses any tags in the string
Why not try to replace '<', '>' and '\' by the corresponding unicode characters?
Regards.
Html format tags ignored in Dialog
You could format with HTML by using a WebView in the dialog:
strings.xml
<string name="example_text" formatted ="false"><![CDATA[ <strong> Example Text </strong> ]]></string>
java
String string = getString(R.string.example_text);
WebView wv = new WebView (getBaseContext());
wv.loadData(string, "text/html", "utf-8");
wv.setBackgroundColor(Color.WHITE);
wv.getSettings().setDefaultTextEncodingName("utf-8");
new AlertDialog.Builder(this)
.setCancelable(false)
.setView(wv)
.setNeutralButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.show();
Related Topics
Android Sdk: Get Raw Preview Camera Image Without Displaying It
Change Viewpager Animation Duration When Sliding Programmatically
Setexactandallowwhileidle - Is Not Exact as of Developer Reference
Mediastore - Uri to Query All Types of Files (Media and Non-Media)
How to Change Viewpager's Page
How to Add a Button to a Preferencescreen
How to Show Snackbar at Top of the Screen
Encoding H.264 from Camera with Android Mediacodec
How to Request Root Access in Android
How to Fix "Failed to Sync Vcpu Reg" Error
Interstitial Admob Ads: "Illegalstateexception: Only Fullscreen Activities Can Request Orientation"
Android: Create Circular Image with Picasso
What Is the Default Font Family in Android
How to Create Topic in Fcm Notifications