In this tutorial, we are going to learn how we can integrate Google AdMob ads without violating Google policies of use. Some developers may just want to make money.
In this tutorial, we are going to learn how we can integrate Google AdMob ads without violating Google policies of use.
Some developers may just want to make money.Google advertising is one of the methods that you can make money by showing Google ads that are family friendly.Normally Google does not support sexual content.If you trying adding google ads in applications that have sexual content you eventually end up being burned.
Today we will focus on just google Interstitial Ads. The Banner, Native Ads, and Rewarded Video are for another day.
You can read more about Google AdMob here
Interstitial Ads are the one that occupies the whole device screen and they normally require the user to click them so that they can close.
Banner Ads are the one that appears at the bottom of the screen.
NB:
If Ads are placed wrongly they can sometimes be nagging and they will make your application users give bad reviews.So make money wisely and let users interact with the application.
Let get started:
Open your IDE and create a new project:
After adding the dependency build.gradle(Module:app) should look like below
Some developers may just want to make money.Google advertising is one of the methods that you can make money by showing Google ads that are family friendly.Normally Google does not support sexual content.If you trying adding google ads in applications that have sexual content you eventually end up being burned.
Today we will focus on just google Interstitial Ads. The Banner, Native Ads, and Rewarded Video are for another day.
You can read more about Google AdMob here
Interstitial Ads are the one that occupies the whole device screen and they normally require the user to click them so that they can close.
Banner Ads are the one that appears at the bottom of the screen.
NB:
If Ads are placed wrongly they can sometimes be nagging and they will make your application users give bad reviews.So make money wisely and let users interact with the application.
Let get started:
Open your IDE and create a new project:
Name:AndroidInterstitialAds
Package Name:package net.techoverload.www.androidinterstitialads
After creating the project, we need to add google dependancies to our buid.grandle (Module:app ) so that we can be able to use ads imports.
implementation 'com.google.android.gms:play-services-ads:11.8.0'
If your using Android Studio is pretty much easy adding com.google dependency, Click on project structure, the Ads, check on Admob check box.
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "net.techoverload.www.androidinterstitialads"
minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.android.gms:play-services-ads:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Generally, Interstitial Ads loads when the user clicks on application buttons or other views.In this example, we are going to load the Ad when the user clicks a button inside the application.Lets now create the activity_main layout.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="net.techoverload.www.androidinterstitialads.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Google Ads Example"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/click_me"
android:text="Show Ad"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package net.techoverload.www.androidinterstitialads;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
private InterstitialAd interstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, "YOUR APPLICATION ID");
interstitialAd = new InterstitialAd(this);
//Load google test Ads
interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
interstitialAd.loadAd(new AdRequest.Builder().build());
Button button=(Button)findViewById(R.id.click_me);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(interstitialAd.isLoaded())
{
interstitialAd.show();
}
else
{
Toast.makeText(getBaseContext(),"Ad not loaded",Toast.LENGTH_SHORT).show();
Log.d("TAG","The interstial Ad was not loaded");
}
}
});
}
}
In above code, I have used google test Ads to load the ads unit
interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
Google has set aside tests ads to allow developers to test their ads without using live ads they created in their AdMob account.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.techoverload.www.androidinterstitialads">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run the application and you should see below the screen in your Android Emulator.
Download Project Source code
Adding Listener to the Ad
We can add a listener to the Ad by implementing AdListener interface.
interstitialAd.setAdListener(new AdListener() { public void onAdClosed() { interstitialAd.loadAd(new AdRequest.Builder().build()); } });