Showing Native AD in Recycler View Android

Showing Native AD in Recycler View Android

Showing AD to our application are one of the best ways to generate some revenue from your app.Show here we are going to implement a native AD to our application. But we before that it is better to have a look at the Google Ad Policy and Recommendation.

If you want to learn more about why native ad then can do some googling, In short I can say that native ad is more impressive and make more impact to user.

In our earlier tutorial we have create a Video Gallery app today we will learn how to implement native ad to our Video Gallery app. Video Gallery Demo app were available at Google Play store you can check out it so you have an idea before going to work.

Creating a AD is an easy steps but putting at right place is an art and you have done some experiment on this to display the ad at right place in your app. But real question is that what we required to implement the new ad to our application. We required two things as given below.

1. You need to create account on admob.
2. You also required an account on Google Firebase because it is now official google ad library.

I hope you have done with above to steps and you have working account on admob and Google Firebase both.

Create Project In Firebase:-

Go to Firebase home page and tab on create project button.

Native Ad
Scroll down to left bottom and click on the Admob button inside earn segment. Here you will get the option to signup for admob and then follow the further instruction to create the admob account or sign in.  Here you can learn more about implementing the firebase to your project.
Create a Ad Unit and select type as Native ad, select size of the add and validate and save it.

Create the Layout Files to Show the Native Ad

Ad the adview to your layout file in header.xml file

 <?xml version="1.0" encoding="utf-8"?>  
 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   xmlns:card_view="http://schemas.android.com/tools"  
   xmlns:ads="http://schemas.android.com/apk/res-auto"  
   card_view:cardCornerRadius="1dp"  
   android:layout_marginBottom="0dp"  
   android:layout_marginTop="0dp"  
   android:layout_marginLeft="0dp"  
   android:layout_marginRight="0dp">  
 <LinearLayout  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical">  
   <com.google.android.gms.ads.NativeExpressAdView  
     android:id="@+id/adView"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_gravity="center_horizontal"  
     ads:adSize="320x150"  
     ads:adUnitId="@string/ad_unit_id"  
     android:visibility="gone">  
   </com.google.android.gms.ads.NativeExpressAdView>  
   <TextView  
     android:id="@+id/gallery_title"  
     android:layout_width="match_parent"  
     android:layout_height="48dp"  
     android:text="Video"  
     android:layout_gravity="left"  
     android:gravity="left"  
     android:textSize="16sp"  
     android:background="@color/lightgray"  
     android:textColor="@android:color/primary_text_light"  />  
 </LinearLayout>  
 </android.support.v7.widget.CardView>  

Modify the header view of the adapter.java to show the AD

Next, Modify your header view class in your adapter.java file as given below.

  public class headView extends RecyclerView.ViewHolder {  
     protected TextView vName;  
     protected Context context;  
     protected  Bundle b;  
     protected int position;  
    // protected AdView adView;  
     public headView(View v) {  
       super(v);  
       vName = (TextView) v.findViewById(R.id.gallery_title);  
       final NativeExpressAdView adView = (NativeExpressAdView) v.findViewById(R.id.adView);  
       adView.loadAd(new AdRequest.Builder().build());  
       adView.setAdListener(new AdListener() {  
         @Override  
         public void onAdLoaded() {  
           super.onAdLoaded();  
           adView.setVisibility(View.VISIBLE);  
         }  
         @Override  
         public void onAdOpened() {  
           super.onAdOpened();  
         }  
         @Override  
         public void onAdLeftApplication() {  
           super.onAdLeftApplication();  
         }  
         @Override  
         public void onAdFailedToLoad(int i) {  
           super.onAdFailedToLoad(i);  
           adView.setVisibility(View.GONE);  
         }  
         @Override  
         public void onAdClosed() {  
           super.onAdClosed();  
           adView.setVisibility(View.GONE);  
         }  
       });  
     }  
   }  

You can download the source code from https://github.com/debugandroid/NativeAd

Get Email Updates!

Signup now receive an email once I publish new content. I will never give away, trade or sell your email address. You can unsubscribe at any time.

Join 911 other subscribers

This Post Has 2 Comments

Leave a Reply

Close Menu