Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting Text Color using Html.fromHtml() in Android?

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

But I will like to know whether its the proper way of doing it or not.

Please let me know your views on this.

like image 383
sunil Avatar asked Apr 28 '10 15:04

sunil


People also ask

How do you color your text on Android?

Open your device's Settings app . Text and display. Select Color correction. Turn on Use color correction.

How do you change the color of Spannable text on Android?

Changes the color of the text to which the span is attached. For example, to set a green text color you would create a SpannableString based on the text and set the span. SpannableString string = new SpannableString("Text with a foreground color span"); string. setSpan(new ForegroundColorSpan(color), 12, 28, Spanned.

How do I color my text in HTML?

To change some of the text in the HTML document to another color use the FONT COLOR Tag. To change the color of the font to red add the following attribute to the code to the <FONT COLOR=" "> tag. #ff0000 is the color code for red.

How do we change a color of a text in Android Studio?

TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.


7 Answers

Or far simpler than dealing with Spannables 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);
like image 82
Christopher Orr Avatar answered Oct 11 '22 09:10

Christopher Orr


Using color value from 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); 
like image 30
SergeyA Avatar answered Oct 11 '22 11:10

SergeyA


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("Your text here");
Spannable sText = (Spannable) text.getText();
sText.setSpan(new BackgroundColorSpan(Color.RED), 1, 4, 0);

Where this will highlight the charecters at pos 1 - 4 with a red color. Hope this helps!

like image 25
stealthcopter Avatar answered Oct 11 '22 10:10

stealthcopter


Alternative solution: Using 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");
like image 30
Vidar Vestnes Avatar answered Oct 11 '22 09:10

Vidar Vestnes


 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
    }
like image 22
Khyati Vara Avatar answered Oct 11 '22 09:10

Khyati Vara


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

like image 32
mhdtouban Avatar answered Oct 11 '22 09:10

mhdtouban


To make part of your text 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 the 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());
like image 22
bsma Avatar answered Oct 11 '22 11:10

bsma