Anish Jain
Anish Jain

Reputation: 231

How can I show Interstitial ad from Non Activity Class?

I have a non activity class named custom adapter which shows images on main activity which loads images from the internet and sets wallpapers on click I want to show interstitial ad from Non Activity class,(CustomAdapter) when image view is clicked Tried many stuff but doesn't work out , plz help me out

Custom Adapter.java

    class CustomViewHolder extends  RecyclerView.ViewHolder {

    RibbonLayout ribbonLayout;
    ImageView imageView;

    public CustomViewHolder(View itemView) {
        super(itemView);

        ribbonLayout = (RibbonLayout)itemView.findViewById(R.id.ribbonLayout);
        imageView = (ImageView)itemView.findViewById(R.id.imageView);
    }

    }

    public class CustomAdapter extends  RecyclerView.Adapter<CustomViewHolder> {

    Context context;

    List<Item> itemList;

    public CustomAdapter(Context context, List<Item> itemList) {
        this.context = context;
        this.itemList = itemList;
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
        return new CustomViewHolder(itemView);

    }

    @Override
    public void onBindViewHolder(CustomViewHolder holder, int position) {
        Item item = itemList.get(position);
        if (item.type == 0) { //new
            holder.ribbonLayout.setShowBottom(false);
            holder.ribbonLayout.setShowBottom(false);


            holder.ribbonLayout.setHeaderRibbonColor(Color.parseColor("#2B323A"));
            holder.ribbonLayout.setHeaderTextColor(Color.parseColor("#FFFFFF"));

            holder.ribbonLayout.setHeaderText((item.headerText));
            Picasso.with(context).load(item.imageURL)
                    .into(holder.imageView);
            holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        Bitmap bitmap = ((BitmapDrawable) ((ImageView) view).getDrawable()).getBitmap();
                        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
                        wallpaperManager.setBitmap(bitmap);

                        Toast.makeText(context, " \\ (•◡•) /Yay! Wallpaper Set \\ (•◡•) / ", Toast.LENGTH_LONG).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }
}

Upvotes: 2

Views: 3146

Answers (1)

Rahulrr2602
Rahulrr2602

Reputation: 721

You can call an Interstitial Ad like this from a Non-Activity Class

  Custom Adapter.java

        class CustomViewHolder extends  RecyclerView.ViewHolder {

        RibbonLayout ribbonLayout;
        ImageView imageView;

        public CustomViewHolder(View itemView) {
            super(itemView);

            ribbonLayout = (RibbonLayout)itemView.findViewById(R.id.ribbonLayout);
            imageView = (ImageView)itemView.findViewById(R.id.imageView);
        }

        }

        public class CustomAdapter extends  RecyclerView.Adapter<CustomViewHolder> {

        Context context;

        List<Item> itemList;

        public CustomAdapter(Context context, List<Item> itemList) {
            this.context = context;
            this.itemList = itemList;
        }

        @Override
        public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
            return new CustomViewHolder(itemView);

        }

        @Override
        public void onBindViewHolder(CustomViewHolder holder, int position) {
            Item item = itemList.get(position);
            if (item.type == 0) { //new
                holder.ribbonLayout.setShowBottom(false);
                holder.ribbonLayout.setShowBottom(false);


                holder.ribbonLayout.setHeaderRibbonColor(Color.parseColor("#2B323A"));
                holder.ribbonLayout.setHeaderTextColor(Color.parseColor("#FFFFFF"));

                holder.ribbonLayout.setHeaderText((item.headerText));
                Picasso.with(context).load(item.imageURL)
                        .into(holder.imageView);
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            Bitmap bitmap = ((BitmapDrawable) ((ImageView) view).getDrawable()).getBitmap();
                            WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
                            wallpaperManager.setBitmap(bitmap);

                            Toast.makeText(context, " \\ (•◡•) /Yay! Wallpaper Set \\ (•◡•) / ", Toast.LENGTH_LONG).show();
 Activity.ShowAd();//Hope you want to call the ads from here.
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return itemList.size();
        }
    }

And this is your Activity Class

    Activity.java

    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.InterstitialAd;

    public class MainActivity extends Activity {

        public static InterstitialAd mInterstitialAd;

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

            MobileAds.initialize(this,
                "ca-app-pub-3940256099942544~3347511713");

            mInterstitialAd = new InterstitialAd(this);
            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() {
        // Code to be executed when when the interstitial ad is closed.
    }
});
            // Make sure to set the adapter after the above code.
        }

      public static void ShowAd()
        {
           if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
            } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    }

Note- 1) Change all the ad Ids as these are the test Ids. 2) Call the set adapter code after requesting for the Interstitial Ads 3) Also, see that the time taken by the user is sufficient so that the ads is loaded.

Upvotes: 3

Related Questions