AdMob ads in Android Studio

In This tutorial you learn how to add Admob ads in Android Studio. Most of developer use ADMob ads. So I will tell you in this post. How to Integrate Admob ads in Android Studio. Admob ads are so easy to integrate.

Types of Ads

Currently you set three types of Admob. In this post i will tell you two types of ads. third one i will tell you in next post

  1. First is banner ads Means ( Small size ads )
  2. Second is Interstitial (Full Screen Ads )
  3. Third is Native ads

Today you will learn Banner and Interstitial ads. So start with a new project in android studio. Add these line in

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "ctutorial.sahil.com.ctutorial"
        minSdkVersion 13
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.android.support:design:25.0.1'
    testCompile 'junit:junit:4.12'
    compile 'com.google.android.gms:play-services-ads:9.4.0'
}


Now next step is Add permission in 
AndroidMainifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ctutorial.sahil.com.ctutorial">
    <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:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <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>

"1&1 
Now third Step is add these line
 activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

          <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.ads.AdView>
    </RelativeLayout>


Now forth step is Add these line in 
"DreamHost"
MainActivity.java

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 {
    private InterstitialAd interstitial;
       @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activitymain);
        AdView adView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest1 = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();
        adView.loadAd(adRequest1);
         interstitial = new InterstitialAd(MainActivity.this);
        interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");         //   AdView adView = (AdView) findViewById(R.id.adView);         AdRequest adRequest = new AdRequest.Builder()                 .setRequestAgent("android_studio:ad_template").build();         // adView.loadAd(adRequest);         interstitial.loadAd(adRequest);         interstitial.setAdListener(new AdListener() {             public void onAdLoaded() {                 displayInterstitial();             }         });     }     public void displayInterstitial() {         if (interstitial.isLoaded()) {             interstitial.show();         }     } } now last step is add these line in app-----res------value------string.xml String.xml
<resources>
    <string name="app_name">App name</string>
 <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
 <string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string> </resources> These ads coding you can sign up AdMob account and click Monetize New App--------select Add App Manually --------Set name-----click select Platform-----Android------ad App----Select AdUnit name and write Banner----done same in Interstitial.
You can run the project then this output show this is 
banner ads output
"admob"

this interstitial ads output
"admob