Before integrating AdMob ads in your applications it is highly recommended to read AdMob policies here : AdMob Policies
Let's start integration step by step
1. First download google repository package using sdk manager in Android Studio as shown in diagram below.
Note : when ever your app in testing stage don't use your real admob id's instead use test ad id's those i'm providing in this tutorial , before submitting your app in play store at that time replace test ad id's with real ad id's check and publish app .
if your using real id's in testing stage your admob account can be suspended without giving intemation.
4. Next create strings in strings.xml to refer admob banner,iterstial id's here i'm using test id's .
strings.xml:
Let's start integration step by step
1. First download google repository package using sdk manager in Android Studio as shown in diagram below.
2. Once you done with downloading above package next update the app to reference the google play services ads sdk in build.gradle file as follows.
build.gradle :
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.ramsandroid.admobsample"
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.android.gms:play-services-ads:7.8.0'
}
next after adding above google play service ads sdk in our build.gradle file you can observe alert is saying that sync now just go with it by select sync now option.
3.Next thing is we need to modify the manifest file by adding permissions, meta data tag and activity tag as follows.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ramsandroid.admobsample"> <!-- Include required permissions for Google Mobile Ads to run--> <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:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!--This meta-data tag is required to use Google Play Services.--> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <!--Include the AdActivity configChanges and theme. --> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" /> </application> </manifest>
If you don't have admob account it is very simple just login to https://www.google.com/admob/ website by using your google user name and password. Once you complete the admob setup by giving some information in the main dashboard you can see the option monetize new app just select it and next select add app your manually next give your app name and select platform android and next click add button in the next step select ad formats like banner or interstitial create id's and finally you can finish the setup by clicking done button.
Note : when ever your app in testing stage don't use your real admob id's instead use test ad id's those i'm providing in this tutorial , before submitting your app in play store at that time replace test ad id's with real ad id's check and publish app .
if your using real id's in testing stage your admob account can be suspended without giving intemation.
4. Next create strings in strings.xml to refer admob banner,iterstial id's here i'm using test id's .
strings.xml:
<resources> <string name="app_name">AdMob Sample</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string> <string name="interstial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string> </resources>
Following code is for banner ad's:
5. Next to show banner ad in any activity look at the code below in xml layout and as well as activity.
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <com.google.android.gms.ads.AdView android:id="@+id/adView" ads:adSize="BANNER" ads:adUnitId="@string/banner_ad_unit_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"></com.google.android.gms.ads.AdView> </RelativeLayout>MainActivity.java:package com.ramsandroid.admobsample; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AdView mAdView = (AdView) findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); } }you can see outpu of this app in below screen shot :
Following code is for interstial ad's:
MainActivity.java:package com.ramsandroid.admobsample; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.InterstitialAd; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //To show banner id AdView mAdView = (AdView) findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); //To show interstial ad final InterstitialAd interstitialAd = new InterstitialAd(this); interstitialAd.setAdUnitId(getResources().getString(R.string.interstial_ad_unit_id)); interstitialAd.loadAd(new AdRequest.Builder().build()); interstitialAd.setAdListener(new AdListener() { @Override public void onAdLoaded() { super.onAdLoaded(); interstitialAd.show(); } }); } }you can see outpu of this app in below screen shot :
Note : Don't show interstial ad's in first activty of your apps. If you do that apps are going to approve to publish in the play store. before integrating admob in your application read admob policies carefully.