Search code examples
javabuttononclickadmobinterstitial

How Can Show InterAd After X Clicks?


How can show the interstitial ads after 2 button clicks in this code java ? "Lastes SDK Admob 20.2.0"

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG,"Button 1 Clicked");

            if (mInterstitialAd != null) {
                save_id(btn1.getId());
                mInterstitialAd.show(main_activity.this);
            }
            else {
                Log.d("TAG", "The interstitial ad wasn't ready yet");

                Intent intent = new Intent(main_activity.this, getstarted.class);
                startActivity(intent);
            }
        }
    });
  

Solution

  • You can store a variable like clickCount and everytime its even value ie. modulo by 2 == 0 you could show the ad right?

    int clickCount = 0;

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG,"Button 1 Clicked");
            clickCount++;
            if (mInterstitialAd != null && (clickCount % 2 == 0)) {
                clickCount = 0;
                save_id(btn1.getId());
                mInterstitialAd.show(main_activity.this);
            }
            else {
                Log.d("TAG", "The interstitial ad wasn't ready yet or the button was only clicked once");
    
                Intent intent = new Intent(main_activity.this, getstarted.class);
                startActivity(intent);
            }
        }
    });