You can do this by using replace() method then Html.fromHtml() like below
yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
@Override
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus){
if (str.contains("Hello how are you ?") == true)
{
str = ((EditText)v)getText.ToString().replaceAll("Hello how are you ?", "<font color='red'>Hello how are you ?</font>");
((EditText)v).setText(Html.fromHtml(str), TextView.BufferType.SPANNABLE);
}
}
}
});
[Edit 1]
yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
@Override
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus){
// But the words in an ArrayList then use them
for(String s : arrLst){
if (str.contains(s) == true) {
str = ((EditText)v)getText.ToString().replaceAll(s, "<font color='red'>Hello how are you ?</font>");
((EditText)v).setText(Html.fromHtml(str), TextView.BufferType.SPANNABLE); }
}
}
}
});
[Edit 2]
yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
@Override
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus){
int sSelection = ((EditText)v)getText.ToString().getSelectionStart();
int eSelection = ((EditText)v)getText.ToString().getSelectionEnd();
String sString = string.substring(sSelection, eSelection);
str = ((EditText)v)getText.ToString().replaceAll(sString , "<font color='red'>"+sString +"</font>");
((EditText)v).setText(Html.fromHtml(str), TextView.BufferType.SPANNABLE);
}
}
});