Search code examples
androidandroid-studiokotlinadmobadmob-rewardedvideoad

Trying to implement Rewarded Ad to Android Studio correctly with Kotlin


Hello everyone i have an app for presenting random lotto balls. I have a button for incrementing token count binded to a rewarded ad. But i can't make it function properly. Here is my code:

package com.zumatrahia.kazandiranlototahmini

import android.content.Context
import android.content.pm.ActivityInfo
import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.ads.*
import com.google.android.gms.ads.rewarded.RewardItem
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback


class SayisalActivity : AppCompatActivity() {
    lateinit var Ball01: TextView
    lateinit var Ball02: TextView
    lateinit var Ball03: TextView
    lateinit var Ball04: TextView
    lateinit var Ball05: TextView
    lateinit var Ball06: TextView
    lateinit var mediaPlayer: MediaPlayer
    lateinit var tokenText: TextView
    var tokenCount: Int = 0
    private var mRewardedAd: RewardedAd? = null
    private final var TAG = "SayisalActivity"

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sayisal)
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

        Ball01 = findViewById(R.id.Ball01)
        Ball02 = findViewById(R.id.Ball02)
        Ball03 = findViewById(R.id.Ball03)
        Ball04 = findViewById(R.id.Ball04)
        Ball05 = findViewById(R.id.Ball05)
        Ball06 = findViewById(R.id.Ball06)
        mediaPlayer = MediaPlayer.create(this, R.raw.success_sound)

        MobileAds.initialize(this) {loadRewardedAd()}

        val spinner: Spinner = findViewById(R.id.spinner)
        ArrayAdapter.createFromResource(
            this, R.array.lotto_choices, R.layout.spinner_list
        ).also { adapter ->
            adapter.setDropDownViewResource(R.layout.spinner_list)
            spinner.adapter = adapter
        }

        val sharedPreferences = getSharedPreferences("tokenCount", Context.MODE_PRIVATE)
        tokenCount = sharedPreferences.getInt("INT_KEY", 5)
        tokenText = findViewById(R.id.token_text)
        tokenText.text = "Sahip olduğunuz çekiliş hakkı sayısı = $tokenCount"
    }

    private fun loadRewardedAd() {
        var adRequest = AdRequest.Builder().build()

        RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917", adRequest, object : RewardedAdLoadCallback() {
                override fun onAdFailedToLoad(adError: LoadAdError) {
                    Log.d(TAG, "Ad wasn't loaded")
                    mRewardedAd = null
                    loadRewardedAd()
                }

                override fun onAdLoaded(rewardedAd: RewardedAd) {
                    Log.d(TAG, "Ad was loaded.")
                    mRewardedAd = rewardedAd

                    mRewardedAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
                        override fun onAdDismissedFullScreenContent() {
                            Log.d(TAG, "Ad was dismissed.")
                            loadRewardedAd()
                        }

                        override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
                            Log.d(TAG, "Ad failed to show.")
                            loadRewardedAd()
                        }

                        override fun onAdShowedFullScreenContent() {
                            Log.d(TAG, "Ad showed fullscreen content.")
                            mRewardedAd = null
                        }
                    }
                }
            })
    }

    private fun showRewardedAd(){
        if (mRewardedAd != null) {
            mRewardedAd?.show(this, OnUserEarnedRewardListener() {
                fun onUserEarnedReward(rewardItem: RewardItem) {
                    var rewardAmount = rewardItem.amount
                    var rewardType = rewardItem.type
                    Log.d(TAG, "User earned the reward.")
                    tokenCountUp()
                }
            })
        } else {
            Log.d(TAG, "The rewarded ad wasn't ready yet.")
            loadRewardedAd()
        }

    }


    fun playButtonPressed(view: View) {
        mediaPlayer.start()
        BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 50).textChange()
        BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 100).textChange()
        BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 150).textChange()
        BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 200).textChange()
        BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 300).textChange()
        BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 400).textChange()
        BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 500).textChange()
        BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 800).textChange()
        BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 1100).textChange()
        BallSayisal(Ball01, Ball02, Ball03, Ball04, Ball05, Ball06, 1600).textChange()
        tokenCountDown()
    }


    fun rewardButtonPressed(view: View) {
        showRewardedAd()
    }


    fun tokenCountDown() {
        val sharedPreferences = getSharedPreferences("tokenCount", Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        editor.apply {
            tokenCount -= 1
            tokenText.text = "Sahip olduğunuz çekiliş hakkı sayısı = $tokenCount"
            putInt("INT_KEY", tokenCount)
        }.apply()
    }


    fun tokenCountUp() {
        val sharedPreferences = getSharedPreferences("tokenCount", Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        editor.apply {
            tokenCount += 1
            tokenText.text = "Sahip olduğunuz çekiliş hakkı sayısı = $tokenCount"
            putInt("INT_KEY", tokenCount)
        }.apply()
    }
}

Problem is when i click the button and open an rewarded video ad in the ad it counts down for reward and says "user rewarded" but Log.d(TAG, "User earned the reward.") doesnt implement. And of course tokenCountUp() doesnt implement either.Only Ad loaded and Ad showed fullscreen content comes to Log. But in a tutorial i followed User earned the reward tag comes to Log screen. If i put tokenCountUp() to either under override fun onAdShowedFullScreenContent() or showRewardedAd(){ if (mRewardedAd != null) { it works but before user actually watches the ad it counts up. Where am i doing wrong?


Solution

  • I changed showRewardedAd function content to this and it works now

    if (mRewardedAd != null) {
                mRewardedAd!!.show(this) { rewardItem -> // Handle the reward.
                    Log.d(TAG, "The user earned the reward.")
                    val rewardAmount = rewardItem.amount
                    val rewardType = rewardItem.type
                    tokenCountUp()
                }
            } else {
                Log.d(TAG, "The rewarded ad wasn't ready yet.")
                loadRewardedAd()
            }