Hi there has been a problem with the TextWatcher class, I have tried to find solutions on the subject in this forum and English but I have not found what I was looking for, I have this piece of code in my method onCreate ():
textoBuscado = (EditText) findViewById(R.id.textobuscado);
textoBuscado.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
buscarCadena(textoBuscado.getText().toString());
}
});
My layout is composed of two editText: textSearch and textContents.
The string method is a method that performs the following action:
If in my editText'Content text 'there is the letter "r" and I write an "r" in my editText'Search text' that "r" in the'Content text 'will be colored blue.
This works correctly, the problem is that if I delete the letter "r" of textSearch to write another the application freezes stops and I have to force its closure.
How can I solve this problem? What is really happening?
EDIT
The method works correctly because the text is colored and if I execute the method using a button everything works correctly, the problem is that I want to do it with textWatcher.
The searchString method is as follows: textEdit represents the editText "textContent" that I mentioned.
public void buscarCadena(String cadena) {
String miTexto = textoEditado.getText().toString();
String textoAuxiliar = miTexto;
SpannableString ss = new SpannableString(miTexto);
int guarda = 0;
while (textoAuxiliar.contains(cadena)) {
int index = textoAuxiliar.indexOf(cadena);
int longitud = cadena.length();
if (guarda == 0) guarda = index;
ss.setSpan(new ForegroundColorSpan(Color.BLUE), index, index + longitud, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textoAuxiliar = textoAuxiliar.replaceFirst(cadena, " ");
}
textoEditado.setText(ss);
textoEditado.setSelection(guarda);
}