If your minSdkVersion
is 24 or higher, use the version of fromHtml()
that takes some flags as a parameter. AFAIK, FROM_HTML_MODE_LEGACY
would be the flag value to use for compatibility with the older flag-less fromHtml()
.
If your minSdkVersion
is lower than 24, your choices are:
Always use the fromHtml()
you are, possibly using the quick-fix (Alt-Enter) to suppress the Lint warning
Use both versions of fromHtml()
: the one taking the flags if your app is running on an API Level 24+ device, or the one without the flags on older devices
The latter would look like:
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N) {
actionBar.setTitle(Html.fromHtml(..., Html.FROM_HTML_MODE_LEGACY));
}
else {
actionBar.setTitle(Html.fromHtml(...));
}
(where ...
is your HTML to convert)
Note, though, that if you are simply trying to change the color of the entire action bar title, use Sandro Machado's solution. Html.fromHtml()
and similar Spanned
-based solutions are for cases where you need different colors for different pieces of text within a single TextView
(or something using a TextView
, such as the action bar).