Nursery Rhymes Video Library

Thursday 19 June 2014

How to Add AdMob Interstitial Ads in Your Android Apps (Interstitials - Google Mobile Ads SDK )

How to use Interstitials Ads - for AdMob In Eclipse (Android)

This Tutorial explains how to use / show Interstitial Ads in Android with Admob.
The tutorial is short and to the point. I recommend you to be relax and understand the coding.
I have also attached the link to the Source Code Here

or for your Ease you can try the apk to test how the example works. Download APK for the example
















This is my Java Class - InterstitialsAdsExampleActivity.java


package example.interstitialAds;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.google.ads.Ad;

import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.InterstitialAd;
import example.interstitialAds.R;

public class InterstitialsAdsExampleActivity extends Activity implements

AdListener {
/** Called when the activity is first created. */
private InterstitialAd interstitialAds = null;
private TextView textView = null;

@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

this.interstitialAds = new InterstitialAd(this, "ca-app-pub-2907244913861147/9834565116");

this.interstitialAds.setAdListener(this);

Button loadButton = (Button) this.findViewById(R.id.loadButton);

loadButton.setOnClickListener(loadButtonOnClick);

this.textView = (TextView) this.findViewById(R.id.stateTextView);

}

private OnClickListener loadButtonOnClick = new OnClickListener() {


@Override

public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText("Loading Intertitial Ads");

AdRequest adr = new AdRequest();


interstitialAds.loadAd(adr);
}
};

@Override

public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub

}


@Override

public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
String message = "Load Ads Failed: (" + error + ")";
textView.setText(message);
}

@Override

public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}

/**

* Called when an Activity is created in front of the app (e.g. an
* interstitial is shown, or an ad is clicked and launches a new Activity).
*/
@Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}

@Override

public void onReceiveAd(Ad arg0) {
if (interstitialAds.isReady()) {
interstitialAds.show();
} else {
textView.setText("Interstitial ad was not ready to be shown.");
}
}
}



And this is the XML file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView

        android:id="@+id/stateTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    
    <Button android:id="@+id/loadButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/loadAds"/>
</LinearLayout>



And this is the Android Manifest:

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

    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="14"/>


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

    <uses-permission android:name="android.permission.INTERNET"/>
    
    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name="com.jms.InterstitialsAdsExampleActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>
        
        <activity android:name="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>

</manifest>