Zwiebel
Zwiebel

Reputation: 1615

How I can place admob ad on an Activity within I don't use XML?

public class AGame extends Game{
    @Override
    public Screen getStartScreen() {
        return new LoadingScreen(this); 


}

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    RelativeLayout rl = new RelativeLayout(this);
    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.FILL_PARENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

    AdView adView = new AdView (this, AdSize.BANNER, "MY_ADMOB_ID");
    rl.addView(adView, adParams);
    AdRequest adRequest = new AdRequest();
    adRequest.setTesting(true);
    adView.loadAd(adRequest);
}

}

I tried this code, but it doesn't show ads on any screen. This code that I pasted here, is my first activity and from it, it's going to the newer screens. I want to show ads in my all activities. If I place a setContentView(rl); row below my adView.loadAd(adRequest); it's only showing an ad and a black screen. I think that I need to implement my ad in this actvity, because in the others, I can't use the onCreate method. Please help me. Thanks for any helps! And sorry, I'm not english.

Edit: LogCat writes to me that I got ads, I only can't see them.

Upvotes: 0

Views: 328

Answers (1)

Daniel Argüelles
Daniel Argüelles

Reputation: 2349

I think you forgot:

addView(rl);

Something like:

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    RelativeLayout rl = new RelativeLayout(this);
    RelativeLayout.LayoutParams adParams = new     RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
        RelativeLayout.LayoutParams.FILL_PARENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    addView(rl);

    AdView adView = new AdView (this, AdSize.BANNER, "MY_ADMOB_ID");
    rl.addView(adView, adParams);
    AdRequest adRequest = new AdRequest();
    adRequest.setTesting(true);
    adView.loadAd(adRequest);
}

Upvotes: 3

Related Questions