Home > other >  I try to put a "processing..." text before the a calculation start, in kotlin
I try to put a "processing..." text before the a calculation start, in kotlin

Time:11-16

I try to put a "processing..." text before the a calculation start, but Kotlin code finishing before the layout text change.

var watcher: TextWatcher = object : TextWatcher {
    override fun beforeTextChanged(
        s: CharSequence?,
        start: Int,
        count: Int,
        after: Int
    ) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

    }

    override fun afterTextChanged(s: Editable?) {
        val text = binding.textInput.text.toString()
        if (text != "0")
            binding.prime.text ="processing..."
            binding.prime.text = foundNPrime(text)
    }
}

binding.textInput.addTextChangedListener(watcher)

How can I change first the layout text?

CodePudding user response:

You must use UiThread ex:

[email protected] { 
   binding.prime.text ="processing..." 
}

CodePudding user response:

Assuming foundNPrime() is blocking the main thread and no UI updates can occur when the main thread is busy. If you block the main thread for too long, you'll start seeing application not responding (ANR) dialogs.

Consider using some async pattern to offload the computation to a background worker thread and keep the main thread responsive. Since you're already using kotlin, kotlin coroutines would be an easy way for doing that.

  • Related