Skip to main content

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

Comments

  1. […] May Like this:- Showing Native AD In Recycler View Android Step by Step Guide to create option Menu in […]

    ReplyDelete
  2. […] Read:- Showing Native AD In Recycler View Android Step by Step Guide to create option Menu in […]

    ReplyDelete

Post a Comment

Popular posts from this blog

Flutter How to Start Android Activity from Flutter View

Flutter and Dart is an excellent combination for creating the UI, but for accessing the platform-specific service we need to open platform-specific activity. So lets in this article we will explore how to start an android activity and access the service from Flutter View. Create a Project for this Android Activity Flutter View Demo Create a Project From File menu select the New Flutter Project Enter the project name Select the AndroidX support and click on next After the above, we step click on Finish We will have the following project structure created. Create the Second Activity in Android Just go to the android folder and open it in separate windows. We will have the following project structure. Create the Activity Just right-click on the Kotlin folder and create a blank activity from the menu. If you create the activity then you may be required to upgrade the Gradle and do some import. So Just click on update and wait for the project s

Kotlin Parcelable Array Objects Send To Activity

We know that IPC (Inter Process Communication) between the activity is an extremely important part of any application development. We often required that we need to send some data to other activity. For example, we may be required to send an array of data, data could be an Integer, String, Long, Double, Float or any other custom data objects. So, In this example, we are going to learn how to implement the Kotlin Parcelable Array object to send the data from one activity to second activity. What is Parcel? The parcel class is designed as a high-performance IPC transport. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC, and references to live IBinde r objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel. Create Kotlin Parcelable Array Objects Parcelable is API for placing the arbitrary objects into the Parcel. In Actual in android app development, Parcelable is an interface

Scan and List All Available WiFi Network In Android

In this tutorial, we learn how to connect to a WiFi hotspot in android by code. In this Demo we will create an as small app which will scan the all available network or Hotspot and list down the network when you select specific network, the application will connect that particular network. You May Like Below Topic: How to Read and Write JSON data using GSON WP Android App using REST and volley WP Android App using REST and volley part2 Implementation of SwipeRefreshLayout in RecyclerView Create Amazing Bottom Navigation Bar Without Any External Library Introduction In this tutorial, we learn how to connect to a WiFi hotspot in android by code. In this Demo we will create an as small app which will scan the all available network or Hotspot and list down the network when you select specific network, the application will connect that particular network. We will add this functionality to our existing Demo app " Video Gallery ". If you would like to check out t