Reputation: 6778
I am unable to get text in an AlertDialog to display in bold or italic.
I've tried formatting directly and also using the CharSequence utilties from the Android string-resource page but I always get just plain text.
What am I doing wrong?
MyFragment.java
String idFormatted = String.format(resources.getString(R.string.id));
CharSequence idStyledText = Html.fromHtml(idFormatted);
CharSequence nameItalic = Utilities.italic(resources.getString(R.string.name));
String typeFormatted = String.format(resources.getString(R.string.type));
CharSequence typeStyledText = Html.fromHtml(idFormatted);
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setPositiveButton("Dismiss",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setIcon(android.R.drawable.ic_dialog_info);
builder.setMessage( idFormatted + myData.getId() + "\t\t" + nameItalic + myData.getName() + "\t\t" + typeFormatted + myData.getType() );
Dialog dialog = builder.create();
dialog.setTitle("Details");
dialog.show();
strings.xml
<string name="id"><i>Id: </i></string>
<string name="name"><i>Name: </i></string>
<string name="type"><i>Type: </i></string>
Edit: -------------------------------------------------------------
Mohammad Arman suggests that the strings from the string.xml will not retain the html. This seems likely to be true, though I can't prove it yet.
Based on his and Farbod Salamat-Zadeh's answer, I tried the code below, but still the result is the same, just plain text.
Spanned idLabel = Html.fromHtml("<i>" + resources.getString(R.string.id) + "</i>");
Spanned nameLabel = Html.fromHtml("<i>" + resources.getString(R.string.name) + "</i>");
Spanned typeLabel = Html.fromHtml("<i>" + resources.getString(R.string.type) + "</i>");
...
builder.setMessage(idLabel.toString() + myData.getId() + "\t\t" + nameLabel + myData.getName());
Edit2: -----------------------------------------------------------
I've tried as Keelan has suggested:
<string name="italic_string"><![CDATA[<i>italic-string</i>]]></string>
and then:
String formattedText = getString(R.string.italic_string);
Spanned result = Html.fromHtml(formattedText);
builder.setMessage(result);
This works.
Upvotes: 7
Views: 3819
Reputation:
You can't simply store HTML in a strings.xml file. You need to put them in CDATA, like so:
<string name="id"><![CDATA[<i>Id: </i>]]></string>
Then to display a formatted string, you still need to use Html.fromHtml()
.
Upvotes: 6
Reputation: 7065
You need to format the html version in a separate String
variable in Java class (otherwise for some reason it won't work).
String myId = <i>Id: </i>;
String myName = <b>Name: </b>
Then you have to add this in the textView like bellow:
textView1.setText(Html.fromHtml(myId +" "+ "1234");
textView2.setText(Html.fromHtml(myName +" "+ "Al Lelopath")
Why your approach is not working
It's because as you are importing the string from string.xml it will lose the attribute and send it as a plain text. And that's why Android could not take that as html.
Upvotes: 1
Reputation: 20080
You can get text anywhere to display in bold or italic - I use the following;
Spanned italicText = Html.fromHtml("<i>" + "My italic text" + "</i>");
Spanned boldText = Html.fromHtml("<b>" + "My boldtext" + "</b>");
// You can use different HTML tags depending on what you want.
Upvotes: 2