Putting an AdMob native ad in a listView Putting an AdMob native ad in a listView android android

Putting an AdMob native ad in a listView


Well @Jagjit has shown the right approach. I will write it step by step
1. Create your own custom adapter (by extending BaseAdapter) which will be shown as listview items
2. Create another layout resource file for showing native ad (design should be similar to the custom adapter created in above step)
3. In the getView method do something as follows (for showing ad at 2nd position)

if (position == 1) {    rowView = inflater.inflate(R.layout.native_ad_adapter, null);    NativeExpressAdView adView = (NativeExpressAdView)rowView.findViewById(R.id.adView);    AdRequest adRequest = new AdRequest.Builder().build();    adView.loadAd(adRequest);} else {    rowView = inflater.inflate(R.layout.my_custom_list, null);    TextView bigtxt = (TextView) rowView.findViewById(R.id.txt1);    ...    ...}

Here your custom adapter is my_custom_list
You will see the ads are loading. But there is a small problem, the actual item of list at position 1 will not be shown.
4. For that, add an extra item to the list before populating the ListView. I have used ArrayList so I could do it easily. My approach is as follows

arrayList1.add(1,"ad here");listview1.setAdapter(new MyAdapter(callerView.getContext(), arrayList1.toArray(new String[arrayList1.size()])));


The approach is to extend BaseAdapter and create your own custom Adapter, instead of using the default ArrayAdapter. Then you need to return an “Ad” View instead of your usual normal View when you want to display an ad. This means that you need to override getCount() to return more rows (for example if you have 10 rows, that means you need to return 11 = 10 actual content + 1 ad)

Then you need to decide in which position to create this View, I think you can do it by simply checking the position variable:

if (position == VALUE) {   // Create and return Ad View } else {   // Create and return a normal View }

Hope it helps. I found this as best till yet hope to find better optimised way for this


I guess Admobadapter lib is what you are looking for...it wraps your adapter to automatically fetch and show ad blocks in every X items in your listview or recyclerview. You just do it as follows: 1. setup wrapper of suitable type (there are basically 4 wrappers - for a listview, for a recyclerview to show Advanced ads and 2 ones for Express ads) 2. inject your adapter into the wrapper 3. inject wrapper to the listview/recyclerview 4. fill up your adapter with your data, raise onDataSetChanged() then the wrapper will do the stuff itself.

Also it supports Express native ads and Advanced native ads as well. According to this issue they are going to add a Maven/Gradle support soon. Good luck!