Chinmay
Chinmay

Reputation: 55

Implement facebook banner ads in recyclerview

I am trying to implement an Facebook banner ad(not native banner) on 3rd position of the recyclerview. I have created an ads_row.xml which has the ad layout. ads_row.xml is displayed properly but ad is not visible. I will post my adapter code below.

I would appreciate if someone could give me advice about which approach i should use in my case to show ads in recyclerview.

Happy Coding

NewsAdapter

public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

private List<LinkedTreeMap> newsLists;
private Context context;
private LayoutInflater inflater;
AdView adView;
int AD_TYPE = 0;
int CONTENT_TYPE = 1;

public NewsAdapter(List<LinkedTreeMap> newsLists, Context context) {
    inflater = LayoutInflater.from(context);
    this.newsLists = newsLists;
    this.context = context;

}

public class PostViewHolder extends RecyclerView.ViewHolder {

    public TextView textView;
    public ImageView imageView;
    public TextView description;

    public PostViewHolder(@NonNull View itemView) {
        super(itemView);

        textView = (TextView) itemView.findViewById(R.id.textView);
        imageView = (ImageView) itemView.findViewById(R.id.image_ml);
        description = (TextView) itemView.findViewById(R.id.description);
    }
}

public class AdViewHolder extends RecyclerView.ViewHolder {
    private LinearLayout adContainer;
    public AdViewHolder(@NonNull View itemView) {
        super(itemView);
        adContainer  = (LinearLayout) itemView.findViewById(R.id.myList_fb_banner);
        adContainer.addView(adView);
    }
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
     if(viewType == CONTENT_TYPE){
         View v = inflater.inflate(R.layout.mylist, parent, false);
         return new PostViewHolder(v);
     }else{
         View v = inflater.inflate(R.layout.ads_row, parent, false);
         AudienceNetworkAds.initialize(context);

         AdSettings.setIntegrationErrorMode(INTEGRATION_ERROR_CRASH_DEBUG_MODE);
         adView = new com.facebook.ads.AdView(context, "IMG_16_9_APP_INSTALL#MYPLACLEMENTID", AdSize.BANNER_HEIGHT_90);
         adView.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

         if (adView == null){
             adView.setVisibility(View.GONE);
         }
         return new AdViewHolder(v);
     }
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    if (getItemViewType(position) == CONTENT_TYPE){
        LinkedTreeMap newsList = newsLists.get(position);
        if (holder instanceof PostViewHolder){
            PostViewHolder viewholder = (PostViewHolder) holder;
            viewholder.textView.setText(newsList.get("title").toString());
            if (!newsList.get("image").toString().isEmpty()) {
                viewholder.imageView.setVisibility(View.VISIBLE);
                Picasso.get()
                        .load(newsList.get("image").toString())
                        .into(viewholder.imageView);
            } else {
                viewholder.imageView.setVisibility(View.GONE);
            }
        }

    }else if(getItemViewType(position) == AD_TYPE){
        if (holder instanceof AdViewHolder){
            AdViewHolder adViewholder = (AdViewHolder) holder;
            if (adViewholder.adContainer.getParent() != null){
    ((ViewGroup)adViewholder.adContainer.getParent()).removeView(adViewholder.adContainer);
            }
            adViewholder.adContainer.addView(adView);
            adView.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
            if (adView == null){
                adView.setVisibility(View.GONE);
            }
            if (position == newsLists.size()-3) {
                adView.setVisibility(View.VISIBLE);
                if (adView != null){
                    adView.loadAd();
                }
            }
        }

    }
}

Upvotes: 2

Views: 626

Answers (1)

Jenea Vranceanu
Jenea Vranceanu

Reputation: 4694

A few issues I noticed.

  1. Your app will crash if adView is null. You cannot manipulate attributes of an object if it is null:
if (adView == null){
    adView.setVisibility(View.GONE);
}

Most likely you want this statement removed totally.

  1. Position calculation is wrong. You are saying that you want to load an ad in the third row of a recycler view items. This expression newsLists.size() - 3 won't give you the index of a third row:
if (position == newsLists.size()-3) {
    adView.setVisibility(View.VISIBLE);
    if (adView != null){
        adView.loadAd();
    }
}

Index of the third row is always 2. Enumeration of indices starts from 0 as the first index.

Update this piece of code to be:

if (position == 2) {
    adView.loadAd();
}
  1. There is no getItemViewType method override. Or at least it is not the question.

I've updated your adapter. You can try to copy-paste it into your application to check how it's working.

public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private List<LinkedTreeMap> newsLists;
    private Context context;
    private LayoutInflater inflater;

    int AD_TYPE = 0;
    int CONTENT_TYPE = 1;

    public NewsAdapter(List<LinkedTreeMap> newsLists, Context context) {
        inflater = LayoutInflater.from(context);
        this.newsLists = newsLists;
        this.context = context;

        // AudienceNetworkAds should be initialized inside of Activity, Fragment or even better Application class
        AudienceNetworkAds.initialize(context);
        AdSettings.setIntegrationErrorMode(INTEGRATION_ERROR_CRASH_DEBUG_MODE);
    }

    public class PostViewHolder extends RecyclerView.ViewHolder {

        public TextView textView;
        public ImageView imageView;
        public TextView description;

        public PostViewHolder(@NonNull View itemView) {
            super(itemView);

            textView = (TextView) itemView.findViewById(R.id.textView);
            imageView = (ImageView) itemView.findViewById(R.id.image_ml);
            description = (TextView) itemView.findViewById(R.id.description);
        }
    }

    public class AdViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout adContainer;
        private AdView adView;
        
        public AdViewHolder(@NonNull View itemView) {
            super(itemView);
            adContainer = (LinearLayout) itemView.findViewById(R.id.myList_fb_banner);
            
            adView = new com.facebook.ads.AdView(context, "IMG_16_9_APP_INSTALL#MYPLACLEMENTID", AdSize.BANNER_HEIGHT_90);
            adView.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
            adContainer.addView(adView);
        }
        
        public void loadAd() {
            adView.loadAd();
        }
    }

    @Override
    public int getItemViewType(int position) {
        // Ad only at the third row
        return position == 2 ? AD_TYPE : CONTENT_TYPE;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == CONTENT_TYPE) {
            View view = inflater.inflate(R.layout.mylist, parent, false);
            return new PostViewHolder(view);
        } else {
            View view = inflater.inflate(R.layout.ads_row, parent, false);
            return new AdViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == CONTENT_TYPE) {
            LinkedTreeMap newsList = newsLists.get(position);
            if (holder instanceof PostViewHolder) {
                PostViewHolder viewholder = (PostViewHolder) holder;
                viewholder.textView.setText(newsList.get("title").toString());
                if (!newsList.get("image").toString().isEmpty()) {
                    viewholder.imageView.setVisibility(View.VISIBLE);
                    Picasso.get()
                            .load(newsList.get("image").toString())
                            .into(viewholder.imageView);
                } else {
                    viewholder.imageView.setVisibility(View.GONE);
                }
            }
        } else if (getItemViewType(position) == AD_TYPE && holder instanceof AdViewHolder) {
            ((AdViewHolder) holder).loadAd();
        }
    }
}

Upvotes: 3

Related Questions