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 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…

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 State Management With Provider and ChangeNotifier

Flutter state management with provider library and why it's so important? There are lots of topics in flutter which are very important but out of those state management of your flutter application is the most important and little bit complicated as well. So in this article, I will try to explain how to use the provider package for managing the state of flutter and dart application and how to access the data down the hierarchy of the widget tree without recreating the whole UI.



Basic knowledge of the state management and data flow in the Flutter



Before starting the coding I would like to explain some basics about the Flutter application so that you can understand, why it's important to manage the state of your application in the correct way.



In Flutter, everything is a widget and whenever the state of your application changes the UI is recreated the UI with the new data. Let's list out the topic or problem that we are going to explore about state management.



Challenge of St…