RecyclerViewAd is an example application that displays an embedded AdView inside a RecyclerView. Example App on the Google Play Store:


First you need to add the required dependencies:

dependencies {
    compile ''
    compile ''

How to implement AdView in a RecyclerView?

First in your Activity, instantiate a RecyclerView as normal:

    protected void onCreate(Bundle savedInstanceState) {

        String[] data = {"ksjdahfjak;","feawjkhfew","flosujfeka","hjgweafiwaf",
                        "oneahfewa", "mowaforw", "hfejwafewa","fewalfuewaf"};

        mRV = (RecyclerView) findViewById(;
        adapter = new MyAdapter(this, data);
        mRV.setLayoutManager(new LinearLayoutManager(this));


Our RecyclerView will only contain one TextView for each row. However, you could do interesting things inside the RecyclerView adapter like embed native ads (something like Twitter or Instagram).

Next you need to define a custom adapter for the RecyclerView. I called mine MyAdapter (Reference:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;
    private LayoutInflater inflater;
    private Activity mainActivity;
    int AD_TYPE = 0;
    int CONTENT_TYPE = 1;

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ViewHolder(View v) {
            if (!(itemView instanceof AdView)) {
                mTextView = (TextView) v.findViewById(;

    public MyAdapter(Context context, String[] myDataset) {
        inflater = LayoutInflater.from(context);
        mDataset = myDataset;
        mainActivity = (Activity) context;


In the custom Adapter we can choose the frequency of AdViews that are being displayed by Overriding getItemViewType. Below I set the freqency of adviews at 1 every 6 rows starting at the 4th row.

    public int getItemViewType(int position) {
        if (position % 6 == 3)
            return AD_TYPE;
        return CONTENT_TYPE;

To make sure we don’t crash our app when we assign values to the rows do the following. This will make sure we are only assigning values to valid views.

    public void onBindViewHolder(ViewHolder holder, int position) {
        if(position % 6 != 3) {

Now in the onCreateViewHolder method we need to make sure we load everything correctly. Be sure to add a strings.xml file in your res/values directory so that the getString(R.string.aduid) works!

    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        AdView adview;
        ViewHolder holder;

        if (viewType == AD_TYPE) {
            adview = new AdView(mainActivity);

            // this is the good adview

            float density = mainActivity.getResources().getDisplayMetrics().density;
            int height = Math.round(AdSize.BANNER.getHeight() * density);
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, height);

            // dont use below if testing on a device
            // follow to setup testing device
            AdRequest request = new AdRequest.Builder().build();
            holder = new ViewHolder(adview);

            View view = inflater.inflate(R.layout.custom_row, parent, false);
            holder = new ViewHolder(view);
        return holder;

And now you should have a nice AdView inside your RecyclerView!