How to add admob banner ad in your android app using android studio ?

Add admob banner ad in your android app using android studio

Add admob banner ad in your android app using android studio

This tutorial we guide you to integrate the Google Mobile Ads SDK into a new app. And show you how to display a banner ad in your app.

Step 1

Login to your https://www.google.com/admob/ account the  click Monetize a new app.

Step 2

Click add your app manually if it is not deployed in google playstore

Enter app name and platform

monitize-new-app

Click Add app

monetize-a-new-app-2

Generate & Save App ID & Ad unit ID for banner ad

Skip firebase if you don’t need it now.

Click Done

Step 3

Open AndroidManifest.xml and add the below mentioned permissions and other properties.

  • Add INTERNET & ACCESS_NETWORK_STATE permissions.
  •  Add google play services version meta-data.
  • Add the AdActivity adding configChanges and theme attributes.

 

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.appname">
 
    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <!--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>

 

Step 4

Now launch Android studio & open your app.

Open your app’s  build.gradle(Module:app)

Add firebase dependency   // Google integrate admob with firebase

dependencies 
 {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 
 compile 'com.android.support:appcompat-v7:24.2.1'
 compile 'com.android.support:design:24.2.1'
 compile 'com.google.firebase:firebase-ads:9.0.2' // Firebase dependency (Always check for the latest version)
 }
apply plugin: 'com.google.gms.google-services'

P.S Always check for the latest firebase dependency (if you have trouble with new one use 9.0.2)

Now sync your project

Step 5

Open your apps string resource file and add this line inside resource tag

<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string> // replace this line with your id in production version

Skip it if you want to add it directly to adview

Step 6

Open your activity XML file to implement adview

<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_height="match_parent"
        android:layout_width="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">
        
         
        <com.google.android.gms.ads.AdView
            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="@string/banner_ad_unit_id">
        </com.google.android.gms.ads.AdView>
         
</RelativeLayout>

 PS: Don’t forget to add this line

xmlns:ads="http://schemas.android.com/apk/res-auto"

Also change ads:adSize =”BANNER”  if you don’t need smart banner ( I recommend to use smart banner – It scale perfectly on device)

Step 7

Move to your activity java code

package com.sample.appname;

import android.support.v7.app.AppCompatActivity;


import com.sample.appname.R;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;        //import these 3 libraries        
import com.google.android.gms.ads.MobileAds;

public class Mainactivity extends AppCompatActivity 
{
 @Override
    protected void onCreate(Bundle savedInstanceState) 
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        MobileAds.initialize(getApplicationContext(), "ca-app-pub- 65899583587752154~78858584206"); // Add your App ID here (Don't confuse with adunit ID)     

        //add code
        AdView mAdView = (AdView) findViewById(R.id.adView);

        AdRequest adRequest = new AdRequest.Builder().build();

        mAdView.loadAd(adRequest);

    }
}

Now build and Run your app

.admob

Change app ID to production version at the time of deployment