You are here

Chapter 42: Putting the ads back in

In chapter 9, we laid the foundation to include AdMob ads and in chapter 10 we created our Main Menu page (main.xml) and verified that it could display ads.

However, we had to temporarily disable the AdMob integration in order to get Eclipse to help us building code that is guaranteed to work on Android 2.1.

But as our coding activities are finished (for the moment at least), we can now set the Project Build Target to 4.0.3 again, by chosing Project -> Properties in the Eclipse Menu, and select 4.0.3 again:

(Remember, our wanted target platform is Android 2.1, but in order to get Eclipse to build the project including AdMob SDK, Android 3.2 or greater must be chosen in Project Properties...)

When the project properties are changed, we can enable our "AdActivity" again. Open up the AndroidManifest.xml again and change it to:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ajomannen.justroids"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />
      <uses-permission android:name="android.permission.INTERNET"/>
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".JustRoidsActivity"
              android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity 
            android:name="com.google.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        />
        
        <activity 
            android:name=".GameActivity" 
            android:label="Game Activity" 
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
          />
          <activity 
            android:name=".HowtoActivity" 
            android:label="Howto Activity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
          />
          <activity 
            android:name=".CreditsActivity" 
            android:label="Credits Activity" 
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
          />
          
    </application>
</manifest>

Then a similar change in "res/layout/main.xml". Uncomment the AdView block:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/rootLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:adjustViewBounds="true"
        android:contentDescription="@string/justroids"
        android:src="@drawable/justroids" />

    <ImageView
        android:id="@+id/btnGame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imgLogo"
        android:layout_gravity="center_horizontal"
        android:clickable="true"
        android:contentDescription="@string/play"
        android:onClick="onClickGame"
        android:src="@drawable/play" />

    <ImageView
        android:id="@+id/btnHowto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnGame"
        android:layout_gravity="center_horizontal"
        android:clickable="true"
        android:contentDescription="@string/howto"
        android:onClick="onClickHowto"
        android:src="@drawable/howto" />

    <ImageView
        android:id="@+id/btnCredits"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnHowto"
        android:layout_gravity="center_horizontal"
        android:clickable="true"
        android:contentDescription="@string/credits"
        android:onClick="onClickCredits"
        android:src="@drawable/credits" />

    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="false"
        ads:adSize="BANNER"
        ads:adUnitId="<YOUR OWN ADMOB ID>"
        android:gravity="bottom"
        ads:loadAdOnCreate="false"
        ads:test="true"
        android:visibility="visible" />

</RelativeLayout>

Finally, also uncomment the ad-related block in JustRoidsActivity.java:

package com.ajomannen.justroids;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class JustRoidsActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AdView adView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest();
        adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
        adRequest.addTestDevice("<YOUR OWN DEVICE'S ID>");
        adView.loadAd(adRequest);
    }

    public void onClickGame(View v) {
        Intent intent = new Intent(this, GameActivity.class);
        startActivity(intent);
    }

    public void onClickHowto(View v) {
        Intent intent = new Intent(this, HowtoActivity.class);
        startActivity(intent);
    }

    public void onClickCredits(View v) {
        Intent intent = new Intent(this, CreditsActivity.class);
        startActivity(intent);
    }

}

That should be it! Now we have the ads back on the Main Menu page:

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer