Skip to main content

How to Add Ads in Android App Example | Admob Ads

There are millions of Free Apps on the Play Store with plenty of them containing ads on them. 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.

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:

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

2. Once the account is created, Click on ‘Get Started’ button to create your first Google Admob Ad unit.
How to Add Ads in Android App Example | Admob Ads
How to Add Ads in Android App Example
3. Google Admob will now ask if you have your App published in Play or App store, select ‘No’ if not.

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

5. 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.

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

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

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

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

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

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

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

allprojects {
    repositories{
        jcenter()
       maven {
            url"https://maven.google.com"
       }
   }
}
 
13. Go to Project Panel again and open Gradle Build (Module) and add following lines of code highlighted in bold:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.google.android.gms:play-services-ads:11.8.0'
}
 
14. 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, "ca-app-pub-3940256099942544~3347511713");

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:

16. 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="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.ads.AdView>
 
17. 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>
 
18. Now go to MainActivity.Java file and create an AdView variable mAdView and add following lines of code after initializing MobileAds.

mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
 
19. The complete code of MainActivity.Java after inserting Google Admob Banner Ad is here:

MainActivity.Java
package com.faultinmycode.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, "ca-app-pub-3940256099942544~3347511713");
        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.

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

 mInterstitialAd = new InterstitialAd(this);
 mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
 
21. 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());
 
22. Note that calls to interstitial ads have to be made in the same thread. To know more about Android Threads, see here.

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

MainActivity.Java
package com.faultinmycode.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, "ca-app-pub-3940256099942544~3347511713");

        //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("ca-app-pub-3940256099942544/1033173712");

        //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.

Please Note!!!

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’.

To know more about how to create Android App, check our tutorial series on Roadway to Android App Development here. To create your own Android Calculator click here or Piano App, Click here.

Comments

Popular posts from this blog

How To Open PDF File in Android Application Example Included

Want to know How To Open PDF File in Android Application Example. To open a PDF File in Android Application, your app can take help from Free Android library available on Github.

Here are few apps made from PDF Library you can look at, click here to know more.

By using Android Library you are not only leveraging the hard work from another developer but also making it easier and quicker to develop an android app.
How To Open PDF File in Android Application Example  In this tutorial series, we will teach you how to open a pdf file in Android Application Example included with the help of Android PDFViewer library developed by Barteksc.

Before I proceed further, here is the screenshot of the kind of PDF Viewer that’ll be created using Android Studio.



 How To Open PDF File in Android Application Example

The app will open a specified pdf file which will be copied in the assets folder of your Android studio project. I've used bitcoin.pdf file, you can use any of your choices.
Software F…

Python Turtle Example: Flappy Bird Development in under 10 mins!

Python Turtle Example | Develop Flappy Bird Clone in 10 mins In the following tutorial, we have used following Python libraries:
1. random: To generate obstacles at random position. 2. turtle: To help provide directions to the player. 3. vector from freegames: To 2-dimensional vectors.
Before developing the game, here is a screenshot of what you'll be developing in 10 mins.
Steps to create Python Flappy Bird Game:
1. Open Pycharm and install 'freegames' package by going to Files > Settings > Project Interpreter. 2. Click on '+' sign on right and search for 'freegames' package. 3. Import packages in your project by writing following lines of code:
from random import * from turtle import * from freegames import vector 4. There are two variables required in the game, one for Bird object and another for an array of obstacles:
bird = vector(0, 0) balls = []
5. Create function for the player response on screen tap:
def tap(x, y): "Move bird up in res…

Open Weather Map API Example | Create Android Weather App

In this tutorial blog, using open weather map API example, we will create an Android weather app. The Android App will able to access Open weather map website server and access weather details of particular location.

The App will look similar to the image shown below. It will fetch real time weather of the location provided in MainActivity.Java file.


Software / Files used:Android Studio (Version 3.x) Files Edited / Created:activity_main.xmlMainActivity.JavaWeather.JavaAndroidManifest.xml Steps Involved in Open Weather Map API Example: Here are the Steps you need to follow on ‘Open Weather map API Example’ to create Android Weather App:

1. Open the Open Weather map API link provided here. Sign up for Current Weather Data API key here.

2. Once the key is received, save it to notepad or on stick notes.

3. Open Android Studio and create a new Project with name, ‘FIMC Weather App’, we will select an empty activity in the wizard and wait for Gradle build.



4. Open AndroidManifest.xml…