Change Text Color of One Word in a Textview

Change text color of one word in a TextView

Easiest way I know is to just use html.

String first = "This word is ";
String next = "<font color='#EE0000'>red</font>";
t.setText(Html.fromHtml(first + next));

But this will require you to rebuild the TextView when (if?) you want to change the color, which could cause a hassle.

How to change text color of the part of the TextView?

Try This:

Set TextView as a HTML using SpannableTextView

String text = "<font color='black'>123456</font><font color='red'>789</font>";
textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);

How can I change the color of a part of a TextView?

Spannable is more flexible:

String text2 = text + CepVizyon.getPhoneCode() + "\n\n"
+ getText(R.string.currentversion) + CepVizyon.getLicenseText();

Spannable spannable = new SpannableString(text2);

spannable.setSpan(new ForegroundColorSpan(Color.WHITE), text.length(), (text + CepVizyon.getPhoneCode()).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

myTextView.setText(spannable, TextView.BufferType.SPANNABLE);

How to change text Size and color of single word in Text View

You can try SpannableString, it's a powerful tool that you can use to set multiple styles in TextView. See this.

How to change text color of specific text in textview at runtime?

Assuming that you get a string like you mentioned,

If you want to color a text you can use <font color='#EE0000'>TextYouWantToColor</font> so you need to replace <(> with <font color='#EE0000'> and <)> with </font>

Example

public class YourActivity extends AppCompatActivity  {

private String yourString ;
private String yourNewString ;
private TextView tv ;
private String colorCodeStart = "<font color='#EE0000'>"; // use any color as your want
private String colorCodeEnd = "</font>";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tv = (TextView)findViewById(R.id.tv);

yourString = "MY name is <(>Mohit Kumar<)>. <(>Sachin<)> is my role model. I am <(>12<)> year old. Currently i am in <(>delhi<)> but my hometown is <(>bangalore<)>.";
yourNewString = yourString.replace("<(>",colorCodeStart); // <(> and <)> are different replace them with your color code String, First one with start tag
yourNewString= yourNewString.replace("<)>",colorCodeEnd); // then end tag
Log.d("CheckNew",yourNewString);
tv.setText((Html.fromHtml(yourNewString)));

}


}

Note :Html.fromHtml() is deprecated, what is the alternative? Check here

Sample Image

Change text Background color of Specific word in a TextView

Here is my working code that you can use to highlight some part of string:

     private void highlightTextPart(TextView textView, int index, String regularExpression) {
String fullText = textView.getText().toString();
int startPos = 0;
int endPos = fullText.length();
String[] textParts = fullText.split(regularExpression);
if (index < 0 || index > textParts.length - 1) {
return;
}
if (textParts.length > 1) {
startPos = fullText.indexOf(textParts[index]);
endPos = fullText.indexOf(regularExpression, startPos);
if (endPos == -1) {
endPos = fullText.length();
}
}
Spannable spannable = new SpannableString(fullText);
ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });
TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(null, Typeface.BOLD_ITALIC, -1, blueColor, null);
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.GREEN);
spannable.setSpan(textAppearanceSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(backgroundColorSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
}

Then in your activity, call like the following:

    int index = 3;
String regularExpression = " ";
String text = "Hello StackOverflow From BNK!";
TextView textView = (TextView) findViewById(R.id.textView);
if (textView != null) {
textView.setText(text);
highlightTextPart(textView, index, regularExpression);
}

Sample Image

Change the color of a part of a TextView in strings.xml

Use <font color="#008000">free</font> instead. According to the documentation, the correct attribute name is color and it only supports hex codes.

Single TextView with multiple colored text

yes, if you format the String with html's font-color property then pass it to the method Html.fromHtml(your text here)

String text = "<font color=#cc0029>First Color</font> <font color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));

Android change color part of a string

Use ForegroundColorSpan.

if(classe != null && classe.contains(“idiom”))
{
Spannable spannable = new SpannableString(classe);
spannable.setSpan(new ForegroundColorSpan(Color.BLUE), classe.indexOf(“idiom”), classe.indexOf(“idiom”) + “idiom”.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtclasse.setText(spannable);
}


Related Topics



Leave a reply



Submit