简体   繁体   中英

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?

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!

You need to create your own custom list view and custom adapter.

  1. Create a scroll view and a LinearLayout as child
  2. Create your template as xml layout that will handle your content
  3. Create a class that will in Inflat your Layout to The LinearLayout In the ScrollView . Like this :

     private static void CreateIntemEmptty(Context context, ViewGroup view) { LayoutInflater inflater = (LayoutInflater) Home.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View child = inflater.inflate(R.layout.empty, null); ViewGroup Prnt =(ViewGroup)view.getParent().getParent(); Prnt.removeViewInLayout((View)view.getParent()); ViewGroup.LayoutParams p =new ViewGroup.LayoutParams(Prnt.getWidth(),Prnt.getHeight()); Prnt.addView(child,p); } 

    in this code i create an empty Element and add it

  4. You can add the ADS in the LinearLayout as a child.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM