Im trying to add multiple html foreground formattings of a snackbar text programmatically.
In my strings.xml:
<string name="html_test">The html entries %1$s and %2$s are looking different.</string>
How i try to format them:
public static Spanned getString(Context p_Context, int p_iResID, int p_iColor, String... p_Items) {
int l_iColor = ContextCompat.getColor(p_Context, p_iColor);
String l_HexColor = Integer.toHexString(l_iColor);
String l_Before = "<font color=" + l_HexColor + ">";
String l_After = "</font>";
Object[] l_Items = new String[p_Items.length];
for(int i = 0; i < p_Items.length; i++) {
l_Items[i] = l_Before + p_Items[i] + l_After;
}
return Html.fromHtml(p_Context.getString(p_iResID, l_Items));
}
How i call the function:
getString(getContext(), R.string.html_test, R.color.blue, "Test1", "Test2");
Then i create a snackbar and pass the html formated text.
Snackbar l_SnackBar = Snackbar.make(p_Root, p_Text, p_iSnackBarLenght);
l_SnackBar.getView().setBackgroundColor(p_iBGColor);
return l_SnackBar;
The problem is that there is no html formatting of the two entries i passed into getString()
.
I don't want to use ![CDATA...
because i read that there are some problems with the formatting.