Sopnil Shah
Sopnil Shah

Reputation: 61

showing same ad on different button click

Excuse my ignorance but I was trying to show an interstitial ad while user clicks on multiple button. I have four buttons and each lead to different activity. How can I show same ad (without creating extra ad units & variables) on multiple button click? Here is what I've done so far:

        Button btn1, btn2, btn3, btn4;
        interstitialAd.setAdListener(new AdListener()
                                     {
                                         @Override
                                         public void onAdClosed() {
                                             super.onAdClosed();
                                             Intent intent = new Intent(MainActivity.this, Activity1.class);
                                             startActivity(intent);
                                         }
                                     }
        );


        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (interstitialAd.isLoaded()) {
                    interstitialAd.show();
                } else {
                    Intent intent = new Intent(MainActivity.this, Activity1.class);
                    startActivity(intent);
                }


            }});

Upvotes: 1

Views: 64

Answers (3)

Nithis Kumar
Nithis Kumar

Reputation: 308

Use onAdClosed method in Interstitial listener to loadAd again if it's already shown and put show funtion on click listeners of the button. I thought four buttons are in the same activity (correct me if I'm wrong)

In your button's onClickListener you used if else statements like show Ads or Go to this activity I mean if unable to show ads then go to this activity, well if the ad was shown to the user then the user will stay at the same activity.

Try this :

btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (interstitialAd.isLoaded()) {
                    interstitialAd.show();
Intent intent = new Intent(MainActivity.this, Activity1.class);
                    startActivity(intent);
//***This is simple, when the button was clicked Ad will show, also the Activity1 will open.

} else {
                    Intent intent = new Intent(MainActivity.this, Activity1.class);
                    startActivity(intent);
                }


            }});

or You should add Interstitial Listener inside the Click listener method of the button, so you can open another activity when Ad was closed.

Upvotes: 1

Kevin Kurien
Kevin Kurien

Reputation: 842

onButttonClick show ad like Ratish said

OnClickListener onClickListener=new OnClickListener() {
        @Override
        public void onClick(View view) {
            position = 1(for first activity)
            if (interstitialAd.isLoaded()) {
                interstitialAd.show();
            } else{
                nextActivity();
            }
} 

then on Ad closed do go to next activity do this :

make a String array and store all package names in it and declare an int like :

    String optionSelected[] = {"com.sb.android.acg.upgrade.activity.firstActivity", "com.sb.android.acg.upgrade.activity.SecondActivity", "com.sb.android.acg.upgrade.activity.ThirdActivity", "com.sb.android.acg.upgrade.activity.FourthActivity", "com.sb.android.acg.upgrade.activity.fifthActivity", "com.sb.android.acg.upgrade.activity.SixthActivity"};

int position 

and in onAdClosed:

 @Override
 public void onAdClosed() {
    super.onAdClosed();
    nextActivity();
}

and in nextActivity

public void nextActivity(){
Intent intent;
        Class<?> aclass = Class.forName(optionSelected[position]);
        intent = new Intent(MainActivity.this, aclass);
         startActivity(intent);
}

hope you understood how to call next activity. If any doubt tell me in comment

Upvotes: 0

Ratish Bansal
Ratish Bansal

Reputation: 2462

You can create a instance of OnClickListener like

OnClickListener onClickListener=new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (interstitialAd.isLoaded()) {
                    interstitialAd.show();
                } else {
                    Intent intent = new Intent(MainActivity.this, Activity1.class);
                    startActivity(intent);
}
}

and assign this all button clicks like

btn1.setOnClickListener(onClickListener);
btn2.setOnClickListener(onClickListener);

Upvotes: 0

Related Questions