In the preview tutorial I explained how to add AdMob to your project, but we created the AdView in Java. So in this tutorial I will show you how to set it up in XML. Actually it's more simple this way, at least in my opinion :)
1. Create a new project (or use an existing one if you want to)
2. Go to res - layout - main.xml and change the LinearLayout to RelativeLayout (for simplicity...I used RelativeLayout and it's better for you to do the same so as not to become confused). This time I made the ad to be displayed at the bottom of the screen.
3. In the RelativeLayout add this code just after before the android:layout_width:
1. Create a new project (or use an existing one if you want to)
2. Go to res - layout - main.xml and change the LinearLayout to RelativeLayout (for simplicity...I used RelativeLayout and it's better for you to do the same so as not to become confused). This time I made the ad to be displayed at the bottom of the screen.
3. In the RelativeLayout add this code just after before the android:layout_width:
Now you have to introduce this code just before the RelativeLayout tag is closed:
?
<com.google.ads.AdView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/adMob"
android:layout_alignParentBottom="true"
ads:adUnitId="YOUR AdMob publisher ID"
ads:adSize="BANNER"/>
Your entire main.xml should look like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_layout">
<com.google.ads.AdView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/adMob"
android:layout_alignParentBottom="true"
ads:adUnitId="YOUR AdMob publisher ID"
ads:adSize="BANNER"/>
</RelativeLayout>
Now in the the activity class you must have this:
import android.app.Activity;
import android.os.Bundle;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
public class MyActivity extends Activity{
AdView adView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//request TEST ads to avoid being disabled for clicking your own ads
AdRequest adRequest = new AdRequest();
//test mode on EMULATOR
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
//test mode on DEVICE (this example code must be replaced with your device uniquq ID)
adRequest.addTestDevice("4G74FC73D62D42B62A7F7DA61EF5F776");
adView = (AdView)findViewById(R.id.adMob);
// Initiate a request to load an ad in test mode.
// You can keep this even when you release your app on the market, because
// only emulators and your test device will get test ads. The user will receive real ads.
adView.loadAd(adRequest);
}
@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
Now the result should be like this:
