Skip to main content

Flutter State Management With Provider and ChangeNotifier

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 sy

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 Ther

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 o