Monday, December 29, 2014

Integrating Google AdMob with Banner in Android Applications


Overview :

AdMob brings together best-in-class technology in a single platform, so you can gain insights about your users, drive more in-app purchases, and maximize your ad revenue. No longer will you have to rely on a combination of tools or use precious development resources to build your own solution.

Step 1 :

First we add an account in AdMob publisher and Identify your publisher ID on the top right of your dashboard. Click here to sign up the AdMob publisher.

Step 2 :

Create a new project in Eclipse File ⇒ New ⇒ Android Application Project. Fill in the details and name your project AdmobProject.

Step 3 :

Here is a complete example of MainActivity.java


package com.sampleadmob;

import android.app.Activity;

import android.os.Bundle;

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 Activity {

private InterstitialAd interstitial;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

interstitial = new InterstitialAd(MainActivity.this);

// Insert the Ad Unit ID

interstitial.setAdUnitId("ca-app-pub-xxxxxxxxxxxx/xxxxxxxxxx");

AdView adView = (AdView) this.findViewById(R.id.adView);

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

adView.loadAd(adRequest);

interstitial.loadAd(adRequest);

interstitial.setAdListener(new AdListener() {

public void onAdLoaded() {

displayAd();

}

});

}

public void displayAd() {

// If Ads are loaded, show Interstitial else show nothing.

if (interstitial.isLoaded()) {

interstitial.show();

}

}

}


Step 4 :

In activity_main.xml we declare this

<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_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="com.example.sampleadmob.MainActivity" >

<com.google.android.gms.ads.AdView

android:id="@+id/adView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

ads:adSize="BANNER"

// Insert the Ad Unit ID

ads:adUnitId="ca-app-pub-xxxxxxxxxxxxx/xxxxxxxx" />

</RelativeLayout>


Step 5 :

In Android manifest file we declare permissions to allow the internet connection and network state and declare activity for Google play services.


Declare Google services activity :

<meta-data

android:name="com.google.android.gms.version"

android:value="@integer/google_play_services_version" />

<activity

android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >

</activity>


Internet and network state permission :


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

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


Screenshot :

 


Baalaji is a Software Engineer at Span Technology Services

9 comments:

  1. Brilliant site you have here, so much cool data!.. Free SEO Solutions

    ReplyDelete
  2. I see and read yoyr articles about integrating google admob with banner in.i clearly say you that i find something new in your article or if you know about something new plz come that point ballons and get a lot of information

    ReplyDelete
  3. I see and read yoyr articles about incorporating google admob with flag in.i unmistakably state you that I discover something new in your article or on the off chance that you think about something new plz come that point http://zonlineshop.com/ and get a ton of data

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Your articles about fusing google is good.i undeniably state you that I find something new in your article or in case you consider something new plz come that site iptv and get a huge amount of informative data.

    ReplyDelete
  6. Thus, webmasters must investigate the pros and cons of SEO services SirLinksalot That's why it is important to get your content articles indexed before you submit them to the search engines

    ReplyDelete
  7. I have read your article. And I know you're a very good person. And your workforce is also doing very well. seo learning No matter your site I've got a lot to learn but I've seen a site a few weeks early. Which is explained from the awesome tracks seo. Tips are also given each week so if you want to learn right in the menu then the knowledge on this site will increase greatly. free pakistani chat rooms

    ReplyDelete
  8. Wonderful illustrated information. I thank you about that. No doubt it will be very useful for my future projects. how to launch IoT app Would like to see some other posts on the same subject!

    ReplyDelete
  9. This is great valuable information for all business..keep update. IoT app launch guide .And we have also the best mobile app development company in chennai..and we providing all services..

    ReplyDelete