user2331788
user2331788

Reputation: 11

how to place ads in admob in app

how to use ad-mob in app and what are the steps how i post ads in ad-mob how can i test in my device before uploading to Google play store.i am new to ad-mob. please explain me with sample example of it by step-wise.

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

        this.interstitialAds = new InterstitialAd(this, "ca-app-pub-****");
        this.interstitialAds.setAdListener(this);

        Button loadButton = (Button) this.findViewById(R.id.loadButton);
        loadButton.setOnClickListener(loadButtonOnClick);

        this.textView = (TextView) this.findViewById(R.id.stateTextView);
    }

    private OnClickListener loadButtonOnClick = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            textView.setText("Loading Intertitial Ads");

            AdRequest adr = new AdRequest();
            // add your test device here
            adr.addTestDevice(AdRequest.TEST_EMULATOR);

            interstitialAds.loadAd(adr);
        }
    };

    @Override
    public void onDismissScreen(Ad arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
        String message = "Load Ads Failed: (" + error + ")";
        textView.setText(message);
    }

    @Override
    public void onLeaveApplication(Ad arg0) {
        // TODO Auto-generated method stub
    }

    /**
     * Called when an Activity is created in front of the app (e.g. an
     * interstitial is shown, or an ad is clicked and launches a new Activity).
     */
    @Override
    public void onPresentScreen(Ad arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onReceiveAd(Ad arg0) {
        if (interstitialAds.isReady()) {
            interstitialAds.show();
        } else {
            textView.setText("Interstitial ad was not ready to be shown.");
        }
    }
}

Upvotes: 0

Views: 94

Answers (1)

William
William

Reputation: 20196

  1. Get rid of your AdListener.
  2. Call intersitial.loadAd in your constructor
  3. Call interstitial.show at a natural break point in your app
  4. Call interstitial.loadAd
  5. Go to 3.

See https://developers.google.com/mobile-ads-sdk/docs/admob/android/interstitial

Upvotes: 1

Related Questions