Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting an AdMob native ad in a listView

I'd like to use the new AdMob native ad functionality in my app. I want to put a native ad within a listView, where every 20th item or so in the list is an ad. Is it possible to achieve this with a Native Express Ad? Or do I have to use a Natve Advanced Ad?

like image 936
AcFreeman Avatar asked Jun 01 '16 21:06

AcFreeman


People also ask

How can I see native ads on Android?

Load an Ad. Native ads are loaded via the AdLoader class, which has its own Builder class to customize it during creation. By adding listeners to the AdLoader while building it, an app specifies which types of native ads it is ready to receive. The AdLoader then requests just those types.

What is native ads Admob?

Native ads match both the form and function of the user experience in which they're placed. They also match the visual design of the app they live within. These ads augment the user experience by providing value through relevant content that flows within the context of surrounding app content.


3 Answers

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()])));
like image 108
Bibaswann Bandyopadhyay Avatar answered Nov 15 '22 18:11

Bibaswann Bandyopadhyay


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

like image 27
Jagjit Singh Avatar answered Nov 15 '22 17:11

Jagjit Singh


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!

like image 35
kot331107 Avatar answered Nov 15 '22 17:11

kot331107