Skip to content

Instantly share code, notes, and snippets.

@nickhargreaves
Created June 4, 2018 18:00
Show Gist options
  • Save nickhargreaves/de47960a34cfe70dc370f3f63eaad42f to your computer and use it in GitHub Desktop.
Save nickhargreaves/de47960a34cfe70dc370f3f63eaad42f to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity() {
private lateinit var mResultTextView: TextView
private lateinit var mPhonenumberTextView: TextView
private var b: Boolean? = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mResultTextView = findViewById(R.id.result_textview)
mPhonenumberTextView = findViewById(R.id.phone_number)
findViewById<Button>(R.id.scan_barcode_button).setOnClickListener {
val intent = Intent(applicationContext, BarcodeCaptureActivity::class.java)
startActivityForResult(intent, BARCODE_READER_REQUEST_CODE)
}
findViewById<Button>(R.id.make_payment).setOnClickListener{
if(b == true && mPhonenumberTextView.text.isNotEmpty()) {
// Make request using volley
val queue = Volley.newRequestQueue(this)
val url = "[YOUR_API_URL]?phone_number=" + mPhonenumberTextView.text + "&item_id=" + mResultTextView.text
val stringRequest = StringRequest(Request.Method.GET, url,
Response.Listener<String> { response ->
mResultTextView.text = response.toString()
},
Response.ErrorListener { mResultTextView.text = "Error" })
queue.add(stringRequest)
}else{
Toast.makeText(this, "Make sure you have entered the phone number and that your QR code scan was successful", Toast.LENGTH_LONG).show()
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == BARCODE_READER_REQUEST_CODE) {
if (resultCode == CommonStatusCodes.SUCCESS) {
if (data != null) {
val barcode = data.getParcelableExtra<Barcode>(BarcodeCaptureActivity.BarcodeObject)
val p = barcode.cornerPoints
b = true
mResultTextView.text = barcode.displayValue
} else
mResultTextView.setText(R.string.no_barcode_captured)
} else
Log.e(LOG_TAG, String.format(getString(R.string.barcode_error_format),
CommonStatusCodes.getStatusCodeString(resultCode)))
} else
super.onActivityResult(requestCode, resultCode, data)
}
companion object {
private val LOG_TAG = MainActivity::class.java.simpleName
private val BARCODE_READER_REQUEST_CODE = 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment