Ahmad
Ahmad

Reputation: 72653

Android - AdMob Set Ads to GONE?

I wanted to ask, how to implement an AdListner for Admob. I want the Ad to disapear if its clicked. I tried this but it did not help.

final AdView ad1 = (AdView) findViewById(R.id.ad1);
        ad1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                ad1.setVisibility(View.GONE);
                ad1.destroy();          }
        });

thank you.

Upvotes: 2

Views: 352

Answers (3)

Ahmad
Ahmad

Reputation: 72653

If someone is still searching for this, here is the code I used:

public  class myActivity extends Activity implements AdListener{
    /** Called when the activity is first created. */


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


         AdView adView = (AdView)this.findViewById(R.id.ads2);
         adView.setAdListener(this);
        adView.loadAd(new AdRequest());





    }

    public void onDismissScreen(Ad arg0) {
        RelativeLayout rellayout = (RelativeLayout) findViewById(R.id.rellayout);
        AdView adView = (AdView)this.findViewById(R.id.ads2);
        rellayout.removeView(adView);
    }

    public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
        // TODO Auto-generated method stub

    }

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

    }

    public void onPresentScreen(Ad arg0) {
        // TODO Auto-generated method stub

    }

    public void onReceiveAd(Ad arg0) {
        // TODO Auto-generated method stub

    }

}

Upvotes: 0

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

I think as AdView is RelativeLayout so you need to

 * ad1.setClickable(true);

or

 * can  put entry in layout XML `android:clickable="true"`

enter image description here

Upvotes: 1

petey
petey

Reputation: 17160

Depending on the device you are on, you may need to use that View's invalidate() method. (I would notice I needed to do this for some ad stuff on freewheel on 2.1 devices).

-- OR -- Try placing your ad1 view in a wrapper view (LinearLayout or something small) and then setting that wrapper view's visibility to View.GONE.

Upvotes: 1

Related Questions