-1

Anybody know how to show the interstitial ads when the user click the button second time.I mean when the user click the button once then the ad should not appear but whenever the user click the same button second time then the ad must show..

public class MainActivity extends AppCompatActivity {


Toolbar toolbar;
ShowInterstitial showInterstitial;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showInterstitial = new ShowInterstitial(this);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.app_bar);

    /*toolbar = findViewById(R.id.app_bar);
    toolbar.setTitle("hell");
    toolbar.*/

}
int counter = 0;
public void onClick(View view) {

    if(view.getId() == R.id.ll1 ) {


        counter++;
        if (counter == 2) {
            counter = 0;
            Intent intent = new Intent(this, AggregatesActivity.class);
            startActivity(intent);
            if (showInterstitial != null && ShowInterstitial.isLoaded())
                showInterstitial.showInterstitial();
        }

    }

and ShowInterstitial code is here which i calling in different activities.

    public class ShowInterstitial {

    private InterstitialAd mInterstitialAd;
    private Context context;
    private boolean isAddReplace = false;

    public ShowInterstitial(Context context) {
        this.context = context;
        mInterstitialAd = newInterstitialAd(context);
        loadInterstitial();
    }

    private InterstitialAd newInterstitialAd(final Context context) {
        InterstitialAd interstitialAd = new InterstitialAd(context);
        /*if (!isAddReplace)
            interstitialAd.setAdUnitId(context.getString(R.string.interstitial_one));*/

            interstitialAd.setAdUnitId(context.getString(R.string.interstitial_one));
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                isAddReplace = !isAddReplace;
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
            }

            @Override
            public void onAdClosed() {
                goToNextLevel();
            }
        });
        return interstitialAd;
    }

    public boolean showInterstitial() {
        if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            goToNextLevel();
        }
        return false;
    }

    public void loadInterstitial() {
        // Disable the next level button and load the ad.
        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();
        mInterstitialAd.loadAd(adRequest);
    }

    private void goToNextLevel() {
        // Show the next level and reload the ad to prepare for the level after.

        mInterstitialAd = newInterstitialAd(context);
        loadInterstitial();
    }


}
Sufyan Hashmi
  • 21
  • 1
  • 10

2 Answers2

8

@Sufyan Hashmi you need a int variable whose value will increase on every click whenever the value is 2 you should call load inerestitial ad and assign the variable's value zero.

 int counter = 0;

 if(view.getId()==R.id.ll1)

    {
        counter++;
        if (counter == 2) {
            counter = 0;
            Intent intent = new Intent(this, AggregatesActivity.class);
            startActivity(intent);
            if (showInterstitial != null && showInterstitial.isLoaded())
                showInterstitial.showInterstitial();
        }

    }
Drona Tomar
  • 152
  • 4
  • Please confirm me that you want to show the InterstitialAd first and when the ad will close then the activity will open? or you want to open the activity and InterstitialAd at same time? – Drona Tomar Dec 31 '18 at 11:50
  • at the same time.. when the user tape on the button it always go to the next activity but when the user tape the same buton second time then the ad must show. – Sufyan Hashmi Dec 31 '18 at 11:54
  • @Sufyan Hashmi I have edited the code. Please review. – Drona Tomar Dec 31 '18 at 12:02
  • I am facing the problem .Actually I have created a separate class named **ShowInterstitial** for the ads and I think that's why now M facing the **.isLoaded**. And also i edited the question please review. – Sufyan Hashmi Dec 31 '18 at 12:15
  • @Sufyan Hashmi Please share your ShowInterstitial Class. – Drona Tomar Dec 31 '18 at 12:34
  • Now See my question again – Sufyan Hashmi Dec 31 '18 at 12:46
  • review my question plz – Sufyan Hashmi Dec 31 '18 at 16:18
  • sorry bro I was gone offline for little while.. actually your code did worked but now when the user enters the activity and when press back key then the ads is showing but i did not set it onBackPress button..I want as the user enters then the ad should popuot. – Sufyan Hashmi Jan 16 '19 at 04:29
2

How to show the interstitial ads on second click in android?

You can take boolean variable and manage click event based on that boolean variable

Example : take a boolean variable with true value Than inside ClickListener when user clicks the button check that boolean variable is true means use clicked first time the button and change the value of boolean variable to false

SAMPLE CODE

take one boolean variable

boolean isFirstTimeClick=true;

Now make your ClickListener like this

    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(isFirstTimeClick){
                isFirstTimeClick=false;
                Toast.makeText(PicturePreviewActivity.this, "First Time Click", Toast.LENGTH_SHORT).show();
            }else {
                isFirstTimeClick=true;
                Toast.makeText(PicturePreviewActivity.this, "Second Time Click", Toast.LENGTH_SHORT).show();
            }
        }
    });
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • I did this and paste it in my code but didn't work.. here is my code if(view.getId() == R.id.ll1 && isFirstTimeClick) { isFirstTimeClick=true; Intent intent = new Intent(this, AggregatesActivity.class); startActivity(intent); showInterstitial.showInterstitial(); – Sufyan Hashmi Dec 31 '18 at 11:06
  • @SufyanHashmi please update your code in question so i can try to heplp you – AskNilesh Dec 31 '18 at 11:10
  • see my question – Sufyan Hashmi Dec 31 '18 at 11:40
  • @SufyanHashmi share your whole code and also explain where you want to logic for second code – AskNilesh Dec 31 '18 at 11:41
  • I just want to show on second click... It's working fine on single click or by default but I want to add some piece of code to show it on second click.. – Sufyan Hashmi Dec 31 '18 at 11:44
  • @SufyanHashmi have u read my answer i have already added solution for it and its working fine for me – AskNilesh Dec 31 '18 at 11:45