Last active
October 31, 2019 05:44
-
-
Save ralphgabrielle/2c3a5081ae4cbe615c615abfd59fad2f to your computer and use it in GitHub Desktop.
Simple QR Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : BaseActivity() { | |
override val layoutId: Int? | |
get() = R.layout.activity_main | |
override fun viewCreated() { | |
bScan.setOnClickListener { | |
startActivityForResult<QRCodeActivity>(REQUEST_CODE_QR) // Kotlin Anko Intent Library | |
} | |
} | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
if (resultCode == Activity.RESULT_OK) { | |
if (requestCode == REQUEST_CODE_QR) { | |
data?.getStringExtra("qr")?.let { | |
Log.d("QR Code Received", it) | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class QRCodeActivity : BaseActivity(), QRCodeReaderView.OnQRCodeReadListener { | |
override val layoutId: Int? | |
get() = R.layout.activity_qrcode | |
override fun viewCreated() { | |
qrCode.setOnQRCodeReadListener(this) | |
setSupportActionBar(toolbar) | |
supportActionBar?.setDisplayHomeAsUpEnabled(true) | |
supportActionBar?.setDisplayShowHomeEnabled(true) | |
} | |
override fun onResume() { | |
super.onResume() | |
qrCode.startCamera() | |
} | |
override fun onPause() { | |
super.onPause() | |
qrCode.stopCamera() | |
} | |
override fun onOptionsItemSelected(item: MenuItem): Boolean { | |
when (item.itemId) { | |
android.R.id.home -> { | |
finish() | |
return true | |
} | |
} | |
return super.onOptionsItemSelected(item) | |
} | |
override fun onQRCodeRead(text: String?, points: Array<out PointF>?) { | |
qrCode.stopCamera() | |
val intent = Intent() | |
intent.putExtra("qr", text) | |
setResult(Activity.RESULT_OK, intent) | |
finish() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment