0

I have a small doubt and I would like if you could make. Currently, I have a String and I check if a word using ".contains("String");" as for example:

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

for (int x = 1; x < 2; x++) {
    tv.append("Text one\n");
    tv.append("Text Two\n");
}

if(tv.getText().toString().contains("Text one")){
    //Found
}else{
    //Not found
}

My question is this. Can I do conditional and if you find the word I'm looking, apply a style?

if(tv.getText().toString().contains("Text one")){
    tv.setTypeface(null, Typeface.BOLD); //I found text. Apply text bold
}else{
    //Not found
}

Regards!

Ataulf
  • 27
  • 5
  • You want to set bold to only `Text one`? – Raghunandan Mar 13 '14 at 15:58
  • @Raghunandan Yes, according to the condition – Ataulf Mar 13 '14 at 15:58
  • The changes you make to the TextView (bold, color) will affect all Strings in that TextView. Sounds what your looking for is a SpannableString which can set spans (such as bold, color or typeface) to certain parts of the String. – Slickelito Mar 13 '14 at 15:59

1 Answers1

3

You need to use a SpannableString.

In the below all characters of # is set to Red. You can make it bold

Create clickable link in text view in android

Edit: Just found the example for making it bold

Why doesn't my text show up with style when using SpannableStringBuilder?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256