I want to display on android button price of my InnApp product.
I use queryProductDetailsAsync to get price.
Issue is that when I assign price in button "binding.ButtonBuy.text = price" sometimes it don't show anything or wrong data. I suspect that its because queryProductDetailsAsync is Async and I should firstly wait for function to finish and than assing proper price.
I wonder why Timber.tag("Mik").d("\nPrice: " + price) is always displaying proper price but when assign to button its not always proper.
Can somebody help me to modify code properly to wait with "binding.ButtonBuy.text = price" when price is ready ?
class Zakupy : AppCompatActivity(), PurchasesUpdatedListener {
private lateinit var binding: ActivityZakupyBinding
private lateinit var billingClient: BillingClient
private var price: String = "?"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityZakupyBinding.inflate(layoutInflater)
setContentView(binding.root)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
billingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener(this).build()
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
Timber.tag("Mik").d("BillingRespondCode-OK")
val productList = QueryProductDetailsParams.newBuilder().setProductList(ImmutableList.of(QueryProductDetailsParams.Product.newBuilder().setProductId("add_free").setProductType(BillingClient.ProductType.INAPP).build())).build()
billingClient.queryProductDetailsAsync(productList) { billingResult,
productDetailsList ->
val productDetails = productDetailsList[0] // choose index zero because we had only 1 product
price = productDetails.oneTimePurchaseOfferDetails?.formattedPrice.toString()
.filter { it.isDigit() || it == ',' || it == '.' } + " " + productDetails.oneTimePurchaseOfferDetails?.priceCurrencyCode
Timber.tag("Mik").d("\ncena: " + price)
binding.ButtonKup.text = price
}