Highlighting Text Color using Html.fromHtml() in Android?
Or far simpler than dealing with Spannable
s manually, since you didn't say that you want the background highlighted, just the text:
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
Android - Html.fromHtml handle background color
Add your own BackgroundColorSpan
as you see fit.
Here is some code that sets such a span on all occurrences of a search term within a TextView
:
private void searchFor(String text) {
TextView prose=(TextView)findViewById(R.id.prose);
Spannable raw=new SpannableString(prose.getText());
BackgroundColorSpan[] spans=raw.getSpans(0,
raw.length(),
BackgroundColorSpan.class);
for (BackgroundColorSpan span : spans) {
raw.removeSpan(span);
}
int index=TextUtils.indexOf(raw, text);
while (index >= 0) {
raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
+ text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
index=TextUtils.indexOf(raw, text, index + text.length());
}
prose.setText(raw);
}
So, find your beginning and ending points, create a BackgroundSpan
with your desired color, and use setSpan()
to apply it.
Note that this assumes that only part of your text needs the background color. If the entire TextView
needs the color, go with njzk2's suggestion, and just apply the color to the whole TextView
.
Highlight the textcolor in android html.fromHtml
Here's a list of html tags supported by textview
http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html
For using SpannableString
http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring
Example:
Using Spannable String
_tv.setText("");
SpannableString ss1= new SpannableString(s);
ss1.setSpan(new StyleSpan(Typeface.BOLD), 0, 23, 0);
ss1.setSpan(new StyleSpan(Typeface.ITALIC), 0, 23, 0);
ss1.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 23, 0);
_tv.setText(ss1);
Using Html
String text = "<font color=#cc0029>hello</font> <font color=#ffcc00>world</font>";
yourtextview.setText(Html.fromHtml(text));
Using multiple text colors in Android's textview [ Html.fromhtml() ]
textview.setText(Html.fromHtml("<i><small><font color=\"#c5c5c5\">" + "Competitor ID: " + "</font></small></i>" + "<font color=\"#47a842\">" + compID + "</font>"));
Try the above.
Change color and make bold part of a TextView in xml
<b>
Log In</b>
will make it. The string resource is to be as follows:
<string name="already_have_an_account">Already have an account? <font color='#FF6200EE'><b>Log In</b></font></string>
Here is more on Styling with HTML markup.
Related Topics
Camera Intent Not Working with Samsung Galaxy S3
Scan Android Sd Card for New Files
Android Studio: Gradle: Error: Cannot Find Symbol Variable
Most Memory Efficient Way to Resize Bitmaps on Android
View the Task's Activity Stack
What Is the Relationship Between Looper, Handler and Messagequeue in Android
Change Keyboard Input Language
How to Ask Runtime Permissions for Camera in Android , Runtime Storage Permissions
How to Pause Frame Animation Using Animationdrawable
Stuck with Getting Camera Pic When Using the Tab Activity
Value of a Global Variable Is Reset After It Is Initialised in Valueeventlistener
No Adapter Attached; Skipping Layout
How to Programmatically Get the List of Registered Users in Firebase
Should One Create a Bond with a Bluetooth Le Device
Exporting SQLite Database to CSV File in Android
Android - Emulator in Landscape Mode, Screen Does Not Rotate