Highlight searched text in textview not working

newbie77 :

#solved# When i build there is no error but upoun search for a specific text in edit text and hit the search button no changes are made i.e no highlights on my text.Please help.Below are my codes

// my java code
public void onClick(View v) {
                String textTOHighlight=etText.getText().toString();
  //delete this line  String replacedWith = "<span style='background-color:yellow'>"+textTOHighlight+"</span>";
                String originalText=textView.getText().toString();
// delete this line  String modifiedText=originalText.replaceAll(textTOHighlight,replacedWith);
                textView.setText(Html.fromHtml(modifiedText));
//added this and it works
String modifiedText=originalText.replaceAll(textTOHighlight, "<font color='red'>"+textTOHighlight+"</font>");
                textView.setText(Html.fromHtml(modifiedText));
// strings.xml
<string-array name="chapters">
<item>this is just an example for first item</item>
<item>this is the second example <item>
</string-array>
// activity
 <EditText
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:id="@+id/et_text"
        android:hint="Enter text"
        android:padding="7dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/bg_round"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/search"
        android:text="Search"
        android:layout_marginTop="10dp"
        android:layout_marginStart="120dp"/>

    <TextView
        android:id="@+id/textv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="90dp"
        android:scrollbars="vertical"
        android:background="@drawable/bg_round"/>
MohammadMoeinGolchin :
fun colorsearch(a:String,charText: String):SpannableStringBuilder{
            var l = 0
            var b:ArrayList<Int>
            b = ArrayList()
            var w = 0
            var i = 0
            if (charText!=""){
                label@ while (i < a.length) {
                    var j=0
                    while (j<charText.length){
                        Log.v("abc", j.toString())
                        if (i == a.length)
                            break@label
                        while ((a[i] != charText[j])) {
                            if (j != 0) {
                                continue@label
                            }
                            i++
                            if (i == a.length)
                                break@label
                        }
                        i++
                        j++
                    }
                    b.add(i)
                    w++
                    if (i == a.length)
                        break@label
                }}
            val searchtitle = SpannableStringBuilder(a)
            while (l < w) {
                searchtitle.setSpan(
                    ForegroundColorSpan(Color.RED),
                    b[l] - charText.length, b[l],
                    Spanned.SPAN_EXCLUSIVE_INCLUSIVE
                )
                l++
            }
            return searchtitle
        }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174820&siteId=1