Android: AdMob onClickListener

I display AdMob banners in my android apps . I would like the user to disappear by clicking on the banner. I tried the code AdView.setOnClickListener

but it doesn't 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'
}

      

+3


source to share


4 answers


I can help you with adding AdWhirl.

I saw the sources and did the following:



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;
}

      

}

+2


source


Try using the AdMob 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);
}

      

Ask your class to implement a listener and then add the listener to adView:



adView.setAdListener(this);

      

Implement the onDismissScreen event, which occurs when your app resumes control after processing a click on the ad. At this point, you can uninstall AdView and you will receive CPC credit.

@Override
public void onDismissScreen(Ad ad) {
  if (adView != null) {
    adView.destroy();
  }
}

      

+10


source


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

adView.setAdListener (new AdListener () {

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

      

});

See link below for details https://developers.google.com/android/reference/com/google/android/gms/ads/AdListener.html#onAdLeftApplication ()

+1


source


I agree with the answer first of all, but I am just extending the answer. I did as below

Load your ad AdView

below

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

      

Now we need to set the ad listener to AdView

. Take one global boolean variable of type isClicked

, so whenever the user clicks on the ad and includes that page, the method onAdLeftApplication()

is called to make this variable true. When the user clicks on the ad and then redirects to that page, so that the current app goes into the 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 when the user returns to the current action of the application the method will be called onResume()

, so your other stuff will be implemented there or below

@Override
    protected void onResume() {
        super.onResume();
        if (isClicked){
            isClicked = false;
            // do your other stuff whatever you want;
        }
    }

      

0


source







All Articles