Applying style to selected text of a String in android?

1.1k views Asked by At

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!

1

There are 1 answers

0
Raghunandan On BEST ANSWER

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?