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

WordPress Android App with REST API And Retrofit

Retrofit is the library which converts your JSON data into Java object. In this tutorial, we will learn about how to implement the Retrofit to create a WordPress Mobile App. As we all know, WordPress is the one of the biggest CMS (Content Management System) of the world. Wordpress full fill the requirements for all most every type of the website. So, in this tutorial, I am going to learn how to create an Android App using the WordPress REST API and Retrofit.
What is Retrofit?
If you want to know about Retrofit, I would like to give you just a simple one-line explanation of the Retrofit. It is a library which converts your REST HTTP request to Java interface. We will learn how we can do all these stuff using some very simple steps.

Further, you can check the Retrofit Library here.

So, In this tutorial, we will use the Retrofit to retrieve all post and there except ( A short Description of Post) form a WordPress website.
Prerequisite for using the Retrofit for WordPress Android App
There is s…

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 sync completed.
Update the project build config …

Flutter Theme Creation, Programmatic Way

Flutter and Dart is an excellent combination for creating the mobile app with material design with nice GUI. Its lots of widget which help the developer for creating the app UI very fast with respect to the other language. You may face some problem in starting due to comm, curly brace and semicolon but after some time you will find the way how to work with these all stuff and it becomes very easy for you. But in this article, we are not going to discuss all these. In this article, we will handle a very interesting topic on how to create a custom theme or multiple themes and let's user decide which theme to use.



Create a flutter project



So let's create a flutter project by selecting the appropriate option from the File menu in android studio or Visual Code. If you are new to Flutter and Dart you can check out our recent post to get started Creating Cross-platform app using flutter.



Once your project is created you will get the default counter app and you try running the app in the…