2

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();
    }
}
8
  • Pass the context from Activity class to Non-Activity Class while setting the adapter. And the simply use the context whenever you want to show the Ad. Mar 31, 2018 at 11:10
  • Tried many stuff but doesn't work out. Show us what have you tried.
    – Kunu
    Mar 31, 2018 at 11:11
  • @Rahulrr2602 can you please write it out as a answer, Will help other people too
    – Anish Jain
    Mar 31, 2018 at 11:22
  • If something worked for others and not for you then you must be doing something wrong. And unless you show your code and what error are you getting then it is difficult to debug your problem.
    – Kunu
    Mar 31, 2018 at 11:23

1 Answer 1

3

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.

4
  • Thanx it worked, just needed to add Adlistener further
    – Anish Jain
    Mar 31, 2018 at 13:13
  • @AnishJain Added the listener code also. You can let me know if you need any help regarding that also. Mar 31, 2018 at 13:46
  • Nope I did that part, but thanx for adding , it may help some other people looking for answers
    – Anish Jain
    Mar 31, 2018 at 13:59
  • 1
    @AnishJain Glad, it helped you. Mar 31, 2018 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.