Hello there I'm working on this project for onscreen handwriting recognition using ML Kit.
The problem that I'm facing is I need to click twice to update the text in the text box. On the first click, it recognises the text and then on the second click, it recognises the text and updates. I'm new to Kotlin and I'm not being able to figure out why.
Main activity :
StrokeManager.download()
recognize.setOnClickListener {
    StrokeManager.recognize(this)
    res()
    textview = findViewById(R.id.textView) as TextView
    textview.setText(res())
}
Strokemanger Object file:
fun recognize(context: Context) {
    val recognizer: DigitalInkRecognizer =
        DigitalInkRecognition.getClient(
            DigitalInkRecognizerOptions.builder(model).build()
        )
    val ink = inkBuilder.build()
    recognizer.recognize(ink)
        .addOnSuccessListener { result: RecognitionResult ->
            recogResult = result.candidates[0].text
        }
        .addOnFailureListener { e: Exception ->
            Log.e("StrokeManager", "Error during recognition: $e")
        }
}
fun res(): String? {
    var resultToText = recogResult
    return resultToText
}
bellow is the link to my git project:
 
                        
recognizerworks asynchronously. It means it will not return the result as soon as you callrecognizemethod. By the time you callres()theOnSuccessListenerhasn't yet been called.One option would be to modify
fun recognize(context: Context)to accept one more argument as a callback:And this is how an updated version of "reading" the resulting text could look like:
I've used
isClickableproperty ofrecognizeview to prevent multiple recognition processes being launch in a short period of time, especially if the previous process is not yet finished.