I have a program that I'm writing to scan barcodes.
Right now, I can get the output from the reader to show up in the button. This is what the code looks like:
decodeCallback = DecodeCallback {
runOnUiThread {
binding.button.text= it.text
}
}
What I want it to say though is this:
"Search For:
foo"
All inside the button.
I am really new to kotlin so I'm struggling with this one.
CodePudding user response:
I don't think you need runOnUiThread because callbacks typically are automatically called on the UI thread already.
You can use a String template to fill a term into a String:
decodeCallback = DecodeCallback {
binding.button.text= "Search For: ${it.text}"
}
If you're on Android, you should use String resources so your user-facing strings are all defined in one place and you can do translations later easily. In your resources you would have something like this, where you use a Java format string.
<string name="searchButtonLabel">Search For: %s</string>
And then use
decodeCallback = DecodeCallback {
binding.button.text = getString(R.string.searchButtonLabel, it.text)
}
If this is in a Fragment, you'd use requireContext().getString(.
