Highlight text color with Html.fromHtml () in Android?

I am developing an application in which there will be a search screen where the user can search for specific keywords, and this keyword should be highlighted. I found the Html.fromHtml method.

But I would like to know if this is being done correctly or not.

Please let me know your views on this.

+74
android text textview highlight
Apr 28 '10 at 15:09
source share
8 answers

Or much easier than dealing with Spannable manually, since you did not say that you want to highlight the background, just text:

 String styledText = "This is <font color='red'>simple</font>."; textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE); 
+180
May 30 '10 at 10:14
source share

Using the color value from an xml resource:

 int labelColor = getResources().getColor(R.color.label_color); String olorString = String.format("%X", labelColor).substring(2); // !!strip alpha value!! Html.fromHtml(String.format("<font color=\"#%s\">text</font>", olorString), TextView.BufferType.SPANNABLE); 
+34
Feb 20 '13 at 18:04
source share

This can be achieved using a Spannable String. You will need to import the following

 import android.text.SpannableString; import android.text.style.BackgroundColorSpan; import android.text.style.StyleSpan; 

And then you can change the background of the text using something like the following:

 TextView text = (TextView) findViewById(R.id.text_login); text.setText(""); text.append("Add all your funky text in here"); Spannable sText = (Spannable) text.getText(); sText.setSpan(new BackgroundColorSpan(Color.RED), 1, 4, 0); 

If it highlights the charecters at positions 1 - 4 with red. Hope this helps!

+12
Apr 28 '10 at 17:19
source share

Alternative solution: Use a WebView instead. Html is easy to work with.

 WebView webview = new WebView(this); String summary = "<html><body>Sorry, <span style=\"background: red;\">Madonna</span> gave no results</body></html>"; webview.loadData(summary, "text/html", "utf-8"); 
+4
Apr 28 '10 at 17:48
source share
  String name = modelOrderList.get(position).getName(); //get name from List String text = "<font color='#000000'>" + name + "</font>"; //set Black color of name /* check API version, according to version call method of Html class */ if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) { Log.d(TAG, "onBindViewHolder: if"); holder.textViewName.setText(context.getString(R.string._5687982) + " "); holder.textViewName.append(Html.fromHtml(text)); } else { Log.d(TAG, "onBindViewHolder: else"); holder.textViewName.setText("123456" + " "); //set text holder.textViewName.append(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY)); //append text into textView } 
+2
May 23 '17 at 11:08 a.m.
source share
 textview.setText(Html.fromHtml("<font color='rgb'>"+text contain+"</font>")); 

It will give the color exactly what you did in the html editor, just install textview and compare it with the value of textview. Android does not support color spacing, changes the font color in the editor, and you are all ready to go.

0
Feb 19 '16 at 7:51
source share

the font is outdated, instead of Html.fromHtml("<span style=color:red>"+content+"</span>") use span Html.fromHtml("<span style=color:red>"+content+"</span>")

0
Apr 24 '17 at 15:28
source share

So that part of the text is underlined and colored

in your strings.xml

 <string name="text_with_colored_underline">put the text here and &lt;u>&lt;font color="#your_hexa_color">the underlined colored part here&lt;font>&lt;u></string> 

then in activity

 yourTextView.setText(Html.fromHtml(getString(R.string.text_with_colored_underline))); 

and for clickable links:

 <string name="text_with_link"><![CDATA[<p>text before link<a href=\"http://www.google.com\">title of link</a>.<p>]]></string> 

and in your activity:

 yourTextView.setText(Html.fromHtml(getString(R.string.text_with_link))); yourTextView.setMovementMethod(LinkMovementMethod.getInstance()); 
0
05 Oct '18 at 20:46
source share



All Articles