unacorn
unacorn

Reputation: 1022

Admob - Rewarded Ads is loaded but not showing

I am adding rewarded ads into my application after user close the interstitial ads, it will show a screen that show the text input previously, and having a show video button beneath it for rewarded ads. (I am at testing phase hence I don't include any rewarded items whatsoever yet, just want to show the video ads first).

When I use Toast widget to identify whether the ads has been loaded or not, I realise ads have been loaded ("onRewardedAdLoaded" text is shown). However when I click the watch video button, it seems that the showRewardedVideo() is not being called. Can someone help me to see what's wrong with my code?

enter image description here

My code:

public class MainActivity extends AppCompatActivity {

public static final String EXTRA_MESSAGE = "com.example.mysecondapp.MESSAGE";
private AdView mAdView;
private InterstitialAd mInterstitialAd;
Button mMyButton;
private RewardedAd rewardedAd;
private Button showVideoButton;
boolean isLoading;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });
    loadRewardedAd();
    mAdView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
    mMyButton = (Button) findViewById(R.id.button);
    mMyButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
                mInterstitialAd.setAdListener(new AdListener() {
                    @Override
                    public void onAdClosed() {
                        AdRequest adRequest = new AdRequest.Builder().build();
                        mInterstitialAd.loadAd(adRequest);
                        sendMessage(mAdView);
                        // Create the "show" button, which shows a rewarded video if one is loaded.
                        showVideoButton = findViewById(R.id.show_video_button);
                        showVideoButton.setVisibility(View.INVISIBLE);
                        showVideoButton.setOnClickListener(
                                new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        showRewardedVideo();
                                    }
                                });
                    }
                });
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    });
}

private void loadRewardedAd() {
    if (rewardedAd == null || !rewardedAd.isLoaded()) {
        rewardedAd = new RewardedAd(this, "ca-app-pub-3940256099942544/5224354917");
        isLoading = true;
        rewardedAd.loadAd(
                new AdRequest.Builder().build(),
                new RewardedAdLoadCallback() {
                    @Override
                    public void onRewardedAdLoaded() {
                        // Ad successfully loaded.
                        MainActivity.this.isLoading = false;
                        Toast.makeText(MainActivity.this, "onRewardedAdLoaded", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onRewardedAdFailedToLoad(int errorCode) {
                        // Ad failed to load.
                        MainActivity.this.isLoading = false;
                        Toast.makeText(MainActivity.this, "onRewardedAdFailedToLoad", Toast.LENGTH_SHORT)
                                .show();
                    }
                });
    }
}



/**
 * Called when the user taps the Send button
 */
public void sendMessage(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

private void showRewardedVideo() {
    showVideoButton.setVisibility(View.INVISIBLE);
    if (rewardedAd.isLoaded()) {
        RewardedAdCallback adCallback =
                new RewardedAdCallback() {
                    @Override
                    public void onRewardedAdOpened() {
                        // Ad opened.
                        Toast.makeText(MainActivity.this, "onRewardedAdOpened", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onRewardedAdClosed() {
                        // Ad closed.
                        Toast.makeText(MainActivity.this, "onRewardedAdClosed", Toast.LENGTH_SHORT).show();
                        // Preload the next video ad.
                        MainActivity.this.loadRewardedAd();
                    }

                    @Override
                    public void onUserEarnedReward(RewardItem rewardItem) {
                        // User earned reward.
                        Toast.makeText(MainActivity.this, "onUserEarnedReward", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onRewardedAdFailedToShow(int errorCode) {
                        // Ad failed to display
                        Toast.makeText(MainActivity.this, "onRewardedAdFailedToShow", Toast.LENGTH_SHORT)
                                .show();
                    }
                };
        rewardedAd.show(this, adCallback);
    }
}
}

Thanks!

Upvotes: 0

Views: 2797

Answers (1)

Nithis Kumar
Nithis Kumar

Reputation: 308

Modify your rewarded ad loading method like this

private void loadRewardedAd() {
if (rewardedAd == null || !rewardedAd.isLoaded()) {
    isLoading = true;
    rewardedAd.loadAd("ca-app-pub-3940256099942544/5224354917",
        new AdRequest.Builder().build());
}
}
*****Method ends here***** //implement RewardedVideoAdListners' callback methods like this
    @Override
public void onRewarded(RewardItem reward) {
    Toast.makeText(this, "onRewarded! currency: " + reward.getType() + "  amount: " +
            reward.getAmount(), Toast.LENGTH_SHORT).show();
    // Reward the user.
}

@Override
public void onRewardedVideoAdLeftApplication() {
    Toast.makeText(this, "onRewardedVideoAdLeftApplication",
            Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdClosed() {
    MainActivity.this.loadRewardedAd();
    Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
    Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdLoaded() {
    Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdOpened() {
    Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoStarted() {
    Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoCompleted() {
    Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show();
}

Then implement RewardedVideoAdListener like this

public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener  {

show ads method like below

private void showRewardedVideo() {
showVideoButton.setVisibility(View.INVISIBLE);
if(rewardedAd.isLoaded){
rewarded.show();
    } else {
//Do something...
}
}

Upvotes: 0

Related Questions