I have a fragment with an EditText and a couple of buttons.
One of the buttons allows the user to scan a barcode with the GmsBarcodeScanning library.
If successful, the scanned barcode shoould appear into the EditText, but I can’t get it to work.
The problem seems to be that after the onSuccessListener() the onCreateView() is executed and thus all the components are reset.
I think it must be simple, but I’ve spent hours and can’t find a solution.
class SelecComanda : Fragment() {
private var et_codi: EditText? = null
private var btn_scan: Button? = null
private var btn_next: Button? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.selec_comanda, container, false)
et_codi = root.findViewById(R.id.editTextCodi)
btn_scan = root.findViewById(R.id.boto_scan)
btn_next = root.findViewById(R.id.button_second)
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btn_next!!.isEnabled = false
btn_next!!.setOnClickListener {
// TODO
}
btn_scan!!.setOnClickListener {
val options = GmsBarcodeScannerOptions.Builder()
.enableAutoZoom()
.build()
val scanner = GmsBarcodeScanning.getClient(this.context!!, options)
scanner.startScan()
.addOnSuccessListener { barcode ->
et_codi!!.setText(barcode.rawValue.toString())
btn_next!!.setEnabled(true)
}
.addOnCanceledListener {
// TODO
}
.addOnFailureListener { e ->
// TODO
}
}
}
}
How could I recover the scanned barcode and put into the EditText?