Add Banner Ads to Android app

Before doing anything first create add unit ids at admob .
Now add dependecies as below

dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.google.android.gms:play-services-ads:11.8.0'
compile 'com.android.support:design:26.1.0'
}

Add app id and add unit id in strings.xml

<resources>
<string name="app_name">AdMob</string>
<string name="title_activity_second_activiy">Interstitial</string>
<string name="msg_welcome">Welcome to Admob. Click on the below button to launch the Interstitial ad.</string>



<!-- TODO - add your ad unit Ids -->
<!-- AdMob ad unit IDs -->
<string name="admob_app_id">id here</string>
<string name="banner_home_footer">id here</string>


</resources>

Create Class MyApplication.java

package your package;

import android.app.Application;

import com.google.android.gms.ads.MobileAds;



public class MyApplication extends Application {

@Override
public void onCreate() {
super.onCreate();

// initialize the AdMob app
MobileAds.initialize(this, getString(R.string.admob_app_id));
}
}

Android Mainifest i

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="yourpackage"
>

<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>

activity Xml file

<?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"
xmlns:tools="http://schemas.android.com/tools"
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="yourpackage.MainActivity"
>



<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_home_footer"
></com.google.android.gms.ads.AdView>
</RelativeLayout>

Mainactivity.java class

package info.yourpackage;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {

private AdView mAdView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);






// TODO - remove this if condition
// it's for demo purpose
if (TextUtils.isEmpty(getString(R.string.banner_home_footer))) {
Toast.makeText(getApplicationContext(), "Please mention your Banner Ad ID in strings.xml", Toast.LENGTH_LONG).show();
return;
}

mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(getString(R.string.banner_home_footer));

AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
// Check the LogCat to get your test device ID
.addTestDevice("your device id")
.build();

mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
}

@Override
public void onAdClosed() {
Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();
}

@Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
}

@Override
public void onAdLeftApplication() {
Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
}

@Override
public void onAdOpened() {
super.onAdOpened();
}
});

mAdView.loadAd(adRequest);


}

@Override
public void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}

@Override
public void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}

@Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}
}

All Done 
Happy Coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *