Reputation: 13
I'm new to Android development and I'm trying to learn how to add Admob to exiting apps. Using the Admob SDK instructions I've been able to do this successfully on several of the apps I've tried. I have one however that for the life of me I can't get to go to the bottom of the screen like it should. I know its something in the main.xml but I can't figure it out.
Here is the code I have now that puts the ad block right in the center of the screen:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/eight_ball"
android:layout_gravity="center_vertical"
android:layout_margin="10px" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView android:id="@+id/MessageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/triangle"
android:text="@string/shake_me_caption"
android:focusable="false"
android:gravity="center_vertical|center"
android:layout_marginTop="14dip">
</TextView>
</LinearLayout>
</FrameLayout>
And the part of my main activity java file that deals with this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialization();
// Adding AdMob banner
adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
layout.addView(adView);
adView.loadAd(new AdRequest());
}
As I said I'm pretty sure its the main.xml that needs work (with probably some tweaking to the code in the main activity too) but no matter what I've tried the ad block either stays in the center or disappears altogether.
Thanks in advance.
Upvotes: 1
Views: 347
Reputation: 901
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId=""
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
place this code inn your xml file where ever you want to place. You dont need the to write the code you have wrote in your java file.
Just insert this code to the xml. Thats all. Cheers.
Upvotes: 1