A Definitive Tutorial to Incorporate An In-app Ad via AdMob

AdMob is the abbreviation of “Advertisement On Mobile”. As most of the users often prefer the free mobile applications over the paid applications, the developers have streamlined the two alternative routes for app monetization including in-app purchases and in-app advertisements. If you are looking for the ways to efficiently monetize your app by integrating an advertisement in it, this AdMob tutorial 2015 will offer you a complete guide to breakthrough the process in an absolute way.

There are several advertising networks that are grabbing the popularity across the globe. While delivering top-notch solutions for in-app advertisements, they help developers associated with Android applications development platform to generate greater revenue. For instance, the Google AdMob that is the much sought after choice for the majority of developers.

It augments app monetization via an amazing app-vertising model. The featured Software Development Kit (SDK) facilitates the connection across the eminent mobile platforms including, Android, Windows phone and iOS. Its SDK also offers a list of ad formats to choose from like the ad banner as interactive ads, standard banner ad, custom search ad and so forth.

Let’s begin with this AdMob tutorial 2015 and explain the steps needed to be processed for integrating an ad in your mobile application.

What's Trending

Step 1: First Things First – Create an account

While getting started, the very first thing that you will need to do is configure your account with the AdMob. However, if you already possess an account with AdMob, simply sign into that account and proceed further.

Once you are done with that, you will see your publisher id on the top-right corner of the dashboard.

AdMob tutorial 2015 Step 1

Step 2: Develop an ad unit

Now, navigate to the Monetize tab and generate an advertisement unit while considering your app design. Here, you can see a window as shown in the below image.

AdMob tutorial 2015 Step 2

Step 3: Create a new Eclipse project

First, get the Google Play Services Library downloaded in your Eclipse IDE.

  • Now, access the File tab and under the New tab select the Android Application Project.
  • After doing so, you will get a small form, simply add the project details in the corresponding fields.

Here, let’s assume the project name to be AdmobBannerExample.

  • Application Name : AdmobBannerExample
  • Project Name : AdmobBannerExample
  • Package Name : com.android.admob
  • Finally import the Google Play Services Library into your mobile application.

AdMob tutorial 2015 Step 3

Step 4: Update the MainActivity.java class

  • Access the MainActivity.java class of your application project.
  • Copy and paste the below mentioned piece of tutorial code into it.

Code Snippet for the MainActivity.java class

package com.android.admob;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
/**
* Main Activity. Inflates main activity xml and child fragments.
*/
public class MyActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
/**
* A placeholder fragment containing a simple view. This fragment
* would include your content.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);
return rootView;
}
}
/**
* This class makes the ad request and loads the ad.
*/
public static class AdFragment extends Fragment {
private AdView mAdView;
public AdFragment() {
}
@Override
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
// Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
// values/strings.xml.
mAdView = (AdView) getView().findViewById(R.id.adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device. e.g.
// "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
// Start loading the ad in the background.
mAdView.loadAd(adRequest);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_ad, container, false);
}
/** Called when leaving the activity */
@Override
public void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}
/** Called when returning to the activity */
@Override
public void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}
/** Called before the activity is destroyed */
@Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}
}
}

Step 5: Create XML graphical layout

For generating an XML graphical layout for the MainActivity class, just navigate to the res, right click on the Layout tab, and create a new Android XML File.

Save this file as activity_my.xml. Now, open this file and add the below mentioned piece of code into it.

Code Snippet for activity_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity"
tools:ignore="MergeRootFrame">
<fragment
android:name="com.google.android.gms.example.bannerexample.MyActivity$PlaceholderFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/adFragment" />
<fragment
android:id="@+id/adFragment"
android:name="com.google.android.gms.example.bannerexample.MyActivity$AdFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>

Code Snippet for fragment_my.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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=".MyActivity$PlaceholderFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>

Code Snippet for the fragment_ad.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--ads:adUnitId sets the ad unit ID, which is defined in values/strings.xml -->
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" />
</RelativeLayout>

To ensure the Internet access, it is essential to add the “uses-permission” in the “AndroidManifest.xml” file. The below mentioned chunk of code will facilitate access to the Google Play services meta-data and AdActivity.

 <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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"            
android:theme="@android:style/Theme.Translucent" />

Code Snippet for the AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.admob" >
<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!--This meta-data tag is required to use Google Play Services.-->
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
</manifest>

On executing the process mentioned in this article, you will get the output as showcased in the following image.

Output:

AdMob tutorial 2015 Output

By following this AdMob tutorial 2015 steps thoroughly and deploying the aforementioned code properly, you can easily integrate an advertisement in your application. This way you can efficiently maximize your return on revenue and boost your business value.

About the author

Naaz

Naaz is a web designer and loves to find new tips and tricks for creativity purposes and likes to share them with the people.
Follow him on Google+ to get latest posts from the Author.

1 Comment

Leave a Comment

Get you FREE gift Today

Subscribe to our mailing list and get your free gift now