Andrew
Andrew

Reputation: 31

Adding AdMob Ad to RelativeLayout

I have my app completely done. I want to add my admob ad to the bottom of my screen. I have the jar file imported and all that. I just cannot figure out how to get the ad at the bottom of the screen then what I need in the .java/main.xml/manifest.xml I tried a few tutorials but just got a force close.

Upvotes: 2

Views: 8325

Answers (1)

Steven
Steven

Reputation: 798

Have you found the following site from Admob?

http://code.google.com/mobile/ads/docs/android/fundamentals.html

This is a really good guide on how to integrate the sdk. It explains what must be declared in the manifest, layout and code itself. Be sure to follow the "create your banner in XML." Link on this page - this will show you how to setup your main xml. Where it states,

<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"                          
ads:adSize="BANNER"                         
ads:loadAdOnCreate="true"/>

Simply add the tag,

android:layout_alignParentBottom="true" 

to position the ad at the bottom of the layout. So if your using a relative layout it will appear something like,

 <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <com.google.ads.AdView  
      android:id="@+id/ad" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      ads:backgroundColor="#000000"
      ads:adUnitId="<Your Ad Unit ID>"
      ads:primaryTextColor="#FFFFFF"
      ads:secondaryTextColor="#CCCCCC"
      ads:adSize="BANNER"
    />
</RelativeLayout>

Because you are using RelativeLayout, replace the banner example code with,

 // Create the adView     
AdView adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);     
// Lookup your RelativeLayoutLayout assuming it’s been given     
// the attribute android:id="@+id/ad"     
RelativeLayoutlayout = (RelativeLayout)findViewById(R.id.ad);     
// Add the adView to it
layout.addView(adView);     
// Initiate a generic request to load it with an ad     
adView.loadAd(new AdRequest());

Noticed, on a seperate note I think there is a typo on the Advanced tab for this site in the code sample at the InterstitialAd section of Advanced -

interstitialAd.loadAd(adRequest); 

should read,

interstitial.loadAd(adRequest);

Hope this helps

Upvotes: 9

Related Questions