Integrate Google Ads android :
Admob Ads, We generally see ads regarding different products in different streams, so as in our android app we can show ads with the help of Google Admob.
When you link your app with the Admob using sdk it starts showing you the ads in the place you have specified.You may customize the categories of ads you want to show on your app from admob console.
These ads are intended for the end user so be careful while you are integrating them use Test Ads. As it is against the Admob policy to use the live ads while testing.
Generally this phenomena is termed as App Monetization in this tutorial on Integrate Google Ads android ads i will show you how to integrate
- Banner Ads
- Interstitial Ads
Banner Ads
Ad’s which are generally displayed as a small banner in top of the screen or bottom of the screen. While placing these ad’s make sure you avoid places such that no unwanted clicks occur on these views.
Interstitial Ads
Compared to banner ads these ad’s are displayed in full screen.So displayed these ads when ever you think is necessary same like above ads avoid unwanted clicks.
If you don’t have and Admob account can signup here Google Admob
Admob Ads Video :
Add Admob SDK to your project to integrate Google Ads.
build.gradle(Module:app)
compile 'com.google.android.gms:play-services-ads:11.6.0'
build.gradle (Project)
dependencies { classpath 'com.google.gms:google-services:3.1.0' }
allprojects { repositories { google() jcenter() maven { url "https://maven.google.com" } } }
activity_main.xml
Adding two buttons to move forward into screens where ads are displayed
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" <Button android:id="@+id/bannerAdd" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="banner" android:text="Banner Add" /> <Button android:id="@+id/interstitialAdd" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="interstitial" android:text="Banner Add" /> </LinearLayout>
MainActivity
Just code buttons to move to screens
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void banner(View view) { Intent i = new Intent(MainActivity.this,BannerAdd.class); startActivity(i); } public void interstitial(View view) { Intent i = new Intent(MainActivity.this,Interstitial.class); startActivity(i); } }
now
activity_banner_add :
Integrate Google Ads using which you can display banner add make sure you place them in appropriate place avoiding unwanted clicks.
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="ads.android.com.admobads.BannerAd"> <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="200dp" android:text="Go to Home" android:onClick="home"/> </LinearLayout>
then initialize them
Admob Ads – BannerAd :
For testing purpose i am using sample ad units ca-app-pub-3940256099942544/6300978111
You may find them here
Initialize ads
MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111");
adview
adView = (AdView) findViewById(R.id.adView);
then fetching the ads for the specified adunit
AdRequest request = new AdRequest.Builder() .addTestDevice("ca-app-pub-3940256099942544/6300978111") .build();
Integrate Google Ads into adview
adView.loadAd(request);
public class BannerAd extends AppCompatActivity { AdView adView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_banner_add); MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111"); adView = (AdView) findViewById(R.id.adView); AdRequest request = new AdRequest.Builder() .addTestDevice("ca-app-pub-3940256099942544/6300978111") .build(); adView.loadAd(request); } public void home(View view) { Intent i = new Intent(BannerAd.this,MainActivity.class); startActivity(i); } }
activity_interstitial :
Add a button, by clicking on which you can see a Interstitial ad appearing
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:gravity="center"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Load Interstital Ad" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="230dp" tools:ignore="MissingConstraints" /> </LinearLayout>
Admob Ads – Interstitial :
Integrate Google Ads and initialize Interstitial ads
private InterstitialAd mInterstitialAd = new InterstitialAd(this);
adding adunit there is a different adunit for both Banner and Interstitial Ad mat refer link for sample adunits
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
you need to load ad before it’s been displayed
mInterstitialAd.loadAd(new AdRequest.Builder().build());
then load the ad on button click
if (mInterstitialAd.isLoaded()) { mInterstitialAd.show(); } else { Intent i = new Intent(Interstitial.this,MainActivity.class); startActivity(i); }
AdListener can be used to make any functionality if required during the ads are being displayed or after being displayed
After an ad is displayed you need to load the next ad
mInterstitialAd.loadAd(new AdRequest.Builder().build());
@Override public void onAdClosed() { // Code to be executed when when the interstitial ad is closed. mInterstitialAd.loadAd(new AdRequest.Builder().build()); }
mInterstitialAd.setAdListener(new AdListener() { @Override public void onAdLoaded() { // Code to be executed when an ad finishes loading. } @Override public void onAdFailedToLoad(int errorCode) { // Code to be executed when an ad request fails. } @Override public void onAdOpened() { // Code to be executed when the ad is displayed. } @Override public void onAdLeftApplication() { // Code to be executed when the user has left the app. } @Override public void onAdClosed() { // Code to be executed when when the interstitial ad is closed. mInterstitialAd.loadAd(new AdRequest.Builder().build()); } });
public class Interstitial extends AppCompatActivity { private InterstitialAd mInterstitialAd; Button Button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_interstitial); mInterstitialAd = new InterstitialAd(this); mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); mInterstitialAd.loadAd(new AdRequest.Builder().build()); Button = (Button) findViewById(R.id.button); Button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mInterstitialAd.isLoaded()) { mInterstitialAd.show(); } else { Intent i = new Intent(Interstitial.this,MainActivity.class); startActivity(i); } } }); mInterstitialAd.setAdListener(new AdListener() { @Override public void onAdLoaded() { // Code to be executed when an ad finishes loading. } @Override public void onAdFailedToLoad(int errorCode) { // Code to be executed when an ad request fails. } @Override public void onAdOpened() { // Code to be executed when the ad is displayed. } @Override public void onAdLeftApplication() { // Code to be executed when the user has left the app. } @Override public void onAdClosed() { // Code to be executed when when the interstitial ad is closed. mInterstitialAd.loadAd(new AdRequest.Builder().build()); } }); } }
Manifest
Add internet and access network permissions to you manifest file to integrate Google Ads.
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ads.android.com.admobads"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <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> <activity android:name=".BannerAd" /> <activity android:name=".Interstitial"/> </application> </manifest>
For more info visit Admob
Share and like this tutorial on Integrate Google Ads android if you like it. If you have any query’s on this tutorial do let us know in the comment section below.