How to implement Admob Native Express AdView in Listview and RecyclerView on Android Devices ?

In this tutorial, we are going to find out how to implement admob native express adview in a recycler and a listview.  We will place a native ad after a specific number of items is listed on a recycler or listview.

Native ads are more catchy and attractive for users which result in more click conversions inside your application or game.

 

Admobadapter – It is an Android library that makes it easy to integrate Admob native ads (both Express and Advanced) into ListView/RecyclerView in the way that is shown in the following image/animation.We use this particular open source library for the easy implementation, and so we can avoid lots of unwanted codes and algorithms inside our app and let it do the hard work for us.

We use this particular open source library for the easy implementation, and so we can avoid lots of unwanted codes and algorithms inside our app and let it do the hard work for us.

ListView

Step 1

Import admobadpter lib into project gradle

 

dependencies 
{
    compile 'com.github.clockbyte:admobadapter:1.4.1'
}

Step 2 – (We are skipping the XML design part here )

In your activity code where you initialize listview and adpater.

 ListView listad;
 AdmobAdapterWrapper adapterWrapper;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_listview);
        MobileAds.initialize(getApplicationContext(), getString(R.string.test_admob_app_id));

        listad= (ListView) findViewById(R.id.Listadview);    // Listview name

        //creating your adapter, it can also be a custom adapter as well
        ArrayAdapter<String> adapter  = new ArrayAdapter<String>(this,
                android.R.layout.Listitem);

       	DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    dpWidth = displayMetrics.widthPixels / displayMetrics.density;           // it is used to avoid the size issue for native ads (if not enough space it won't load)
    adapterWrapper = new AdmobExpressAdapterWrapper(getContext(), getString(R.string.nativead), new AdSize((int)dpWidth-4, 350));

        //wrapping your adapter with a AdmobAdapterWrapper.
        adapterWrapper.setAdapter(adapter); 
        
       
        //Ad count limit
        adapterWrapper.setLimitOfAds(10);  
        //Number of intervals between ads
        adapterWrapper.setNoOfDataBetweenAds(5);
        //Set Ad block index (only display first ad after predefined number of items)
        adapterWrapper.setFirstAdIndex(2);

        listad.setAdapter(adapterWrapper); // setting an AdmobAdapterWrapper to a ListView

        //preparing the collection of data
        final String sItem = "item #";
        ArrayList<String> lst = new ArrayList<String>(100);
        for(int i=1;i<=100;i++)
            lst.add(sItem.concat(Integer.toString(i)));

        //adding a collection of data to your adapter and rising the data set changed event
        adapter.addAll(lst);
        adapter.notifyDataSetChanged();
    }
    
     /*
    * Seems to be a good practice to destroy all the resources you have used earlier 🙂
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        adapterWrapper.release();
    }
}

Or if you have a custom adapter use this

adapterWrapper.setAdapter(postListAdapter);
    adapterWrapper.setLimitOfAds(600);
    adapterWrapper.setNoOfDataBetweenAds(6);
    adapterWrapper.setFirstAdIndex(2);
    listView.setAdapter(adapterWrapper);
    postListAdapter.addAll(posts);
    postListAdapter.notifyDataSetChanged();

RecyclerView

RecyclerView Recyclerlist;
    AdmobRecyclerAdapterWrapper adapterWrapper;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_recycleview);
        MobileAds.initialize(getApplicationContext(), getString(R.string.test_admob_app_id));

        
        Recyclerlist= (RecyclerView) findViewById(R.id.recycler);
        Recyclerlist.setLayoutManager(new LinearLayoutManager(this));

        //creating your own adapter
         RecyclerExampleAdapter adapter  = new RecyclerExampleAdapter(this);

       DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    
    dpWidth = displayMetrics.widthPixels / displayMetrics.density;
    adapterWrapper = new AdmobExpressAdapterWrapper(getContext(), getString(R.string.nativead), new AdSize((int)dpWidth-4, 350));

        //wrapping your adapter with a AdmobAdapterWrapper.
        adapterWrapper.setAdapter(adapter);
        adapterWrapper.setLimitOfAds(10);
        adapterWrapper.setNoOfDataBetweenAds(5);
        adapterWrapper.setFirstAdIndex(2);
        adapterWrapper.setViewTypeBiggestSource(100);

        Recyclerlist.setAdapter(adapterWrapper); // setting an AdmobRecyclerAdapterWrapper to a RecyclerView

        //preparing the collection of data
        final String sItem = "item #";
        ArrayList<String> lst = new ArrayList<String>(100);
        for(int i=1;i<=100;i++)
            lst.add(sItem.concat(Integer.toString(i)));

        //adding a collection of data to your adapter and rising the data set changed event
        adapter.addAll(lst);
        adapter.notifyDataSetChanged();
    }
    

    @Override
    protected void onDestroy() {
        super.onDestroy();
        adapterWrapper.release();
    }
}

Link to the library  – https://github.com/clockbyte/admobadapter

For advanced tutorials Checkout this article  –  https://github.com/clockbyte/admobadapter/wiki/Cookbook