Search
Close this search box.

How To Insert AdMob Native Ad In your Android App

In this article, I will show you how to insert Native Ad code in your Android Application.

But before that let me give you a brief idea about what is Native Ad.

Native Ad is one of the Ad types of AdMob which help you to generate revenue by displaying Ads on your App. To learn more about AdMob and Native Ad, click on the link provided below.

What is AdMob and how to generate Ad unit ID?

There are total four types of AdMob Ads through which you can earn money. You can click on the link below to learn more about other types of AdMob Ads.

  1.  Banner Ad
  2.  Native Ad
  3.  Rewarded Ad
  4.  Interstitial Ad

Native Ads can be inserted between ListView or CardView or any List item, at any place. It totally depends on you where you want to display the Native Ad in your application.

Now, let’s get started with how to place the code of Native Ad in your Android Application. I have assumed that you are using Android Studio for developing your app and your project is opened in it. Follow the below steps and copy paste the code into your project carefully.

⟩⟩  First of all, you need to add below line of code in your build.gradle dependencies file.

compile 'com.google.firebase:firebase-ads:10.2.0'

⟩⟩  Then you need to add below line of code in your build.gradle (Project) dependencies file.

classpath 'com.google.gms:google-services:3.0.0'

after adding the above codes you will get a sync now option at the top right corner of the page. Click on that and wait for a few minutes to complete the process, once the gradle building is finished you are ready to go with the next steps.

⟩⟩ Then open the Manifest file and add Internet permission to it.

<uses-permission android:name="android.permission.INTERNET" />

⟩⟩  In project structure, navigate to activity_main.xml and paste the following code in your layout.

<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="320x300"
ads:adUnitId="@string/ad_unit_id">
</com.google.android.gms.ads.NativeExpressAdView>

and in the same file i.e activity_main.xml add below lines of code in the header part

xmlns:ads="http://schemas.android.com/apk/res-auto"

⟩⟩  Now open MainActivity.java and add below lines of code in public class

private static String LOG_TAG = "EXAMPLE";
NativeExpressAdView mAdView;
VideoController mVideoController;

⟩⟩  Then under MainActivity.java and add below lines of code in onCreate() method.

// Locate the NativeExpressAdView.
mAdView = (NativeExpressAdView) findViewById(R.id.adView);

// Set its video options.
mAdView.setVideoOptions(new VideoOptions.Builder()
        .setStartMuted(true)
        .build());

// The VideoController can be used to get lifecycle events and info about an ad's video
// asset. One will always be returned by getVideoController, even if the ad has no video
// asset.
mVideoController = mAdView.getVideoController();
mVideoController.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
    @Override
    public void onVideoEnd() {
        Log.d(LOG_TAG, "Video playback is finished.");
        super.onVideoEnd();
    }
});

// Set an AdListener for the AdView, so the Activity can take action when an ad has finished
// loading.
mAdView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        if (mVideoController.hasVideoContent()) {
            Log.d(LOG_TAG, "Received an ad that contains a video asset.");
        } else {
            Log.d(LOG_TAG, "Received an ad that does not contain a video asset.");
        }
    }
});

mAdView.loadAd(new AdRequest.Builder().build());

⟩⟩  Now open string.xml file in values folder and paste the below line of code.

<string name="ad_unit_id">ca-app-pub-39402560999xxxxx/21772xxxxx</string>

Native AdMob ID used here is a sample ID. In order to get revenue, you need to replace this test ID with your own Native Ad ID.

If you don’t know how to get your own Native Ad Id then click here how to get Native Ad Unit ID.

Note: Don’t use your real Ad Unit ID while testing and never click on your own Ads it is against AdMob Policy.

That’s it, friends, this is the correct and simple way to insert Native Ad in your Application. Just follow the above steps and copy paste the code carefully at the correct location mentioned above.

If you are a beginner you will love to learn Android Application development from scratch.

If you found this article helpful please do comment below and share the article to support me. If you have any suggestion please feel free to comment below.

Sign up for our Newsletter

Fastcomet

Award winning hosting company for your website

Elementor

Best page builder for creating a stunning website

Astra

The most popular theme among webmasters

We use cookies for essential website functions and to better understand how you use our site, so we can create the best possible experience for you ❤️

elegantespace logo png

How to insert AdMob Native Ad in your Android App (Write Your Opinion)