How to put an Interstital Ad inside an adapter class?

百般思念 提交于 2019-11-29 12:57:28

I have also faced this very same issue. In your case instead of opening interstitial ads from adapter activity, open it from next activity of adapter activity. Below is my Solution: Consider Activity_B as your Adapter activity.

  • Suppose there are 3 activities and opening sequence is as follows:

    Activity_A --> Activity_B --> Activity_C.
    
  • Now I want to show interstitial Ad between Activity_B and Activity_C.

  • I have first loaded the Interstitial Ad in Activity_A and then call(or show) in Activity_C.

You can do above like this:

In Activity_A i.e. MainActivity add code like this:

public void showme(){

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        mInterstitialAd.loadAd(adRequest);

        mInterstitialAd.setAdListener(new AdListener()
        {

            @Override
            public void onAdClosed()
            {
                //reload interstitial
                AdRequest adRequest = new AdRequest.Builder()
//                        .addTestDevice("YOUR_DEVICE_ID")
                        .build();
                mInterstitialAd.loadAd(adRequest);
            }
        });
    }

    public static void showInterstitial() {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }

Call this showme() in Activity_A inside OnCreate.

In Activity_C paste below code inside OnCreate:

Activity_A.showInterstitial();

Also, this method doesn't violate any Google Admob policy of interstitial ads as well as banner ads.

you should do like following code.... Declare this code in adapter class at the top... private InterstitialAd mInterstitialAd;

After this you put the rest of the code in your onBindViewHolder()

@Override
public void onBindViewHolder(@NonNull MyHolder holder, final int position) {

    Picasso.get()
            .load(picsArray[position])
            .into(holder.imageView);

    mInterstitialAd = new InterstitialAd(context);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());

    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            // Code to be executed when an ad finishes loading.
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            // Code to be executed when an ad request fails.
        }

        @Override
        public void onAdOpened() {
            // Code to be executed when the ad is displayed.
        }

        @Override
        public void onAdLeftApplication() {
            // Code to be executed when the user has left the app.
        }

        @Override
        public void onAdClosed() {
            Intent intent = new Intent(context, Full_Image_Activity.class);
            intent.putExtra("IMG",picsArray[position]);
            context.startActivity(intent);

///// onAdClosed() method you can put your intent or what ever you want, this is for when you close the add then the intent must know what to do... } });

}

After this you can put the code in your item.onClicklistner method like below..

itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    Intent intent = new Intent(context, Full_Image_Activity.class);
                    intent.putExtra("IMG",picsArray[getAdapterPosition()]);
                    context.startActivity(intent);
                }

            }
        });

In the else{} you can put your code like intent or whatever you want to do... Hope it will work for many peoples...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!