There are millions of Free Apps on the Play Store with plenty of them containing ads on them.

Need some career advice or prepping for an Android developer interview? Hit me up on Topmate.io, and let's chat!

How to Add Ads in Android App Example
How to Add Ads in Android App Example

If you are planning to build your own Freemium app and integrate it with Google Admob Ads, here is a guide for you on How to Add Ads in Android App Example.

Learn to build your custom Android Keyboard App, Read more.

How to Add Ads in Android App Example

To start with How to Add Ads in Android App Example here is your Step to Step guide:

Open Google Admob website, link here and create a free account.

Once the account is created, Click on ‘Get Started’ button to create your first Google Admob Ad unit.

Google Admob will now ask if you have your App published in Play or App store, select ‘No’ if not.

Enter your App name and Platform, ‘Android’ for Android Studio.

Google Admob App ID is now generated which is a unique App ID for your App which will be added later to the source code.

Now select what kind of Google Admob Ad you want to integrate into your App. Is it Banner, Interstitial or Reward Ad.

Select Banner Ad to generate a Google Admob Banner App ID. On next page, give Ad unit a name.

Click on Advance settings if you want to select between text or Rich media ad and Refresh rate of Ad.

Select ‘Create Ad Unit’ and your Google Admob Banner Ad unit is created.

Similarly, create a Google Admob Interstitial Ad unit for this tutorial.

Now create a new Android Studio project or open your recent Android Studio project in which you want to implement this Google Admob Ad.

Go to Project panel in the left corner and open Gradle Build (Project) and add the following lines of code:


allprojects {
    repositories{
        jcenter()
       maven {
            url"https://maven.google.com"
       }
   }
}

Go to Project Panel again and open Gradle Build (Module) and add following lines of code:

dependencies {
    implementation 'com.google.android.gms:play-services-ads:11.8.0'
}

Now Go to MainActivity.Java file import following:

import com.google.android.gms.ads.MobileAds;

and also add following lines of code in onCreate() method:

// Don't forget to insert your App ID below
// we have inserted Test ID that you can use while testing your App.
MobileAds.initialize(this, "INSERT_APP_ID_HERE");

Explore some Android projects with Source Code to earn more revenue, Read more.

Google Admob Banner Ad Implementation

Google Admon Banner Ad is a rectangular box ad image or text that occupy a Top or bottom location in your Android or iOS App. The user will stay on App current activity while the ad will refresh after a certain time period. To implement Google Admob Banner Ad continue with following steps:

Open activity_main.xml file and insert the following line of code in the layout XML File for banner:

<com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="_INSERT_AD_ID_HERE_">
        </com.google.android.gms.ads.AdView>

Don’t forget to place your Banner Ad ID in the code above. The complete code for activity_main.xml file looks likes this:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.faultinmycode.admobadexample.MainActivity">

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

Now go to MainActivity.Java file and create an AdView variable mAdView and add the following lines of code after initializing MobileAds.

mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

The complete code of MainActivity.Java after inserting Google Admob Banner Ad is here:

package com.androiddvlpr.admobadexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

public class MainActivity extends AppCompatActivity {
    private AdView mAdView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Don't forget to insert your App ID below
        //we have inserted Test ID that you can use while testing your App.
        MobileAds.initialize(this, "_INSERT_APP_ID_HERE_");
        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
}

Google Admob Interstitial Ad Implementation

Google Admob Interstitial Ads are one that takes up complete screen space of user device. They can be used when the user clicks on a button and switches to a different activity or after a particular time is lapsed. When an app shows an interstitial ad, the user has the choice to either tap on the ad and do what the ad says or close it the interstitial Ad and return to the app.

To create an Interstitial Ad, go to mainActivity.Java and create an InterstitialAd Variable mInterstitialAd and pass the following lines of code:

mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("_INSERT_INTERSTITIAL_AD_ID_HERE_");

Now, the ad has to be loaded onto the user screen after a certain time or when the user pauses or switches to a different activity. In that case to the trigger component add following lines of code:

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

Note that calls to interstitial ads have to be made in the same thread.

The complete code of MainActivity.Java file for Banner and Interstitial Ads can be found here:

package com.androiddvlpr.admobadexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;

public class MainActivity extends AppCompatActivity {
    private AdView mAdView;
    private InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Don't forget to insert your App ID below
        //we have inserted Test ID that you can use while testing your App.
        MobileAds.initialize(this, "_INSERT_APP_ID_HERE_");

        //Google Admob Banner Ad
        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        //Google Admob Interstitial Ad
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("_INSERT_INTERSTITAL_AD_ID_HERE_");

        //To Load Gogole Admob Interstitial Ad
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
    }
}

TaDa! Ads are created and are shown.

In case you’re testing your app never use your Admob App and Ad IDs else Google will block your Admob account. You can always use these Google Admob Test IDs.

Google Admob Test App ID: ca-app-pub-3940256099942544~3347511713
Google Admob Banner AD Test ID: ca-app-pub-3940256099942544/6300978111
Admob Interstitial AD Test ID: ca-app-pub-3940256099942544/1033173712

Once you publish your app on the play store, don’t forget to link it with your Admob Account.

You can do so by visiting your Admob Account and clicking on particular App, Go to App settings and click on ‘Link with App on Google Play’.

Interested in creating more apps? See here: Learn to create Live Wallpaper App in Android and Flutter Android Studio Install and Build your First app.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like