Google AdMod for mobile ads with Recycler viewing

I am writing an Android application using a recycler view with a map view. I need to use my own Google AdMod ads.

I also tried to find examples and the google developer network, but did not get a specific solution.

Please help me with a sample code or direct me to the right place where I can find it.

+4
source share
1 answer

I recently asked the same question, but I used my own AdMob ads for ListView. Then I decided to publish my solution for admobadapter . Hope this helps you. I believe it is not very difficult to set up my solution for RecyclerView / CardViews. BTW feel free to contribute / plug.

basic use might look like this:

    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 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 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 height and screen resolution of a target devices
        adapterWrapper.setNoOfDataBetweenAds(10);

        //It 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 the sources folder java(feel free to edit the package names in all files, but leave the license header as it is).

and following resources

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

to the folder res/layout. Also do not forget to copy the resource string test_admob_unit_idfrom admobadapter/admobadapter/src/main/res/values/strings.xmlto your file strings.xml.

AdmobAdapterWrapper, , :

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) {
//...
}

, AdmobAdapterWrapper's RecyclerView.Adapter's , , , getView(...) onBindViewHolder(...) onCreateViewHolder(...), :) . RecyclerView, :

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

AdmobAdapterWrapper RecyclerView.Adapter, , / . ! RecyclerView.Adapter lib , , ... ListView RecyclerView ? , , , .

+8

Source: https://habr.com/ru/post/1607175/


All Articles