Add Admob in Android App

Step1:

Create new Project in eclipse.

Add Admob in Android App

Step2:

import Google Play Services library into your workspace.

What is Google Play Services

Step3:

add Google Play Services library into your project.

Right Click on project than open “property” window.

Click on “Android” and add library.

Add Admob in Android App

Step4:

Add your Banner and Interstitial id in “strings.xml”

Example:

<string name="banner_id">ca-app-pub-7335882925318194/**********</string>
<string name="interstitial_id">ca-app-pub-7335882925318194/**********</string>  

Add Admob in Android App

Step5:

Give permission for internet inside “manifest.xml”.

<uses -permission android:name="android.permission.INTERNET"></uses>

Copy and paste these lines into you manifest.xml under tag.

 

                android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

        <meta -data android:name="com.google.android.gms.version"
                   android:value="@integer/google_play_services_version" />

Add Admob in Android App

Step6:

Create a xml file inside “layout” folder which is under “res” folder. Give file name as “banner_adview.xml”.

Copy and paste these codes into “banner_adview.xml”

< ?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

  <com .google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_id"
        android:visibility="visible"></com>

</linearlayout>

Step7:

include “banner_adview.xml” into your “activity_main.xml” by adding these lines into “activity_main.xml”

<include layout="@layout/banner_adview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></include>

Add Admob in Android App

Step8:

create “InterstitialAd” variable into “MainActivity.java”.

private InterstitialAd interstitial;
add these lines to integrate banner adview into “onCreate()”
AdView adView = (AdView) this.findViewById(R.id.adView);
  		AdRequest request = new AdRequest.Builder().build();
  		adView.loadAd(request);
  		interstitial = new InterstitialAd(getBaseContext());
  		interstitial.setAdUnitId(getResources().getString(R.string.interstitial_id));
  		interstitial.loadAd(request);

and finally add these line for interstitial ad on back pressed button.
@Override
	public void onBackPressed() {
		if(interstitial.isLoaded()){
			interstitial.show();
		}
		super.onBackPressed();
	}

Add Admob in Android App

Run your project and check you have successfully added Admob into your app.

Add Admob in Android App

Related Posts

Leave a comment

Hey, so you decided to leave a comment! That's great. Just fill in the required fields and hit submit. Note that your comment will need to be reviewed before its published.