Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google AdMod Native Ads with Recycler view

I am writing Android App using recycler view with card view. I need to use Google AdMod Native Ads.

I tried searching for examples and google developer network also, but did not get a concrete solution.

Please help me with sample code or direct me to proper location where I can find this.

like image 444
Thontesh Renukarya Avatar asked Feb 09 '23 01:02

Thontesh Renukarya


1 Answers

Recently I stucked with the same question but I implemented AdMob native ads for the ListView. Then I decided to post my solution for that to admobadapter. Hope it will help you. I suppose it's not very complex to customize my solution for RecyclerView/CardViews. BTW feel free to contribute/fork.

The basic usage could look like:

    ListView lvMessages;
    AdmobAdapterWrapper adapterWrapper;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initListViewItems();
    }

    /**
     * Inits an adapter with items, wrapping your adapter with a {@link AdmobAdapterWrapper} and setting the listview to this wrapper
     * FIRST OF ALL Please notice that the following code will work on a real devices but emulator!
     */
    private void initListViewItems() {
        lvMessages = (ListView) findViewById(R.id.lvMessages);

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

        adapterWrapper = new AdmobAdapterWrapper(this);
        adapterWrapper.setAdapter(adapter); //wrapping your adapter with a AdmobAdapterWrapper.
        //here you can use the following string to set your custom layouts for a different types of native ads
        //adapterWrapper.setInstallAdsLayoutId(R.layout.your_installad_layout);
        //adapterWrapper.setcontentAdsLayoutId(R.layout.your_installad_layout);

        //Sets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
        adapterWrapper.setLimitOfAds(3);

        //Sets the number of your data items between ad blocks, by default it equals to 10.
        //You should set it according to the Admob's policies and rules which says not to
        //display more than one ad block at the visible part of the screen,
        // so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
        adapterWrapper.setNoOfDataBetweenAds(10);

        //It's a test admob ID. Please replace it with a real one only when you will be ready to deploy your product to the Release!
        //Otherwise your Admob account could be banned
        //String admobUnitId = getResources().getString(R.string.banner_admob_unit_id);
        //adapterWrapper.setAdmobReleaseUnitId(admobUnitId);

        lvMessages.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();
    }

And the result will look like this

UPDATE: Integration

You can simply copy the following sources from github

admobadapter/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobAdapterWrapper.java
admobadapter/admobadapter/src/main/java/com/clockbyte/admobadapter/AdmobFetcher.java

to your java sources folder (feel free to edit the package names in all files but please leave the License header as is).

and the following resources

admobadapter/admobadapter/src/main/res/layout/adcontentlistview_item.xml
admobadapter/admobadapter/src/main/res/layout/adinstalllistview_item.xml

to your res/layout folder. Also please don't forget to copy the string resource test_admob_unit_id from admobadapter/admobadapter/src/main/res/values/strings.xml to your strings.xml file.

After implementation all the steps you'll have to change the declaration of the AdmobAdapterWrapper at least like this:

public class AdmobAdapterWrapper extends RecyclerView.Adapter<your ContactViewHolder class> implements AdmobFetcher.AdmobListener {

//...
    private RecyclerView.Adapter<your ContactViewHolder class> mAdapter;

    public RecyclerView.Adapter<your ContactViewHolder class> getAdapter() {
        return mAdapter;
    }

    public void setAdapter(RecyclerView.Adapter<your ContactViewHolder class> adapter) {
//...
}

then I guess you'll have to replace some of the AdmobAdapterWrapper's methods by RecyclerView.Adapter's specific methods, I suppose that it will be enough to replace the getView(...) by onBindViewHolder(...) and onCreateViewHolder(...) so seems this part of job is up to you :) It doesn't seem very hard. Then you can simply use it with your RecyclerView like in the example above :

AdmobAdapterWrapper adapterWrapper = new AdmobAdapterWrapper(this); 
  adapterWrapper.setAdapter(your_recyclerview_adapter_that_will_be_filled_with_your_data); 
recyclerView.setAdapter(adapterWrapper);

If you'll extend AdmobAdapterWrapper for the RecyclerView.Adapter please don't hesitate to contribute/fork to my lib. It will be very appreciated! I'm going to extend the RecyclerView.Adapter wrapper for my lib a bit later but I have no time for it at the moment, sorry...Is there any possibility for you to use a ListView instead of RecyclerView for some time? However if you'll have a questions please don't hesitate to ask me.

like image 146
kot331107 Avatar answered May 08 '23 13:05

kot331107