When click again on button to show rewarded ad no ad is showing

1.3k views Asked by At

So, i want to make an app with a simple button that shows an rewarded ad. The button works and shows a rewarded ad but when i press again no rewarded ad is showing. I want to show ad for every button click.

this is the code:

public class MainActivity extends AppCompatActivity {
    private RewardedAd rewardedAd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);
        TextView textView = (TextView) findViewById(R.id.textView);

        rewardedAd = new RewardedAd(this,
                "ca-app-pub-3940256099942544/5224354917");


     final RewardedAdLoadCallback[] adLoadCallback = {new RewardedAdLoadCallback() {
            @Override
            public void onRewardedAdLoaded() {
                // Ad successfully loaded.
            }

            @Override
            public void onRewardedAdFailedToLoad(LoadAdError adError) {
                // Ad failed to load.
            }
        }};


        rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback[0]);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rewardedAd.isLoaded()) {
                    Activity activityContext = MainActivity.this;
                    RewardedAdCallback adCallback = new RewardedAdCallback() {

                        @Override
                        public void onRewardedAdOpened() {
                            // Ad opened.
                        }

                        @Override
                        public void onRewardedAdClosed() {


                        }

                        @Override
                        public void onUserEarnedReward(@NonNull RewardItem reward) {


                    }

                        @Override
                        public void onRewardedAdFailedToShow(AdError adError) {
                            // Ad failed to display.
                        }
                    };
                    rewardedAd.show(activityContext, adCallback);
                } else {
                    Log.d("TAG", "The rewarded ad wasn't loaded yet.");
                }

            }
        });

      }
    }

I didnt set the reward , first i want the button to be able to show multiple ads

1

There are 1 answers

1
Whitebird On