0

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
                }
7
  • The above code is correct. All the code inside the lambda function is called only after the asynchronous task is complete. Maybe you are setting the TextView somewhere else in your code that you shouldn’t be. Commented May 8, 2024 at 18:30
  • Unrelated recommendation: you probably should not try to remove the currency from the String and tack it back on because the currency symbol’s proper position before or after the number varies by country. Commented May 8, 2024 at 18:33
  • Thanks for help @Tenfour04. I have debug it more and seems that real issue is with "binding.ButtonBuy.text = price" line. Sometimes (10%) its just don't work and not assign any value despite price is derived correctly. Not sure why. I am not modyfing it in another place. There is just default value "$". Commented May 9, 2024 at 12:09
  • Maybe you are binding multiple layouts under certain conditions and updating the outdated layout. Commented May 9, 2024 at 12:27
  • @Tenfour04 I have added more code in original question. Probably something wrong with this binding. Can You check ? Commented May 9, 2024 at 13:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.