For advanced configurations, use the ScanCustomCode() ActivityResultContract. Instead of calling .launch(null), pass a ScannerConfig object built via the ScannerConfig.build { ... } DSL to control scanner behavior.
val scanCustomCode = registerForActivityResult(ScanCustomCode(), ::handleResult)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
…
binding.button.setOnClickListener {
scanCustomCode.launch(
ScannerConfig.build {
setBarcodeFormats(listOf(BarcodeFormat.FORMAT_CODE_128)) // set interested barcode formats
setOverlayStringRes(R.string.scan_barcode) // string resource used for the scanner overlay
setOverlayDrawableRes(R.drawable.ic_scan_barcode) // drawable resource used for the scanner overlay
setHapticSuccessFeedback(false) // enable (default) or disable haptic feedback when a barcode was detected
setShowTorchToggle(true) // show or hide (default) torch/flashlight toggle button
setShowCloseButton(true) // show or hide (default) close button
setHorizontalFrameRatio(2.2f) // set the horizontal overlay ratio (default is 1 / square frame)
setUseFrontCamera(true) // use the front camera
setKeepScreenOn(true) // keep the device's screen turned on
}
)
}
}
fun handleResult(result: QRResult) {
…
}