-2

I have a recyclerview and I want to search in it with help of searchview. I want my searched characters to be colored and be specific.

I am programming with Kotlin.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MMG
  • 3,226
  • 5
  • 16
  • 43

1 Answers1

3

After a hard effort I found the answer myself. I use a function like this:

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
}

The "charText" in "a" will be Red.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MMG
  • 3,226
  • 5
  • 16
  • 43
  • How to convert Kotlin to Java? –  Mar 23 '20 at 11:22
  • I think they are similar. Maybe here helps you: https://stackoverflow.com/questions/34957430/how-to-convert-a-kotlin-source-file-to-a-java-source-file @Sanidhya Setu – MMG Mar 23 '20 at 11:24