Android : AdMob onClickListener
Asked Answered
J

5

5

I display into my android application AdMob's banners. I would like that when the user click on the banner it gone. I have try the code AdView.setOnClickListener but it not work...

EDIT : this is the code

private void visual_banner(){
//##### Pubblicità #####
        //Create the adView    
        adView = new AdView(this, AdSize.BANNER, "a14e5bed604ebf8");   
        // Lookup your LinearLayout assuming it’s been given    
        // the attribute android:id="@+id/mainLayout"    
        LinearLayout layout = (LinearLayout)findViewById(R.id.layout_ads_streaming);    
        // Add the adView to it    
        layout.addView(adView); 
        // Initiate a generic request to load it with an ad    
        adView.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                adView.destroy();
                img.setVisibility(View.VISIBLE);
            }
        });
        adView.loadAd(new AdRequest());
        //### FINE PUBBLICITA'
}
Jalisajalisco answered 12/3, 2012 at 10:18 Comment(2)
Maybe you want to show us what you've tried, so we'll see if there's a problem.Lists
I have edit my question and insert the codeJalisajalisco
E
4

I can help you with AdWhirl adds.

I have seen sources and have done next:

public class AdWhirlLayoutCustom extends AdWhirlLayout {

public AdWhirlLayoutCustom(Activity context, String keyAdWhirl) {
    super(context, keyAdWhirl);
}

public AdWhirlLayoutCustom(Context context, AttributeSet attrs) {
    super(context, attrs);
}

// We intercept clicks
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    boolean result = super.onInterceptTouchEvent(event);
    switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:
                    // Click!
        break;
    }

    return result;
}

}

Eurhythmic answered 12/3, 2012 at 12:12 Comment(1)
If you intercept the click event and hide the ad though, you won't get credit for the ad click, and that kind of defeats the purpose of showing ads in the first place.Sinless
S
12

Try using AdMob's adListener to listen for events.

public interface AdListener {
  public void onReceiveAd(Ad ad);
  public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode error);
  public void onPresentScreen(Ad ad);
  public void onDismissScreen(Ad ad);
  public void onLeaveApplication(Ad ad);
}

Have your class implement the listener, and then add the listener to the adView:

adView.setAdListener(this);

Implement the onDismissScreen event, which occurs once your app resumes control after handling the ad click. At this point, you can remove the AdView, and you will have gotten credit for the click.

@Override
public void onDismissScreen(Ad ad) {
  if (adView != null) {
    adView.destroy();
  }
}
Sinless answered 12/3, 2012 at 23:23 Comment(0)
E
4

I can help you with AdWhirl adds.

I have seen sources and have done next:

public class AdWhirlLayoutCustom extends AdWhirlLayout {

public AdWhirlLayoutCustom(Activity context, String keyAdWhirl) {
    super(context, keyAdWhirl);
}

public AdWhirlLayoutCustom(Context context, AttributeSet attrs) {
    super(context, attrs);
}

// We intercept clicks
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    boolean result = super.onInterceptTouchEvent(event);
    switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:
                    // Click!
        break;
    }

    return result;
}

}

Eurhythmic answered 12/3, 2012 at 12:12 Comment(1)
If you intercept the click event and hide the ad though, you won't get credit for the ad click, and that kind of defeats the purpose of showing ads in the first place.Sinless
A
1

You can implement onAdLeftApplication() of AdListener interface. This method is called when an ad leaves the application (e.g., to go to the browser).

adView.setAdListener(new AdListener() {

        @Override
        public void onAdLeftApplication ()
        {
           //Do your stuff
        }
    }

});

For more info check below link https://developers.google.com/android/reference/com/google/android/gms/ads/AdListener.html#onAdLeftApplication()

Archibold answered 9/1, 2016 at 5:56 Comment(0)
G
1

I agree with above all the answer but i am just extending the answer. I have done like below

Load the ad in AdView as below

AdView adView = (AdView)findViewById(R.id.adView);
adView.loadAd(new AdRequest.Builder()
                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                    .addTestDevice("your device id")
                    .build());

Now next set the ad listener on that adView. Take one global boolean variable like isClicked so whenever user will click on ad and switch on that page onAdLeftApplication() method will called so make that variable as true. When user will click on ad then redirect to that ad page so current app will goes to onPause state.

adView.setAdListener(adListener);

com.google.android.gms.ads.AdListener adListener = new AdListener() {
        @Override
        public void onAdClosed() {
            super.onAdClosed();
        }

        @Override
        public void onAdFailedToLoad(int i) {
            super.onAdFailedToLoad(i);
        }

        @Override
        public void onAdLeftApplication() {
            super.onAdLeftApplication();
            isClicked = true;
        }

        @Override
        public void onAdOpened() {
            super.onAdOpened();
        }

        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
        }

        @Override
        public void onAdClicked() {
            super.onAdClicked();
        }

        @Override
        public void onAdImpression() {
            super.onAdImpression();
        }
    };

Now whenever user will come back to the current app activity onResume() method is called, So implement your other stuff will be there or as below

@Override
    protected void onResume() {
        super.onResume();
        if (isClicked){
            isClicked = false;
            // do your other stuff whatever you want;
        }
    }
Grettagreuze answered 13/2, 2018 at 7:58 Comment(0)
A
0

Use an AdListener class, with onAdClicked implemented. The class is documented here.

AdView adView = view.findViewById(R.id.adView);

adView.setAdListener(new AdListener() {
    @Override
    public void onAdClicked() {
        super.onAdClicked();
        // Code here
    }
});
Antemundane answered 7/4 at 23:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.