I have a simple app that, as of right now, contains main_activity.kt and fragment_results.kt and their xml files. The premise of the app is that the user scans a barcode and then hits a button. A fragment comes up and displays all of the data tied to that barcode. Once I understand how to do this part, I will extend it so the user can add barcodes to the spreadsheet but that's down the road.
The problem I'm running into with Kotlin is that it looks like a lot of stuff is deprecated and while android studio is great at showing me the new ways to do things, a simple to follow tutorial is hard to find.
main_activity.kt
package com.example.barcode
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentTransaction
import com.budiyev.android.codescanner.AutoFocusMode
import com.budiyev.android.codescanner.CodeScanner
import com.budiyev.android.codescanner.DecodeCallback
import com.budiyev.android.codescanner.ErrorCallback
import com.budiyev.android.codescanner.ScanMode
import com.example.barcode.databinding.ActivityMainBinding
private const val CAMERA_REQUEST_CODE = 101
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var codeScanner: CodeScanner
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setupPermissions()
codeScanner()
}
private fun codeScanner() {
codeScanner = CodeScanner(this, binding.scannerView)
codeScanner.apply {codeScanner
camera = CodeScanner.CAMERA_BACK
formats = CodeScanner.ALL_FORMATS
autoFocusMode = AutoFocusMode.SAFE
scanMode = ScanMode.SINGLE
isAutoFocusEnabled = true
isFlashEnabled = false
decodeCallback = DecodeCallback {
binding.button.text = getString(R.string.searchButtonLabel, it.text)
}
errorCallback = ErrorCallback {
Log.e("Main", "Camera Initilization failed ${it.message}")
}
}
binding.scannerView.setOnClickListener {
codeScanner.startPreview()
binding.button.text = getString(R.string.resetButtonLabel)
}
}
override fun onResume() {
super.onResume()
codeScanner.startPreview()
}
override fun onPause() {
codeScanner.releaseResources()
super.onPause()
}
private fun setupPermissions() {
val permission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
if (permission != PackageManager.PERMISSION_GRANTED) {
makeRequest()
}
}
private fun makeRequest(){
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.CAMERA),
CAMERA_REQUEST_CODE)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
when (requestCode) {
CAMERA_REQUEST_CODE -> {
if (grantResults.isEmpty() || grantResults[0] != PackageManager.PERMISSION_GRANTED){
Toast.makeText(this,"You need the camera for this app", Toast.LENGTH_SHORT).show()
}else{
}
}
}
}
}
My resultsFragment.kt is a stock blank .kt. Haven't worked on that programming yet.
CodePudding user response: