Any new project ideas? 🤔
It searches the text and highlights the text.
Imports: import android.text.Editable; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.TextWatcher; import android.text.style.BackgroundColorSpan;
Main Code: String word = edittext1.getText().toString();
String text = textview1.getText().toString(); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
int start = 0; while ((start = text.indexOf(word, start)) != -1) { spannableStringBuilder.setSpan(new BackgroundColorSpan(0xFFFFC107), start, start + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); start += word.length(); }
// Update the text in textview1 with the highlighted spans textview1.setText(spannableStringBuilder);
©2024 Sketchub | User Policy