Adding AdMob ads to your applications

3 min


0

This publish will have a look at placing adverts into your utility. To use this code you’ll need to join to google’s AdMob service. I’m going to assume that you’ve accomplished all that and have a writer id that you need to use for this instance.

OK, so how to go about this. The thought is to create an advert on the backside of the display screen that’s invisible earlier than the advert arrives, and slides up from the underside of the display screen when it arrives.

Step 1 is to create a brand new Android utility in eclipse referred to as AdMobExample. When you’ve accomplished that you just want to proper click on on the challenge and choose Properties. On the Properties for AdMobExample dialog, click on on Java Build Path from the listing on the appropriate. You ought to now see one thing like this:

To add the GoogleAdMobAdsSdk-4.0.4.jar library you want to click on on the Add External Jars button and choose the jar file (it’s essential to set up the library your self by downloading the SDK from the AdMob website.) I put in the SDK to /usr/lib (on Linux), this is perhaps one thing like C:/Program Files/GoogleAdsMobAdsSdk on Windows.

Now we will begin to construct the app. First up is the AndroidManifest.xml file. We want add an exercise to the <utility> in order that AdMob can do its factor. Add this simply earlier than the </utility> tag:

1
2
3
4
<!-- AdMobActivity definition -->
<exercise android:title="com.google.ads.AdActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:configChanges="orientation|keyboard|keyboardHidden" />

In order for this exercise to work it wants entry to the web, and entry to the community state of the system. This implies that we should request these permissions by including these strains after the <utility> tag:

1
2
3
    <!-- AdMob SDK requires Internet permission  -->
    <uses-permission android:title="android.permission.INTERNET" />
    <uses-permission android:title="android.permission.ACCESS_NETWORK_STATE" />

Finally, AdMob can use the system’s location when it serves adverts. To allow this characteristic you want to add the next after the &jt;uses-permission> strains :

1
2
    <!-- Allow AdMob to use the units location when delivering ads -->
    <meta-data android:worth="true" android:title="ADMOB_ALLOW_LOCATION_FOR_ADS" />

That completes the adjustments to the manifest. Now we’d like to create a structure that can lock the adverts to the underside of the display screen. This is finished through the use of RelativeLayout. Here is the entire foremost.xml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml model="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/com.example.admobexample"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 
<!-- This is the primary view. Here it's only a easy TextView, however it'll
      most likely be one other structure in a extra advanced app -->
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textual content="@string/hello"
    />
 
<RelativeLayout
     android:layout_width="wrap_content"         
     android:layout_height="wrap_content"         
     android:layout_alignParentBottom="true" >  
 
	<!-- Set myapp:adUnitId to be your writer ID -->
    <com.google.ads.AdView
        android:id="@+id/ad"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        myapp:adUnitId="000000000000000"
        myapp:adSize="BANNER"
        myapp:refreshInterval=
     />
 
</RelativeLayout>
</RelativeLayout>

The first RelativeLayout accommodates the primary view, on this instance it’s only a easy TextView, adopted by one other RelativeLayout that accommodates the view for the advert. In phrases of the structure, the essential a part of this structure is line 19, setting the android:layout_alignParentBottom aspect to true tells android to put the AdView on the backside of the display screen. To get the view to ship your adverts you want to change the myapp:adUnitId worth to be your writer id.

Now we now have bought our manifest and structure configured all that’s left is to request an advert within the onCreate methodology:

1
2
3
4
5
6
7
8
9
10
public void onCreate(Bundle savedInstanceState) {
    tremendous.onCreate(savedInstanceState);
    setContentView(R.structure.foremost);
 
    // Request an advert
    AdvertRequest advert = new AdvertRequest();
    advert.setTesting(true);
    AdView adView = (AdView)findViewById(R.id.advert);
    adView.loadAd(advert);
}

The setTesting(true) name tells AdMob to simulate adverts within the emulator, however it’ll nonetheless serve actual adverts on a tool.

If you run this now within the emulator it is best to see a display screen like this:

If you deploy this to a tool then it’ll ship an actual advert.

Download

You can obtain this instance from right here.

To add this instance to eclipse go to File|New|Android Project, choose “Create project from existing source” after which browse to the folder within the downloaded zip file.


What's Your Reaction?

hate hate
0
hate
confused confused
0
confused
fail fail
0
fail
fun fun
0
fun
geeky geeky
0
geeky
love love
0
love
lol lol
0
lol
omg omg
0
omg
win win
0
win

0 Comments

Your email address will not be published. Required fields are marked *

Choose A Format
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Poll
Voting to make decisions or determine opinions
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Meme
Upload your own images to make custom memes
Video
Youtube, Vimeo or Vine Embeds
Audio
Soundcloud or Mixcloud Embeds
Image
Photo or GIF
Gif
GIF format