Admob banner ad integration in Andriod Studio

Admob integration becomes very easy with the Android Studio. No need to include .jar file or google play service lib like in Eclipse. Follow the steps to include admob banner to your app.

 

  1. Make sure you’ve updated the “Extra” part of your android sdk. Because we need here Google Repository. Now add the following line to dependencies area of your app’s gradle file.
    compile 'com.google.android.gms:play-services-ads:7.8.0'
    

    After pasting this line, check the bottom part of the android studio where it will suggest you to change the version of google play service ads. Because it might be possible that there will be new version of google play service ads. Now click on “sync now” at the top of android studio and you are ready to go.

  2. Now open the AndroidManifest.xml file and include following in different part of the file. First add two permission in the <manifest> tag and out side of <application> tag.
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    Now add meta data in your <application> tag.

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

    Now add admob activity in application tag same as meta data.

    <activity 
    android:name="com.google.android.gms.ads.AdActivity" 
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
    android:theme="@android:style/Theme.Translucent" />
  3. Then add the below xml code in your xml layout file where you want to display ad. It’s a good practice to surround your ad with relative layout. So that you can easily hide the ad until it’s load without affecting the rest of your layout.
    <RelativeLayout 
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:id="@+id/rl_ad" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true">
        <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" 
          android:visibility="gone" 
          ads:adSize="BANNER" 
          ads:adUnitId="YOUR_AD_UNIT_ID">
        </com.google.android.gms.ads.AdView>
    </RelativeLayout>

    Make sure that you set the visibility of the ad to “gone” in xml file. Because we will hide the ad until it loads completely or there will be blank white space until the ad loads from server and it looks bad. Do not forget to replace YOUR_AD_UNIT_ID with your actual Ad unit id created from admob dashboard.

  4. And finally add the following code in your respective Activity file and that’s it! The ad should be loaded.
    final AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    mAdView.setAdListener(new AdListener() {
      @Override
      public void onAdLoaded() {
        super.onAdLoaded();
        mAdView.setVisibility(View.VISIBLE);
      }
    });

    It will display the ad once it loaded completely.

 
You can check the detailed explanation of everything in this tutorial as well as other information about integration here on Google Developers site and download sample app from there.

If you have any issues in the above code, please write them below in comment section. Thank you.
 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s