dira
dira

Reputation: 30594

Add AdMob Advertise to each and every Screen of Android App

I am able to load the AdMob add using following code into the very first screen (Activity) of the application.

@Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
        View next = findViewById(R.id.next);
        OnClickListener onClickListener = new OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(BannerEssentials.this, NextActivity.class));
            }
        };
        next.setOnClickListener(onClickListener);

        AdView adView = new AdView(this, AdSize.BANNER, "");
        layout.addView(adView);
        AdRequest request = new AdRequest();
        request.addTestDevice(AdRequest.TEST_EMULATOR);
        adView.loadAd(request);            
    }

The next screen (Activity) comes to foreground after pressing the Next button; but the advertise does not appear on this screen.

Is there any (easy) way to add AdMob add to each and every screen(Activity) of the applicaiton?

Upvotes: 0

Views: 1239

Answers (2)

Eric Leichtenschlag
Eric Leichtenschlag

Reputation: 8931

The reason you aren't seeing any ads is because you did not define a publisher id, but instead provided a blank string. Go to www.admob.com, sign up for a free account, and create an app to get a publisher id to use to get ads.

Upvotes: 0

yorkw
yorkw

Reputation: 41126

Create a super class and centralize all your admob related code into a protected method:

public class AdmobActivity extends Activity {
  ... ...

  protected void showAds(View root) {
    AdView adView = new AdView(this, AdSize.BANNER, "");
    root.addView(adView);
    AdRequest request = new AdRequest();
    request.addTestDevice(AdRequest.TEST_EMULATOR);
    adView.loadAd(request); 
  }

... ...
}

For all your activities that need show ads, extends AdmobActivity and call showAds() properly in Acticity.onCreate() method.

Hope that help.

Upvotes: 5

Related Questions