I want to create an intermediate progress bar and this progress bar will be gone when my task is completed.
Code:
class MyZarinpal:AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_myzarinpal)
val button = findViewById<Button>(R.id.btn_zarinpal)
val progressBar: ProgressBar = progressBar1
verificationpayment()
button.setOnClickListener {
//progress bar visiblity will be visible
// do some back stuff // in here my payment()
// and after loading complitliy the progress bar Visibility will be gone
}
}
private fun mypayment() {
val purchase = ZarinPal.getPurchase(this)
val payment: PaymentRequest = ZarinPal.getPaymentRequest()
payment.merchantID = getString(R.string.ZarinpalMerchantId)
payment.amount = getString(R.string.increase_ten_thousend_toman).toLong()
payment.isZarinGateEnable(true)
payment.description = "your test payment"
payment.setCallbackURL("return://myapp")
purchase.startPayment(payment) { status, authority, paymentGatewayUri, intent ->
if (status == 100) {
startActivity(intent)
} else {
Toast.makeText(
this,
"خطایی در برقراری ارتباط با درگاه رخ داده ، لطفا دوباره امتحان کنید",
Toast.LENGTH_LONG
).show()
}
}
}
private fun verificationpayment() {
val text = findViewById<TextView>(R.id.text_status)
val data: Uri? = intent.data
val getpurchase = ZarinPal.getPurchase(this)
getpurchase.verificationPayment(
data
) { isPaymentSuccess, refID, paymentRequest ->
if (isPaymentSuccess) {
Toast.makeText(this, "افزایش موجودی شما با موفقیت انجام شد ", Toast.LENGTH_LONG)
.show()
text.text = "شماره خرید شما :$refID"
} else {
Toast.makeText(
this,
"در عملیات پرداخت خطایی رخ داده ، لطفا دوباره سعی کنید",
Toast.LENGTH_LONG
).show()
}
}
}
}
I want that, when the button is clicked progress bar view is visible and as soon as the task (in here go to a web page ) is completed,the progress bar view is Gone .
I tried this:
button.setonclicklistener {
progressbar.visibility = View.visible
mypayment() // this is my task that will be run on background
// after loading the progress bar visibilty gone
progressbar.visibilty = View.Gone
But this code does not properly work and even does not show the progress bar. It directly goes to webpage.
You're calling progressBar.visibilty = View.GONE
right after you call mypayment()
, but that function runs asynchronously. This means you hide the progress bar before you mypayment()
finishes. You should call progressBar.visibilty = View.GONE
only after you get the callback from your API:
val getpurchase = ZarinPal.getPurchase(this)
getpurchase.verificationPayment(
data
) { isPaymentSuccess, refID, paymentRequest ->
progressBar.visibilty = View.GONE
if (isPaymentSuccess) {
Toast.makeText(this, "افزایش موجودی شما با موفقیت انجام شد ", Toast.LENGTH_LONG)
.show()
text.text = "شماره خرید شما :$refID"
} else {
Toast.makeText(
this,
"در عملیات پرداخت خطایی رخ داده ، لطفا دوباره سعی کنید",
Toast.LENGTH_LONG
).show()
}
}
By the way, mypayment()
is not a proper function name. You should name functions with an operation/action. For example, mypayment()
should be makePayment()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.